639

How can I format an inline code in Confluence like this? I mean, not a separate code block, but just inline classname, for example.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
DataGreed
  • 13,245
  • 8
  • 45
  • 64

16 Answers16

768

To insert inline monospace font in Confluence, surround the text in double curly-braces.

This is an {{example}}.

If you're using Confluence 4.x or higher, you can also just select the "Preformatted" option from the paragraph style menu. Please note that will apply to the entire line.

Full reference here.

2240
  • 1,547
  • 2
  • 12
  • 30
jaysee00
  • 8,242
  • 1
  • 15
  • 10
  • 146
    In confluence 4+ the wiki markup is gone by default and the "Preformatted" option applies to the entire line. The only way to do it (without installing the wikitext editing plugin) is to pick monospaced font. – Dave Mar 27 '13 at 17:14
  • 20
    It won't work if you're pasting it in - you need to type out the closing braces in order to "trigger" the "auto-format". If you want to format large swathes of text in this manner, then use the Insert -> Wiki Markup dialog and type/paste it out all in there. – jaysee00 Apr 17 '13 at 06:31
  • 4
    I have found that using Ctrl-Shift-D to insert wiki markup is useful. It is the only combo shortcut I use in V5. Yes, the new editor can be unwieldy. Entering code via the wiki editor helps, especially if you are used to markup coding instead of gui. – Underverse Dec 30 '13 at 03:43
  • 32
    A reliable way to make this work is to type in {{foo}}; it always recognises 'foo' as needing preformatting, then paste whatever you really wanted to put in there between the 'f' and the 'o', then remove the 'foo' text. You should be left with what you really wanted, preformatted. – Dan Mitchell Sep 03 '14 at 19:20
  • 2
    @DanMitchell, the thing here is that one needs to _type_ the text that doesn't need formatting and then _type_ the closing curly braces in order to be detected as preformatted text. Confluence simply doesn't do that with pasted text. However, as you've noticed, it can be fooled by typing something short in double curly braces. A single character will do just as well, really. – user3099609 Jan 21 '15 at 16:52
  • 1
    @DanMitchell : thanks for that Dan ! Indeed however, as @user3099609 said, one char is enough: `{{a}}`, then go behind the `a`, paste the text, and press `Canc` to delete the bloody `a`==> 3x faster than `{{foo}}` method! :-D – Campa Jul 24 '15 at 14:27
  • 8
    what about a different background/text color ? monospace isn't the only thing, I wanted to highlight it inside the row, any ideas ? – Ricky Levi Mar 20 '17 at 10:28
  • 1
    It doesn't work for my comments (using Confluence 6.1.2) – ericn Nov 27 '17 at 08:19
  • 2
    I do Ctrl-Shift-D to bring up the insert wiki markup, then type it in with `{{` and `}}`. This always seems to work, even when copying text. The overhead is small versus never having to fight the system. – Duncan Jones May 02 '18 at 14:40
  • At least one of the closing braces must be typed _after_ the opening braces. :rolleyes: – OJFord Jun 10 '18 at 14:59
  • The most useful tip of the year! – Worker Sep 07 '18 at 13:58
  • This doesn't work if your text to be formatted has curly braces in it -- you might have to escape them with a backslash, but then it will probably break if you try to edit the post. Atlassian is a *mess*. – Coderer Jan 29 '19 at 14:44
  • This is work at confluence server 6.15 as well. However I found typing closing curly braces *occasionally* doesn't work – Louis Go May 14 '20 at 03:21
  • 1
    I found {{ }} only works in editor while full texts is typed, it doesn't work if text is copy-pasted. – Louis Go May 14 '20 at 03:47
  • It does not seem to work on my current version of confluence on 2023...also do not know how to get the version. On the about sections says: Confluence 1000.0.0-65c4fc7c3915 – nicoramirezdev Jan 16 '23 at 07:49
88

By default Confluence renders monospaced text with transparent background. You can edit global CSS to add grey color. From Confluence manual:

  1. Choose the cog icon at top right of the screen, then choose Confluence Admin.
  2. Choose Stylesheet.
  3. Choose Edit.
  4. Paste your custom CSS into the text field.
  5. Choose Save.

Custom CSS for displaying grey background in monospaced blocks:

code {
    padding: 1px 5px 1px 5px; 
    font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif;
    background-color: #eeeeee;
}

If you're using Confluence OnDemand (cloud):

  1. Click the cog/gear in the bottom of the sidebar on the left
  2. Select Look and Feel
  3. Click the Sidebar, Header and Footer tab
  4. Paste your custom CSS into the Header field
  5. Wrap the code in a {style} block
  6. Save

Paste the following:

{style}
code {
    padding: 1px 5px 1px 5px; 
    font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif;
    background-color: #eeeeee;
}
{style}

After that you'll get nice and tidy stackoverflow-stylish inline code spans just by writing {{sometext}}.

Soviut
  • 88,194
  • 49
  • 192
  • 260
goodov
  • 1,358
  • 10
  • 12
  • 1
    If your space uses a custom stylesheet/theme, this may not work. A lazy workaround is to append the above example inside style tags under "Look and Feel -> Custom HTML" (tested on version 5.9.6). – jdknight Mar 11 '16 at 20:45
  • 3
    If using Confluence OnDemand, you have to go to the "Sidebar, header and footer" page. Then in the header textbox copy/paste the above CSS code, but _you need to put `{style}` before and after the CSS_. [Example of `{style}` usage](https://answers.atlassian.com/questions/99055/editing-csss-in-confluence-ondemand). – Phil Mar 30 '16 at 14:33
  • 1
    Minor correction, you seem to need to escape the curly braces inside the style block, or else confluence tries to find a macro called padding. {style} code \{ ... \} {style} – edqwerty Nov 08 '16 at 15:39
  • 4
    This works, however in my OnDemand instance (cloud) I am now receiving an error "The following macros are not currently supported in the header: style" at the top of the page. Yet it still renders. Go figure :) – GONeale Feb 15 '18 at 05:47
  • @GONeale were you able to apply the CSS without having that error display? – GraehamF Jan 31 '19 at 21:45
  • 2
    @GraehamF @GONeale I just included `[class^=SpaceCustomSettingsBlockComponent_unsupportedMacros]{display: none;}` in the custom CSS :p – Samizdis May 02 '19 at 13:42
  • Perfect. (Confluence Cloud) I added: `color: red; font-weight: bold;` in mine to make it standout even more so. – Ari Jun 13 '19 at 07:16
73

Confluence 4 and later

As of Confluence 4 and above, typing two curly brackets does not work.

You now need to select Monospace font. Highlight the text you want to change and:

Windows: Ctrl + Shift + M

Mac: Command + Shift + M

Alternatively, you can type a backtick (`) and Confluence will format everything until you type another backtick

Alternatively, next to the bold and italic options, you can click the "more" drop down and select Monospace:

enter image description here

Ian Jamieson
  • 4,376
  • 2
  • 35
  • 55
  • 9
    Please upvote, that's the correct answer in 2020+ – Sliq Apr 07 '21 at 14:38
  • For me, copy a code snippet copied from a Windows NotePad window to Confluence, it adds an extra line between each real line. This happens when using "preformatted" paragraphs or monospace font. I assume this is one of those Unix/Linux linefeed versus Windows CR/LF problems. I can go into markup and use the
     
    tags, but then I have to manually change < and > to < and > (ie., HTML special character encoding). So this works, but it's painful. Is there a way to prevent the extra lines?
    – Ben Slade Apr 21 '22 at 16:01
29

You could ask your fiendly Confluence administrator to create a macro for you. Here is an example of a macro for Confluence 3.x

Macro Name:    inlinecode
Macro Title:   Markup text like stackoverflow inline code
Categories:    Formatting
Macro Body Processing: Convert wiki markup to HTML
Output Format: HTML
Template:

## Macro title: Inline Code
## Macro has a body: Y
## Body processing: Convert wiki markup to HTML
## Output: HTML
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name
## This makes the body text look like inline code markup from stackoverflow
## @noparams
<span style="padding: 1px 5px 1px 5px; font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; background-color: #eeeeee;">$body</span>

Then users can use {inlinecode}like this{inlinecode}

You could also use the {html} or {style} macros if they are installed or add this style to the stylesheet for your space.

While you are at it ask your Confluence admin to create a kbd macro for you. Same as the above, except Macro name is kbd and Template is:

<span style="padding: 0.1em 0.6em;border: 1px solid #ccc; font-size: 11px; font-family: Arial,Helvetica,sans-serif; background-color: #f7f7f7; color: #333;  -moz-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset; -webkit-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset; box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; display: inline-block; margin: 0 0.1em; text-shadow: 0 1px 0 #fff; line-height: 1.4; white-space: nowrap; ">$body</span> 

Then you can write documentation to tell users to hit the F1 and Enter keys.

Underverse
  • 1,271
  • 21
  • 32
20

Surround your inline text with {{ }}.

Caveats:

  1. You have to hit the spacebar after }}
  2. You can't copy inline preformatted text and maintain it's look. If you do copy it you might not be able to add {{ }} to fix it. Just retype it or paste without formatting (Cmd ⌘+Shift+V on Mac) then add {{ }} and hit space.
  3. If you add the {{ }} to existing text later, it can not be surrounded by other characters, e.g. if you want parenthesis around your preformatted text, you cannot fix (my text) by adding braces ({{my text}}). First add space around your text ( my text ) then add the {{ }}.
Nelu
  • 16,644
  • 10
  • 80
  • 88
12

In Confluence 5.4.2 you can add surround your inline code with <code></code> tags in the source editor thusly:

Confluence will show <code>this inline code</code> in a fixed font. 

This can be useful where there are many fragments to modify since the double-brace feature only works when adding text interactively in the Confluence editor.

Community
  • 1
  • 1
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
11

If you want to insert a large code block with optional line numbers, etc use the Code Macro (available under Macros -> Other).

alex
  • 479,566
  • 201
  • 878
  • 984
10

To format code in-line within your text, use the '`' character to surround your code. Usually located to the left of the '1' key on your keyboard.

Example:

`printf("Hello World");`

Same delimiter as Stack Exchange!

Chris Lawlor
  • 47,306
  • 11
  • 48
  • 68
morrisonbrett
  • 121
  • 1
  • 5
  • 2
    ADM - I was showing the code _with_ the delimiters, and you un-escaped them. Now the code looks like code, which is not the intent of my answer. The intent of my answer is to demonstrate that on Confluence the delimiter character is \` which happens to be the same as SO. – morrisonbrett Mar 14 '18 at 01:55
  • 4
    What version of Confluence does this work on? My instance doesn't treat backticks as special characters. – Coderer Jan 29 '19 at 14:46
  • 1
    Doesn't work for me either, actually this was the first thing I tried. – Ric Hard Feb 19 '21 at 09:36
  • This works as of Confluence 7.x. – UtahJarhead Feb 16 '23 at 17:40
7

At the time of this writing, I found that neither {{string}} nor {{ string }} work. My control panel only had the code block button.

However, there was a shortcut listed for fixed-width formatting: Ctrl+Shift+M.

I poked around in the menus but was not able to find out what version is being served to us.

user151841
  • 17,377
  • 29
  • 109
  • 171
6

I found formatting with colors a bit trickier as Confluence (5.6.3) is very fussy about spaces around the {{monospace}} blocks.

Creating Colored Monospace with Wiki Markup

As rendered by Confluence

xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
5

Mark your text with the mouse or cursor and then press:

  • Mac: ⌘ + ⇧ + M (CMD, Shift and M keys)
  • Windows/Linux: CTRL + ⇧ + M (CTRL, Shift and M keys)
Sliq
  • 15,937
  • 27
  • 110
  • 143
4

Easiest way for me is to Insert Markup.

Confluence Insert Markup

Then in text box type the text between curly braces.

It will insert the formatted text in a new line but you can copy it anywhere, even inline.

Ena
  • 3,481
  • 36
  • 34
2

I use a combination of Zelphir and Peter Gluck's answers. i.e.

  1. (Mac-speak) Click COMMAND + SHIFT + D
  2. Select Markdown in the dropdown of the model pop-up
  3. Surround your text with the code tag - i.e. <code>bovvered</code>
  4. Click INSERT
Sam Kenny
  • 395
  • 4
  • 9
  • When I tried this, copying and pasting from Linux terminal output, it loses the newlines. All the code is mushed together on one line – Ben Slade Jun 08 '21 at 20:41
1

The easiest way I've found to do this is to write in markdown right from the start of the line. Press Ctrl+D (shortcut for opening the markup input dialog) and type markdown. The normal wiki editor doesn't seem to be very good for precise formatting. It doesn't seem to know much about character styles and only knows paragraph styles.

Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
0

All of these other answers certainly sound like good ideas and I would recommend using them first, but I will go ahead and add one more to the list for completeness' sake.

You could simply use the html macro and then paste your content wrapped in <pre> </pre> tags.

Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • This can achieve the same effect but should be avoided unless there is a need to markup the text within the block with other HTML tags. – Underverse Jan 20 '16 at 01:10
  • Yes, that is why I recommended using the solutions provided in the other answers first. I never said it was a good answer. I said I wanted to add it to the list for completeness sake. – Alexander Ryan Baggett Dec 06 '16 at 00:47
  • This method is the perfect answer if you need fine control of colours and styles in a code block in Confluence 3.5. I created a `{pre}` macro for our users as a shortcut for `{html}
     ... 
    {html}`. Confluence 5 is another matter entirely.
    – Underverse Dec 09 '16 at 00:02
-2

If you have WinWord, you can copy what you need into that, touch up the results, and then paste that into Confluence. I've found that easier than the other solutions here.

Alkanshel
  • 4,198
  • 1
  • 35
  • 54