This documentation relates to Confluence 3.1.x
If you are using an earlier version, please view the previous versions of the Confluence documentation and select the relevant version.

Code Block Macro

The Code Block Macro allows you to display source code in your document with the appropriate syntax highlighting. The code block displays on the page as shown below.

public static void main(String[] args) 
{ 
  System.out.println("Hello World!");
}

On this page:

Usage with the Macro Browser

To insert the code block macro into a page using the Macro Browser,

  1. Open your desired Confluence page or blog post, then click the 'Edit' button. The page or blog post opens in edit mode.
  2. Click the Macro Browser icon on the toolbar. The macro browser window opens in the middle of the screen in 'macro selection' mode.
  3. Scroll through the list of macros. Alternatively, start typing part of the name of your desired macro into the search box at the top right of the macro browser window. Macros with a matching name will appear in the main pane.
  4. Click on the desired macro to access its parameters and preview parameter changes ('preview mode').
  5. Set the macro parameters to your requirements. If desired, you can preview these changes by clicking 'Refresh'.
  6. Click 'Insert' to put the macro into the page.

Once you've found the code block macro, click 'insert' to add it to your page.

Usage with the Wiki Markup Editor

What you need to type What you will get
{code}
public static void main(String[] args) 
{code}
 public static void main(String[] args) 

Parameters

Parameters are options that you can include in Confluence macros to control the content or format of the macro output. The table below lists relevant parameters for this macro.

Parameter names are different in the macro browser and in wiki markup. Below we show the macro browser parameter names in bold text, and the equivalent wiki markup parameters in (bracketed) text. If we do not show any parameter name for the wiki markup, then you should leave out the parameter name and simply include the parameter value as the first parameter, immediately after the colon (:).

Parameter Default Description
Code Language
(code:)
Java Specifies the programming language for syntax highlighting. The default language is Java but you can specify JavaScript, ActionScript, XML, HTML or SQL instead.

Be aware that any white space contained between the {code} commands is not manipulated in any way by the Code Block Macro. This is to provide the writer with flexibility over code indentation.

All the optional parameters of the {panel} macro are valid for the {code} macro as well.

Examples


Example 1: Java

What you need to type What you will get
{code}
public String getFoo()
{
return foo;
}
{code}
public String getFoo()
{
return foo;
}

Example 2: XML

What you need to type What you will get
{code:XML}
<test>
<another tag="attribute"/>
</test>
{code}
<test>
<another tag="attribute"/>
</test>

Example 3: HTML

What you need to type What you will get
{code:HTML}
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html;">
<TITLE>xmldemo_002</TITLE>
</HEAD>
</HTML>
{code}
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html;">
<TITLE>xmldemo_002</TITLE>
</HEAD>
</HTML>
RELATED TOPICS

Working with Macros
Adaptavist User Guide

Take me back to the Confluence User's Guide.

Labels

macro macro Delete
code code Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Nov 30, 2007

    Anonymous says:

    Is it possible to add more languages? Like bash/ksh/sh, jython, etc.?

    Is it possible to add more languages? Like bash/ksh/sh, jython, etc.?

    1. Dec 02, 2007

      Choy Li Tham says:

      Hi, Unfortunately, Confluence does not support this feature. However, there is ...

      Hi,

      Unfortunately, Confluence does not support this feature. However, there is an improvement request currently being tracked in the following:

      Please do cast your vote to increase its popularity and add yourself as a watcher to the updates. Also, feel free to add comment in the improvement to truly reflect your ideas. Thanks.

      Regards,
      Choy Li

    2. Dec 02, 2007

      David Soul [Atlassian] says:

      While neither list bash or jython, you may wish to check out the http://confluen...

      While neither list bash or jython, you may wish to check out the http://confluence.atlassian.com/display/CONFEXT/New+Code+Macro and http://confluence.atlassian.com/display/CONFEXT/Pygments+Code+Macro plugins which support a wider range of languages and have a more pluggable interface for new languages.

  2. Feb 14, 2008

    Anonymous says:

    Why is there a scroll bar on the right hand side of the code block that doesn't ...

    Why is there a scroll bar on the right hand side of the code block that doesn't even slide? Whenever I create a code block it makes a scroll bar that is just the size of the block! Thanks

    1. Feb 14, 2008

      Choy Li Tham says:

      Hi, Did you meant to say that you would like to control the scrollbar display i...

      Hi,

      Did you meant to say that you would like to control the scrollbar display in the Code Block macro? If that is the case, there is an improvement request being raised at the follwoing:

      If you are keen on this improvement, please do cast your vote to increase its popularity and add yourself as a watcher for future updates. Also, feel free to add comments to reflect the importance of this improvement.

      Regards,
      Choy Li

  3. Feb 28, 2008

    Anonymous says:

    Are there any plans to provide syntax highlighting directly within the Wiki Mark...

    Are there any plans to provide syntax highlighting directly within the Wiki Markup editor for Atlassian wiki markup?  It would make it a lot easier to edit pages if basic syntax highlighting were present in that view.

    1. Feb 28, 2008

      Ming Giet Chong says:

      Hi, There is an improvement request being raised at: http://jira.atlassian.c...

      Hi,

      There is an improvement request being raised at:

      Please feel free to cast your vote and add yourself as a watcher for future updates. Thanks.

      Regards,
      MG

  4. Jun 13, 2008

    Anonymous says:

    The XML code formatter doesn't colorize correctly when an element is split over ...

    The XML code formatter doesn't colorize correctly when an element is split over 2 or more lines.

    <element type="not_split" colorized="correctly">
    </element>
    
    <element type="split" 
             colorized="incorrectly">
    </element>
    
    1. Jun 17, 2008

      Ming Giet Chong says:

      Hi, Please raise a support ticket at the code macro's issue tracker for further...

      Hi,

      Please raise a support ticket at the code macro's issue tracker for further investigate to this issue:

      Regards,
      MG

  5. Jul 11, 2008

    Treg Anderson says:

    Hi, I have added this comment to the New Code Macro comment section but I have s...

    Hi, I have added this comment to the New Code Macro comment section but I have seen this problem using this macro as well.

    I am having a problem using the New Code Macro on IE 7.0.  If I have one long line of text within the {code} tags the macro produces a scrollbar at the bottom of the code box but it covers up the single line of text. I am currently using IE 7.0 to enter this comment and when I shrink the width of the browser the scroll bar pops up over the text on the example below.

    This is some text that could cause a horizantal scroll bar with a narrow enough window

    With the window wide enough the scroll bar goes away and the text can be seen.

    Is this a bug that is known and is in the process of being fixed? I looked for the source code to take a look at it here but there doesn't seem to be any to download.

    1. Jul 14, 2008

      Tony Cheah Tong Nyee says:

      Hi Treg, Thanks for reporting this. I have raised a bug report on behalf of thi...

      Hi Treg,

      Thanks for reporting this. I have raised a bug report on behalf of this at:

      Feel free to add yourself as a watcher so that you will be notified if there is any update on the issue. Also, you may add comments on the bug report to truly reflect the problem that you are having.

      Cheers,
      Tony

  6. Jul 28, 2008

    Anonymous says:

    When I create a single line code block macro, the underscore character is hidden...

    When I create a single line code block macro, the underscore character is hidden when viewed with Firefox. It appears normally in the Preview and Rich Text editors, but when viewed regularly, it doesn't work.

    Here is _an_ example
    

    Could this be a bug? It may also only appear with the theme we are using – I can't tell until I post.

    1. Jul 30, 2008

      Fennie Ng says:

      Hi, In Confluence Wiki Markup, underscore represents italic . Thus, you should ...

      Hi,

      In Confluence Wiki Markup, underscore represents italic . Thus, you should not see underscore for the words as it will be rendered to italic form. If you would like to display underscore, you can use a slash to escape it :

      Here is \_an\_ example
      

      Regards,
      Fennie

      1. Aug 01, 2008

        Anonymous says:

        But within the window.SyntaxHighlighter.config.clipboardSwf = '/s/1724/13/2/_...

        But within the

         block the underscores should display as is.
        
        Attempting to escape it with a backslash \ causes the backslash to be displayed
        
        
        this is my_code
        
        

        this is my\ code

        1. Aug 01, 2008

          Anonymous says:

          window.SyntaxHighlighter.config.clipboardSwf = '/s/1724/13/2/_/download/resou...
           _ _ _ \_

          My last post didn't work out how I planned - but it looks like the underscores are displayed in this version of the wiki - but not ours

          1. Aug 06, 2008

            Fennie Ng says:

            Hi, Are you using the markup in Rich Text Editor or Wiki Markup Editor? I am us...

            Hi,

            Are you using the markup in Rich Text Editor or Wiki Markup Editor? I am using the following code in Wiki Editor and it looks fine :

            Here is \_an\_ example
            

            Turns out to be :

            Here is _an_ example

            Regards,
            Fennie

    2. Sep 24, 2008

      Anonymous says:

      This appears to be a bug:  if you have an underscore character in a code bl...

      This appears to be a bug:  if you have an underscore character in a code block or a noformat block, it will not appear unless there is another line below it.

      Single line:

      % psql my_database admin
      

      Same code, with a blank line at the end:

      % psql my_database admin
      
      

      In Firefox at least, the first version shows up without the underscore character, though the preview shows it.

      1. Oct 13, 2008

        Ming Giet Chong says:

        Hi, Could you please raise a support ticket at https://support.atlassian.com fo...

        Hi,

        Could you please raise a support ticket at https://support.atlassian.com for further investigation? Do provide the screen shot of the expected result. Thanks.

        Regards,
        MG

  7. Aug 19, 2008

    Christian N says:

    Hi, can anyone tell me the css-selector which allows me to modify the code-bloc...

    Hi,

    can anyone tell me the css-selector which allows me to modify the code-block macro ?
    I want to add a horizontal scrollbar to it. Firefox and IE7 already work fine but not so IE6.

    e.g.
    for this code

    {link-to:dashboard}Dashboard{link-to} >{report-info:content:space|link=true}>{report-on:content:ancestors|separator=" > "}{report-info:content:title|link=true}{report-on}>{report-info:content:title|link=true}
    \\
    

    i get this html

    div class="code panel" style="border-width: 1px;">div class="codeContent panelContent">
    <pre class="code-java">{link-to:dashboard}Dashboard{link-to} &gt;{report-info:content:space|link=<span class="code-keyword">true</span>}&gt;{report-on:content:ancestors|separator=<span class="code-quote">" &gt; "</span>}{report-info:content:title|link=<span class="code-keyword">true</span>}{report-on}&gt;{report-info:content:title|link=<span class="code-keyword">true</span>}
    \\</pre>
    /div>/div>
    

    tia Christian

  8. Feb 04, 2009

    Anonymous says:

    The code attribute does not let you scroll... It just cuts off the code?! Am...

    The code attribute does not let you scroll... It just cuts off the code?!
    Am I supposed to manually put in \n's?

    1. Feb 05, 2009

      Komathi Krishnan [Atlassian] says:

      Hi there, I am afraid this is known bug and you can refer here for the bug repo...

      Hi there,

      I am afraid this is known bug and you can refer here for the bug report. One way to get the scroll bar is to leave and empty line at the end of your code.

      Cheers,
      Komathi

  9. Jul 29, 2009

    Anonymous says:

    Hi I noticed that python comments in the code block have python keywords highlig...

    Hi I noticed that python comments in the code block have python keywords highlighted (when they shouldn't) .

    1. Aug 04, 2009

      Azwandi Mohd Aris [Atlassian] says:

      The Code Block Macro does not support Python out-of-the-box. The default formatt...

      The Code Block Macro does not support Python out-of-the-box. The default formatting will fall to Java, hence some Java keywords in the Python script may be highlighted where it shouldn't. You can use {code:none}...{code} if you want to avoid the highlighting.

  10. Aug 11

    Anonymous says:

    Is there a way to define a line in the code block to be emphasized after syntax-...

    Is there a way to define a line in the code block to be emphasized after syntax-highlighting?

    Often there is a need to point the reader to exactly one line in the source code (e.g. that must inserted or is the clue in the code) but not missing syntax highlighting for the rest. In LaTeX there is a parameter for this in the code macro.

    1. Aug 11

      Sashidaran Jayaraman [Atlassian] says:

      Hi, As far as I know, this feature is not currently available in Confluence. Ho...

      Hi,

      As far as I know, this feature is not currently available in Confluence. However, I have found similar feature requests:

      Please add yourself as a watcher, vote for this feature and add your own comments to this feature request. For further details on how we include new features and improvements, you might want to read this page.

      Anyway, as a quick workaround, you could accomplish this by using a user macro trick. In order to create a new user macro:

      1. Go to Dashboard > Administration > User Macros
      2. Click Create a User Macro.
      3. Name your user macro anything that you want. eg: boldcode
      4. Tick "Macro has a body" and Use unprocessed macro body
      5. Change Output to "Macro generates HTML markup"
      6. In the Template, copy and paste the following:
        #set($point1=$body.toString().indexOf($param0))
        #set($point2=$body.toString().indexOf($param1))
        #set($pointBoldStart=$point1 + 1)
        #set($pointBoldEnd=$point2)
        #set($point2=$point2 + 1)
        #set ($before = $body.toString().substring(0,$point1))
        #set ($bold = $body.toString().substring($pointBoldStart,$pointBoldEnd))
        #set ($after = $body.toString().substring($point2))
        
        <div class="code panel" style="border-width: 1px;">
        <div class="codeContent panelContent">
        <pre class="code-java">
        $before<b>$bold</b>$after
        </pre>
        </div>
        </div>
        

      Once you are done with that, you can simply use this macro anywhere in your page by specifying the following in your wiki markup:

      {boldcode:^|$}
      Your code here
      few more lines here
      and more lines here
      ^this should be bolded$
      this is the last line
      {boldcode}
      

      By specifying this, the line that starts with '^' and ends with '$' will be bolded. However, there is a limitation with this approach. It can be used only one time in a {code} macro

      I hope that would be a good start for you. Feel free to change the code to suit your needs.

      Cheers
      JSashi

  11. Sep 25

    Andy Brook says:

    I see this recent change in the code macro, its looks great but has a significan...

    I see this recent change in the code macro, its looks great but has a significant drawback. Copy/Paste of code macro blocks yields:

    public static void main(String[] args)
    2.{
    3.  System.out.println("Hello World!");
    4.}
    

    Arghh!

    Can we (a) not include line numbers, or (b) add some alternate way of getting the content without line-numbers, saving much editing? Viewing page source just to get an unmangled copy of the code block is possible but a bit of a pain.

    1. Sep 27

      Mark Hrynczak [Atlassian] says:

      You're right. This is an experimental new plugin, and there are still issues to ...

      You're right. This is an experimental new plugin, and there are still issues to iron out. This issue is being tracked at NCODE-46. Feel free to vote/comment/watch the issue.

      1. Jan 29

        Anonymous says:

        That issue is marked as resolved in October 09, yet the problem still exists in ...

        That issue is marked as resolved in October 09, yet the problem still exists in the current 3.1.1-rc1 release, um.....

        1. Jan 31

          Mark Hrynczak [Atlassian] says:

          The New Code Macro is a plugin, which is not shipped as part of the Confluence c...

          The New Code Macro is a plugin, which is not shipped as part of the Confluence code.

          You need to upgrade to the latest version of the plugin to get the fix.

          Cheers,
          Mark

  12. Jan 29

    Andreas Camenzind says:

    How can I have the code block macro opitmized, so that it's shown with different...

    How can I have the code block macro opitmized, so that it's shown with different background colors for each line?

    Mine always look boring :(

    1. Jan 31

      Mark Hrynczak [Atlassian] says:

      You might want to try the New Code Macro which provides an improved UI. Cheers,...

      You might want to try the New Code Macro which provides an improved UI.

      Cheers,
      Mark

Add Comment