2
$\begingroup$

I can't get code samples I post into .my questions to display well. Usually I pre-type it on my editor, then copy and paste into the question box. Someone has to help edit it before it displays with the correct formatting. What do I do?

| |
$\endgroup$
  • 1
    $\begingroup$ Glad you found our Meta site. I don't object to this question, and think it will be good to have a local reference to the issue for our site. However, this type of question properly belongs on Meta Stack Exchange, where all things about the system belong. The tricks and quirks of the site pages, bugs, feature requests and all that are discussed there. As a double bonus, going there first may answer your question before you even ask it, and if not, then you can earn reputation there. The individual meta sites are more about how to make that site fit the community needs, and no reputation is earned. $\endgroup$ – Gypsy Spellweaver Oct 27 '19 at 1:50
2
$\begingroup$

Not counting advanced options, there are three ways to show code in your questions and answers.


Number 1:

For short pieces which don't need to be on their own line, such as when talking about a command in text, you can surround the text with backticks, `.


Number 2:

To make a block of code display in its own box, you can indent everything by four spaces.

Number 2:

To make a block of code display in its own box,
you can indent everything by four spaces.

Especially easy when you use an editor to compose you posts first. Highlight the block and indent it by 4 spaces (probably one tab). This one is not so easy when using the on-page text box, since the browser naturally uses TAB to move between form fields and pressing the space bar four times, or more for indented code, on each line can get rather cumbersome.


Number 3:

Visually the same as above, you can place a "code fence", three backticks, above and below the code block. Using that style, entering

```  
gypsy@suse-office:~> sudo l -l /disks  
total 32  
drwxr-xr-x   1 gypsy gypsy   162 Sep  2 16:55 ./  
drwxr-xr-x   1 root  root    336 Oct 22 06:28 ../  
drwxr-xr-x. 18 gypsy gypsy  4096 Sep 24 06:58 data_lib/  
-rw-r--r--   1 gypsy gypsy  3509 Sep  2 16:55 dev_list  
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_1/  
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_2/  
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_3/  
-rw-r--r--   1 gypsy gypsy 11993 Dec 10  2018 movie-list  
drwxr-xr-x. 18 gypsy gypsy  4096 Jun 25 01:52 red_2t/  
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 red_4t/  
drwxr-xr-x   1 gypsy gypsy     0 Dec 28  2018 T1-08/  
drwxr-xr-x. 32 gypsy gypsy  4096 Sep 14 04:14 T1-14/  
drwxr-xr-x   1 root  root      0 Nov 24  2018 T1-16/  
drwxrwxrwx   1 gypsy gypsy  4096 Oct 10 20:37 windows/  
gypsy@suse-office:~>  
```

Gives:

gypsy@suse-office:~> sudo l -l /disks
total 32
drwxr-xr-x   1 gypsy gypsy   162 Sep  2 16:55 ./
drwxr-xr-x   1 root  root    336 Oct 22 06:28 ../
drwxr-xr-x. 18 gypsy gypsy  4096 Sep 24 06:58 data_lib/
-rw-r--r--   1 gypsy gypsy  3509 Sep  2 16:55 dev_list
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_1/
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_2/
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 flash_3/
-rw-r--r--   1 gypsy gypsy 11993 Dec 10  2018 movie-list
drwxr-xr-x. 18 gypsy gypsy  4096 Jun 25 01:52 red_2t/
drwxr-xr-x   1 gypsy gypsy     0 Nov 24  2018 red_4t/
drwxr-xr-x   1 gypsy gypsy     0 Dec 28  2018 T1-08/
drwxr-xr-x. 32 gypsy gypsy  4096 Sep 14 04:14 T1-14/
drwxr-xr-x   1 root  root      0 Nov 24  2018 T1-16/
drwxrwxrwx   1 gypsy gypsy  4096 Oct 10 20:37 windows/
gypsy@suse-office:~>

Of course, both look the same here, and that is the point. To see exactly what was typed, click on the edit link below this answer and you can view the posting as plain text.


Extra note:

If you need a place to experiment, you can try the Formatting Sandbox on Meta Stack Exchange.

For more information about the plethora of options, check out What is syntax highlighting and how does it work?. On that page search, using your browser's search (probably CTRL+F) and look for How do I use syntax highlighting?. The same post also lists the supported syntaxes in the section What language codes are currently available on Stack Exchange.

| |
$\endgroup$
  • 1
    $\begingroup$ After all this time, I still didn't know about that third method. You learn something new every day 😁 $\endgroup$ – Ben I. Oct 26 '19 at 19:14
  • 1
    $\begingroup$ @BenI. An advantage of the last is you can specify the language, "```java" for example, to make syntax highlighting work better. $\endgroup$ – Gypsy Spellweaver Oct 26 '19 at 19:33
  • $\begingroup$ That got me educated. $\endgroup$ – Mallam Awal Oct 26 '19 at 21:54

You must log in to answer this question.

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