Update 2018-06-26: This relates to the current version of Confluence in 2011. Please stop answering this question in relation to new versions (2017+)
---
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.
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.
Perfect, thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
doesnt keep tab indents..
noone can easily copy and paste the code since it adds line numbers that get highlighted
and also cant be used for inline code...
I cant find a viable option for code so far
Our dev team is moving to confluence and seems pretty bad for code examples.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here's the original code in my IDE (Eclipse):
In your Confluence doc, type "{code" and choose the Code Block macro:
Paste the code into the Code Block, publish, and Voila!:
As you can see it keeps tab indents and all formatting. You also have the option of including line numbers.
You can also install and use Better Code Block, which supports more languages and has better highlighting, but at the expense of being slightly slower and not supporting line numbers.
This is with the online ("Cloud"?) version of Confluence. Hope this helps - Good luck!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hmmm thats really weird...
Before I was seeing line numbers and tab indenting wasn't pasting in. Today it seems to work exactly as you said I dont have line numbers or formatting problems.
The other day our lead dev was experiencing the same thing as me where tab indents were removed when pasting in code but only some times, was intermittent issue for him and happening all the time for me.
But problem seems to have resolved itself. 🤔
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
By any chance are you using any kind of remote desktop type software? I've seen similar issues when I've got a NoMachine session open to my work computer (running Linux for what it's worth). Sometimes if I copy from the work computer and paste into a Confluence page open on my home (Windows) machine it will lose the formatting.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I didn't copy from an RDC but I probably had one open int he background somewhere. I use windows RDP and Anydesk and Teamviewer for different systems so perhaps it was one of those modifying the clipboard as soon as I copied it.
If it happens again I'l make a note of witch one(s) i have open and see if closing them fixes the problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Exactly what was wanted, thanks :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This works better than the accepted answer when your input starts with a single leading space. It also doesn't do syntax highlighting.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
(update)
from: https://studio.plugins.atlassian.com/browse/NCODE-168
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:
For some reason, if you don't save first, the line breaks reappear.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For HTML try these steps:
1. Copy and paste your code into this link:
https://www.freeformatter.com/html-validator.html
2. VALIDATE HTML: Click.
3. Insert your cursor into the HTML (ignore the markup for brackets...) and copy all of the code
4. Open your Confluence page.
5. {code...: Paste the code into the code block and you will have good results.
I use similar for VB, C, etc, but agree: sometimes I have extra lines and cleanup.
Am I seeing that a bug was reported on this in 2011 and its now 2018 and the issue is not resolved??
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This issue has been resolved in Confluence 4.0.5 which is now available. Thank you for your patience.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How was it resolved? I still get blank lines every other line of code pasted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still not resolved 2 years later, every line has an extra blank line
case HID_POV:
if(sval >= _StickElements[i].ref->min && sval <= _StickElements[i].ref->max)
{
Becomes
case HID_POV:
if(sval >= _StickElements[i].ref->min && sval <= _StickElements[i].ref->max)
{
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There's already a bug report for this: https://jira.atlassian.com/browse/CONF-23564
Add your vote and comment! :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.