Code Block Macro

This macro is available in Confluence Data Center and Confluence Cloud. Learn about the macros available in Confluence Cloud.

Add a Code Block macro to your page to display code examples with syntax highlighting for more than 80 programming languages

This is great for sharing code snippets such as:

  • sample code
  • terminal commands
  • excerpts from application logs.

On this page:


Screenshot: code sample in the Code Block macro, with syntax highlighting.

Code Block macro configuration options

You can customize the look of your Code Block macro in a number of ways.

The table below outlines each of the parameters available.

Field

Paramaeter name  (used in Confluence storage format or wikimarkup)

Default

Description

Syntax highlighting

language

plain text

Specifies the language (or environment) for syntax highlighting. If you’re an admin, you can configure the default language in the admin area of Confluence.

Title

title

none

Adds a title to the code block. If specified, the title will be displayed in a header row at the top of the code block.

Collapsible

collapse

false

If selected, the code macro's content will be collapsed upon visiting or refreshing the Confluence page. Clicking the expand source link allows you to view this content. If false, the code macro's content is always displayed in full.

Show line numbers

linenumbers

false

If selected, line numbers will be shown to the left of the lines of code.

First line number

firstline

1

When Show line numbers is selected, this value defines the number of the first line of code.

Turn on wrap

-

false

Toggle this button to enable or disable text wrapping within the code block.

This button is also available in the top right corner of the code block macro for anyone reading your page.

Copy as text

-

-

Click on this button to copy your block of code as plain text.

This button is also available in the top right corner of the code block macro for anyone reading your page.

Add the Code Block macro to your page


To add the Code Block macro to a page:

  1. From the editor toolbar, select Insert , then Other Macros.
  2. From the Formatting category, select Code Block.
  3. To choose your preferred language, either start typing its name in the Syntax highlighting field, or click on the dropdown button to scroll through the list.

  4. Use the fields above to customize how the code block should appear on your page. 
  5. Select the Insert button. 
  6. Type or paste your code into the macro placeholder. 
    Note that any white space contained in the placeholder is not manipulated in any way by the Code Block macro. This is to provide the writer with flexibility over code indentation.


You can now publish your page to see the macro in action. Once published, readers of your page will also be able to use options Turn on wrap and Copy as text , displayed in the top right corner of the code block.

Change the macro parameters

Macro parameters are used to change the behavior of a macro. For example, you can directly update the language from the toolbar that appears while you’re editing your code on the page. 

To change the macro parameters:

  1. In the editor, click anywhere within the macro placeholder to display a toolbar.

  2. Select Edit
  3. Update the parameters as required then select Insert.

Administer the Code Block macro

You can configure the Code Block macro to use a specific language and theme by default. You need Confluence Administrator permissions to change the default language .

To set the default language of code blocks in your site:

  1. Go to Administration menu , then General Configuration then Configure Code Macro.
  2. From the Default language dropdown, select your preferred default language.
  3. Select Save.

All new code blocks will use the default language unless you specify otherwise. Existing code blocks will be unchanged. 

Uploaded custom languages were removed in Confluence 9.0. Previously uploaded languages are no longer supported. You can uninstall these from your system.

To remove a user-installed language:

  1. Go to Administration menu then Manage apps.
  2. Go to User-installed Apps and locate the app for your uploaded language.
  3. Look for the following:
    Custom Code Macro Highlighting for...
  4. If you have many apps, you might prefer to search for the namspace:
    com.atlassian.confluence.ext.code.custom.
  5. Choose Uninstall or Disable.

The custom language will no longer appear in the Code Block macro. 

Other ways to add this macro

Add this macro as you type

Type followed by the start of the macro name, to see a list of macros. 

Add this macro using wiki markup

This is useful when you want to add a macro outside the editor, for example as custom content in the sidebar, header or footer of a space.

Macro name: code

Macro body: Accepts plain text.

{code:title=This is my title|linenumbers=true|language=javascript|firstline=0001|collapse=true}
This is my code
{code}
tip/resting Created with Sketch.

Do more with Confluence

Extend Confluence with one of the hundreds of other macros in the Atlassian Marketplace. Some of our most popular include:

Last modified on Oct 3, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.