10

Code segments can be formatted by either starting the line with four spaces or by enclosing the code in backticks.

What is the reason for using one over the other?

The reason I ask is that my answer to No Date and Time on 14.10 has been edited by KasiyA to change my backticks into four spaces, and I got feedback from Fabby about using four spaces for code blocks.

But as far as I can tell, it looks exactly the same as it did before.

What is the difference between the two, and when should they be used? I note that backticks are not mentioned on https://askubuntu.com/editing-help.

A secondary but related question is why the same level of formatting is not supported in comments? I spent some considerable time trying to format my comment on What exactly is an "Ubuntu App", as made with the Ubuntu SDK? into a numbered list, and couldn't do it. And then I read the Help page...

  • 1
    Comments are second-class citizens in the SE world. If there's anything you actually need to give proper formatting to, make it a post. – muru Feb 20 '15 at 10:23
24

You know that every code formatting uses fixed width font. But they also draw a grey box around what you wrote. The difference is now in this grey box:

  • Backticks work like a text marker. They put only single characters/words into the grey box. This is why we can use them for inline code formatting.
    But as you can see, the box does not expand in width, when several lines with code formatting are following each other. You see that the box ends with the last character in every single line. You can see a small white space between consecutive lines of code formatted with backticks too.
    Also, the lines are wrapped when they reach the right text area border.
  • In contrast, 4-space-indentation draws a box around the whole line, filling out the complete width of the text area.

    You can not format single words, but only complete lines.
    Also, lines don't wrap when they reach the right border, but a horizontal scrollbar appears.
    If you write many connected lines with this code formatting, also a vertical scrollbar will appear to shorten the post.
    

    There is also automatically some additional space between code blocks and text blocks.

I hope this answer with examples did show you the main differences between both formatting options.

  • Excellent! (and upvoted) – Fabby Feb 20 '15 at 10:03
  • 3
    Boxes give scrolling. +∞ for scrolling! – muru Feb 20 '15 at 10:22
  • Thanks for the clafification, and the addition info from @Fabby, but it leaves me wondering... for a single line of code, as in my linked answer, four spaces and back ticks give exactly the same result. So how did KasiyA know I'd used backticks? – Carl H Feb 20 '15 at 10:37
  • 1
    @CarlH They don't give exactly the same effect. Backtick-marked codes have their effect only upto the width of the text, with four spaces, it extends to the full width of the post. – muru Feb 20 '15 at 10:41
  • 1
    @CarlH Try it yourself, and then you'll go: "Aaaah!!!" – Fabby Feb 20 '15 at 10:42
4

Just an example as I have nothing to add to ByteCommander's excellent answer:

The ls / command gives the following output

bin    dev   initrd.img      lib32       media  proc  sbin  temp  var
boot   etc   initrd.img.old  lib64       mnt    root  srv   tmp   vmlinuz
cdrom  home  lib             lost+found  opt    run   sys   usr   vmlinuz.old

on a windowed terminal and

bin  boot  cdrom  dev  etc  home  initrd.img  initrd.img.old  lib  lib32  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  temp  tmp  usr  var  vmlinuz  vmlinuz.old

on a full screen terminal.

Also if you use a lot of text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellentesque dui ut arcu 
dignissim, eu efficitur metus luctus. Nunc eget purus sit amet enim bibendum tempus ut 
et nisi. Integer sed nisi ac lectus malesuada ullamcorper. Maecenas volutpat, sapien nec 
sollicitudin molestie, justo nisl pretium lorem, non rutrum eros diam eget ipsum. Ut est 
risus, pellentesque ac ante id, consequat iaculis eros. Nam vel consectetur magna. 
Nullam pulvinar hendrerit arcu, vel egestas magna ullamcorper eget. Nulla ultricies non 
elit sit amet maximus. Nam bibendum maximus placerat. Nam malesuada risus eu sapien 
laoreet luctus. Nam mattis interdum lectus id porta. Etiam eu interdum turpis.

Suspendisse imperdiet justo justo, quis malesuada turpis viverra at. Morbi vel cursus 
sapien, sed auctor ligula. Mauris et elementum lacus. Etiam tempus aliquam dictum. 
Suspendisse semper tellus a nibh condimentum varius. Duis ut ex ac felis dictum 
interdum. Donec aliquet, purus eget convallis euismod, velit nunc interdum mi, nec 
rhoncus ipsum tellus et turpis. Etiam sit amet ultrices ante, vitae laoreet neque. Sed 
eleifend lorem nunc, at posuere magna semper eu. Integer porta laoreet nibh sed congue. 
Vivamus quis augue sapien. Pellentesque ut mi imperdiet nisi consectetur malesuada. 
Pellentesque non libero vitae dui bibendum pharetra. In porttitor ut est non consequat. 
Donec gravida a erat sed dapibus. Phasellus varius a turpis auctor volutpat.

Pellentesque quam diam, blandit at dui eget, accumsan tristique lorem. Class aptent 
taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed 
venenatis diam tincidunt nibh rutrum vulputate. Vestibulum viverra lorem a lorem mollis 
iaculis. Quisque risus leo, pulvinar sit amet magna in, semper elementum nisl. Maecenas 
scelerisque rutrum nulla eget finibus. Donec arcu orci, pretium a leo eu, varius 
fermentum enim. Curabitur nec feugiat velit, eu facilisis felis. Suspendisse ut tellus 
sem. Aliquam erat volutpat. Aliquam porta elit nulla, eu scelerisque nibh faucibus sit 
amet. Nam nec augue erat. Donec eget placerat nisl. Fusce efficitur vehicula ipsum sed 
porttitor.

Nulla accumsan sed neque a sodales. Nulla quis dolor sed ligula ornare ultricies. Fusce 
bibendum ultricies massa eget iaculis. Curabitur sodales, mauris eu vulputate fermentum, 
nisl eros pharetra neque, a mattis ligula orci non orci. Vestibulum scelerisque 
pellentesque lectus ut luctus. Donec aliquet mattis lacus. Integer consectetur nisl at 
urna fringilla bibendum.

In nec massa semper, lobortis libero sed, vestibulum purus. Ut vitae fringilla libero, 
vitae placerat enim. Praesent molestie velit elit, id suscipit turpis tempor at. Morbi 
efficitur egestas dolor, sed hendrerit lacus pulvinar nec. Vestibulum rutrum molestie 
varius. Vivamus imperdiet porta ipsum, vel posuere sem suscipit ac. Interdum et 
malesuada fames ac ante ipsum primis in faucibus. Aliquam eu luctus elit, vestibulum 
convallis elit. Cras finibus porttitor tellus in ultrices.

Maecenas lacinia lacinia quam non tempor. Pellentesque convallis tortor eu dui dignissim 
tempor. Ut leo tortor, suscipit a sapien nec, accumsan tristique sapien. Cras vulputate 
porttitor nibh, venenatis tempus arcu dictum sit amet. Fusce sed ante risus. Sed eget 
augue id elit blandit bibendum. Etiam varius libero eu aliquam aliquet. Aliquam non ex 
efficitur sem varius rutrum. Nulla interdum est id mi pellentesque, non euismod libero 
malesuada. Suspendisse bibendum orci ac dui aliquam blandit.

Pellentesque et mi elit. Quisque blandit tellus enim, ut aliquet dolor consectetur eu. 
Vestibulum vehicula rutrum libero, venenatis lobortis ex faucibus eget. In ullamcorper 
lacus quis odio sagittis molestie. Suspendisse efficitur nulla nisi, at euismod ligula 
congue sit amet. Suspendisse ipsum odio, fringilla sed est molestie, pulvinar posuere 
felis. Donec ultricies molestie ante ut mattis. Quisque auctor sollicitudin ornare. 
Pellentesque facilisis ullamcorper malesuada. Vestibulum ex augue, imperdiet eget magna 
ac, placerat tincidunt nisl. Aliquam imperdiet dictum nibh a placerat. Integer ultrices 
velit eget sem vehicula volutpat. Cras efficitur, dolor id ultrices convallis, nunc nisi 
tincidunt est, sed sollicitudin elit enim non sem. In hac habitasse platea dictumst. 
Praesent commodo nisi quis nulla malesuada lacinia.

Nulla sit amet bibendum justo. Curabitur rhoncus eleifend est, nec sodales quam 
condimentum at. Proin dapibus sodales fringilla. Proin enim eros, luctus eget eleifend 
et, sodales nec dui. Praesent malesuada nulla felis, vel gravida lectus ornare eu. Ut 
nisl mi, tempor et neque in, euismod sagittis felis. Cras pellentesque lacus ac elit 
varius, ac imperdiet nulla tempus. Mauris id nunc urna. Phasellus venenatis dui in 
tellus porttitor, et facilisis enim suscipit.

Cras sodales tristique libero ac volutpat. Maecenas sollicitudin sem quis lacinia 
finibus. Mauris ut ex sit amet nunc lobortis facilisis. Nullam dictum magna non lorem 
aliquam blandit. Vestibulum dignissim ligula sed ante porttitor placerat. Morbi pulvinar 
metus dolor, ut pulvinar eros rutrum in. Maecenas vestibulum pharetra nisi, ac volutpat 
tellus porta vitae. Fusce eleifend sem ut ex pharetra, eget tristique tortor semper. Cum 
sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean 
rhoncus lacus faucibus nisl interdum tempus. Sed lacinia dignissim arcu, sed malesuada 
risus tempor et.

Not only do horizontal scroll-bars show up, but vertical ones as well...

  • 2
    Boxes give scrolling. +∞ for scrolling! – muru Feb 20 '15 at 10:23
  • @muru Huh? Me confused again... – Fabby Feb 20 '15 at 10:30
  • 4
    +1 for the +∞ symbol! – Carl H Feb 20 '15 at 10:33
2

Some example here:

  • Inside a list:

    sample code here #used a space to fit in list

    sample code here #used 8 spaces to fit in a list
    
    • or inside sub-list: #used a space to adding a sub-list

      sample code here #used 4 spaces to fit according to sub-list

      sample code here #used 12 spaces to fit
      

      Note that 4 spaces uses in normal code-block 8 spaces to fitting within a list and 12 spaces to fitting within a sub-list and so on(just add 4 extra spaces to adjust your code everywhere you need) toward the beginning of the line. For Backticks only within a lists, just use 4 spaces everywhere you are.

      • or inside a sub-sub-list:

        sample code here

        sample code here #so here used 16 spaces
        

And here's a normal block of code with 4 spaces:

sample code here
next line of code here
another line and
etc ...

Two block of code followed by together:

block 1 of a sample code
with multiline
block 2 of a sample code
with multiline

One Hint

You sometimes need to use the Backticks in your commands like:

echo `$foo` which is written `` echo `$foo` ``.

see what's happened here and good luck:)

0

I have a tendency to use backticks for commands as these are generally fairly short and don't take up even a full line.

I've learned to use code blocks for output as it's often 
easier to read and typically takes up much more real estate.

Although personally I find unnecessary scroll bars annoying 
and format to avoid them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .