[confluence] How to format an inline code in Confluence?

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

This question is related to confluence

The answer is


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.


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!


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.


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.


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

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.


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.


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


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}}.


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


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.


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 {{ }}.

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


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.


Examples related to confluence

How to format an inline code in Confluence?