How to paste code properly in Confluence 4

When I paste code in Confluence 4, whether in a code block, preformatted text, or anywhere else, the whitespace is mangled and I need to correct it by hand.

Is there a way to correctly paste code so that my whitespace is preserved? Leading whitespace and empty lines in particular are causing the problem.

6 answers

1 accepted

Go to Insert

Insert Wiki Markup

Insert the following

{code}

Your code here...

{code}

You may modify the code first tag to format specific code type like java or xml.

2 votes

The other thing you can do is select Insert > Macro and select the Code macro. You should then be able to paste in that macro and it shouldn't be "mangled". If it is, you might want to raise a bug.

If I do that it adds an extra blank line between every line of code. (And no I'm not going to raise a bug - there are so many formatting bugs I assume it is just infeasible to fix them.)

It's very non-intuitive. Why do we even have a misleading 'Preformatted' styling option? I kept using this and wondering why it won't preserve any code indentation at all.

Now I know I have to use the Code Macro that is buried in the list of macros.

I like the Code Macro, but honestly, it NEEDS to be displayed in an accessible manner. The toolbar needs to have a dedicated button to insert Code with ONE CLICK (not Insert > Other Macros > search "code" > Click Code > Click Insert).

Yes, I know now that I can just type {code to get a code block quickly. But how is a beginner supposed to know that? When I first started, I tried using the most obvious option which is Preformatted text and I was frustrated at how finicky it is.


Here is a way

Use a macro called "no format"
So be where you want to paste in the confluence document. Type a left curly bracket (uppercase the left square bracket), then start typing no format. Select the "no format" option. This creates a box that when you paste single spaced text it stays single spaced in the box.

 

Exactly what was wanted, thanks :-)

This works better than the accepted answer when your input starts with a single leading space. It also doesn't do syntax highlighting.

(update)

from: https://studio.plugins.atlassian.com/browse/NCODE-168

Workaround:
  1. Edit the page
  2. Click on Insert
  3. Choose Wiki Markup
  4. Paste the same code onto the Wiki Markup window in between <tt>code</tt> tags
  5. Save the page and you will get the expected outcome

Here is my original solution:

1. Open Firefox (it doesn't work in Chrome and I haven't tested it in IE)

2. Paste the code into the page (not inside of a code block).

3. Highlight the code you just pasted, click Insert > Other Macro > Code

This will preserve the indentation. You still have to remove the duplicate line breaks.

4. Before you manually delete the line breaks:

  • save the page
  • reopen it for edit
  • manually delete the line breaks

For some reason, if you don't save first, the line breaks reappear.

0 vote
Joseph Clark Atlassian Team Nov 03, 2011

There's already a bug report for this: https://jira.atlassian.com/browse/CONF-23564

Add your vote and comment! :-)

This issue has been resolved in Confluence 4.0.5 which is now available. Thank you for your patience.

How was it resolved? I still get blank lines every other line of code pasted.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,941 views 27 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you