2

I usually indent code with tabs, and every time I want to test a snippet before posting it to a SE site I have to reformat it manually before I can post it. Even when I don't use tabs, I need to indent every line with four whitespaces. Furthermore sometimes I have to add a line just to specify which language I'm using in the snippet. Then I have to select everything and copy it to the clipboard before pasting it to the post. How can I quickly and easily format my code for SE sites' posts?

4
  • 1
    What problems do tabs cause, exactly? Inside a code block, SE automatically renders tabs as four spaces, and the web-based editor will correctly indent and unindent code that contains tabs when you press Ctrl+K or click {}. For example, code in this post contains tabs, which get rendered seamlessly as four spaces and look fine. (Since the tabs serve to indicate recipe lines in a makefile, rendering them as spaces produces non-working code, which I had to explain in the post. But that doesn't seem related to what you're talking about.) Commented Apr 23, 2015 at 18:31
  • @EliahKagan You are right. I've never noticed that. Actually I was convinced it was a problem (altough I still can't type tabs within the editor, but that's another problem). I could have swear I had some troubles pasting tabs... Well, I was wrong. Thanks for pointing that out
    – kos
    Commented Apr 23, 2015 at 18:42
  • 1
    Typing the tab character in browser text boxes (on Super User) may help, if you want to be able to enter tabs in the web-based editor. Commented Apr 23, 2015 at 18:59
  • @EliahKagan Thanks, I'm giving it a try
    – kos
    Commented Apr 23, 2015 at 19:35

3 Answers 3

3

SEcopy

SEcopy it's a bash script I wrote for myself which will read the content from a file or from the clipboard and format it for SE sites, copying the formatted content to the clipboard.

Features:

  • Reads the the content from a file or from the clipboard
  • Converts every TAB character to four whitespaces
  • Indents all lines with four whitespaces
  • Optionally adds a <!-- language: <lang-string> --> line
  • Copies the formatted content to the clipboard

Upcoming in 0.03:

  • Formatting of clipboard's content with language line insertion

Sample usage:

  • Bound to a keystroke, can be used to very quickly select, copy, format and paste the formatted content of an opened document to a post (e.g. to select, copy, format and paste the content of an opened gedit document to a post: Ctrl+A + Ctrl+C + <keystroke> + Ctrl+V)

Source code:

#!/bin/bash

if [ "${1}" ]                                
then
    f1="`< "${1}"`"                            
else                                    
    f1="`xclip -selection clipboard -o`"                
fi
f2="`<<< "${f1}" sed "s/\t/    /g"`"                    
f3="`<<< "${f2}" sed "s/^/    /"`"                    
if [ "${2}" ]                                 
then
    f4="`<<< "${f3}" sed "1s/^/<!-- language: lang-${2} -->\n\n/"`"    
else                                    
    f4=${f3}                            
fi
<<< "${f4}" tee 2> /dev/null | xclip -selection clipboard        
echo "SEcopy: Copied to clipboard!"
exit

Installation (Ubuntu):

  1. Install xclip (if not installed): sudo apt-get update && sudo apt-get install xclip
  2. Download SEcopy: https://www.dropbox.com/s/jgdesc7szcg4cfq/secopy-0.02.tar.gz?dl=0
  3. Extract the archive: mkdir ~/Downloads/secopy && tar xf ~/Downloads/secopy-0.02.tar.gz -C ~/Downloads/secopy
  4. Mark install.sh as executable: chmod +x ~/Downloads/secopy/secopy-0.02/install.sh
  5. Install SEcopy: ~/Downloads/seformat/seformat-0.01/install.sh

Removal (Ubuntu):

  1. Mark uninstall.sh as executable: chmod +x ~/Downloads/secopy/secopy-0.02/install.sh
  2. Uninstall SEcopy: ~/Downloads/secopy/secopy-0.02/uninstall.sh
  3. Uninstall xclip (if not needed): sudo apt-get purge xclip
  4. Remove the temporary folder: rm -rf ~/Downloads/secopy
  5. Remove the archive: rm -rf ~/Downloads/secopy-0.02.tar.gz

Usage:

Usage:

secopy [<path_to_source_file> [<lang_string>]]

<path_to_file> = Path to the source file
<lang_string> = Language string defining the source file's language

If no <path_to_source_file> is specified, SEcopy will format the content of the clipboard.
If no <lang_string> is specified, SEcopy will not add a language defining line.

List of all the possible values for <lang_string>

Examples:

secopy
secopy ~/Desktop/MyBashScript.sh
secopy ~/Desktop/MyBashScript.sh bash

Input:

#!/bin/bash

if [ 1 ]
then
        echo This is an example.
done

Output #1: (file read from the clipboard)

    #!/bin/bash

    if [ 1 ]
    then
        echo This is an example.
    done

Output #2: (file read from a file)

    #!/bin/bash

    if [ 1 ]
    then
        echo This is an example.
    done

Output #3: (file read from a file)

<!-- language: lang-bash -->

    #!/bin/bash

    if [ 1 ]
    then
        echo This is an example.
    done

4
  • 2
    Use Github or something if you want to share scripts.
    – muru
    Commented Apr 23, 2015 at 16:30
  • 1
    @muru Yep, probably better. Going to update
    – kos
    Commented Apr 23, 2015 at 16:36
  • Where is the Git repository? ;)
    – A.B.
    Commented May 8, 2015 at 8:30
  • @A.B. With you very soon in 0.03 ;)
    – kos
    Commented May 8, 2015 at 8:34
1

If you're just too lazy to indent a huge number of lines, you can select it and click on the {}-button in the panel above the text field where you enter your post.

Another possibility is to use the HTML tag pair <pre><code> ... </code></pre> for code formatting. It means "preformatted text" and "code" on the finally displayed site it is exactly the same as code blocks indented with 4 spaces, because those Markdown indentations are converted to exactly this HTML tag pair. Note that you should write the tags in the same line as the first (last) line of your code snippet, according to @EliahKagan.

Example (written with indentation to escape the HTML tags and make them visible!):

<pre><code># Here come some code lines
# ...
exit</code></pre>
7
  • Using HTML tags too much defeats the purpose of Markdown, the simplicity and readability that it provides. They should be used sparingly - <kbd> tags being an example.
    – muru
    Commented Apr 23, 2015 at 16:42
  • That is useful, but I really can't stand the TAB thing. I usually indent with tabs, and sometimes i write long scripts. I don't think there's a way to enter / convert TAB characters within the editor, or maybe you know something?
    – kos
    Commented Apr 23, 2015 at 16:44
  • No, I don't. I also have to admit I never had to convert tabs yet...
    – Byte Commander Mod
    Commented Apr 23, 2015 at 17:16
  • For writing explicit HTML for a block of code, I suggest <pre><code> </code></pre> rather than just <pre> </pre>. Indenting with four spaces (which is what Ctrl+K/{} does) causes the site to produce <pre><code> </code></pre> markup. For the site's current CSS on current popular browsers configured in the usual way, the difference appears small to nonexistent. But I think future site changes, custom style sheets (such as for accessibility), other browsers, and some techniques used to read posts in SE data dumps might differently with and without the nested <code> </code> tags. Commented Apr 23, 2015 at 18:19
  • @EliahKagan I edit my answer and included your advice. Thank you!
    – Byte Commander Mod
    Commented Apr 23, 2015 at 18:32
  • Thanks. Bit there's a problem related to an apparent difference in the way SE renders <pre> with and without <code>. The newline between <pre><code> and the code is rendered as a blank line of code. So <pre><code> should be at the start of the fist line of code. And while it doesn't currently seem necessary (in my browser, etc.), it's probably best for </code></pre> to be at the end of the last line as well, rather than on its own line. <pre> </pre> by themselves appear to avoid this requirement, but I wouldn't recommend it for that--I don't think that behavior can be relied on. Commented Apr 23, 2015 at 18:40
  • @EliahKagan Edited again. Do you agree now? :)
    – Byte Commander Mod
    Commented Apr 23, 2015 at 19:06
0

I generally plan my questions in GEdit which can be set to convert tab indents to spaces. Set it to 4 spaces or whatever works for you and then highlight the code you want to share and hit tab. All indented and ready to past into your question or answer. On windows Notepad++ does something very similar.

You must log in to answer this question.

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