9

Sometimes there are long commands of the form:

sudo apt-get install package_1 package_2 package_3 package_4 package_5 package_6 package_7 package_8 package_9 package_10 package_11 package_12 package_13 package_14 package_15 package_16 package_17 package_18 package_19 package_20 package_21 package_22 package_23 package_24 package_25 package_26 package_27 package_28 package_29 package_30 package_31 package_32 package_33 package_34 package_35 package_36 package_37 package_38 package_39 package_40 package_41 package_42 package_43 package_44 package_45 package_46 package_47 package_48 package_49 package_50

which I find incredible difficult to read or even copy/paste, as it must be scrolled.

My question is: Would it be preferable to break it into multiple lines with the use of bachslashes (\)?

Isn't this better (compare how long it takes to copy)?

sudo apt-get install package_1 package_2 package_3 package_4 package_5 package_6 \
package_7 package_8 package_9 package_10 package_11 package_12 package_13 \
package_14 package_15 package_16 package_17 package_18 package_19 package_20 \
package_21 package_22 package_23 package_24 package_25 package_26 package_27 \
package_28 package_29 package_30 package_31 package_32 package_33 package_34 \
package_35 package_36 package_37 package_38 package_39 package_40 package_41 \
package_42 package_43 package_44 package_45 package_46 package_47 package_48 \
package_49 package_50
  • 2
    In my opinion, it should depend on what is required in the question, and the type of command. But from a copy/paste point of view, one liners are only slightly easier to copy (Just triple click with a mouse) versus copying a paragraph (double click on the first word and drag to the end of the paragraph while still holding your second click). – Dan Nov 18 '13 at 11:30
  • 4
    Food for thought: Guidelines for code editing, especially horizontal scrolling – brasofilo Nov 18 '13 at 11:32
  • github has this cool little clipboard icon in the lower right corner, which copies the url of the repository to make it work better with command line clients. what would it take to get one of these added to all the code blocks to copy the whole code block with one click? – hildred Nov 25 '13 at 5:15
11

I think this is mostly a matter of personal style, and that the advantages of each approach roughly balance each other out.

To easily copy a long single-line command, you can:

  • Triple-click anywhere in the command. I think this currently works on most or all browsers/platforms.

    If for any reason that doesn't work, or for people who have trouble triple-clicking:

  • Select the first character--or any number of leading characters. Press Shift+End.

This will select from the beginning to the end. Then you can copy it like any text, such as with Ctrl+C.

Given those techniques, I think a single-line command and backslash-broken commands are about equally easy to copy and paste. However, I haven't tested them on all browsers (or all platforms).

The single-line command will be wrapped at the end of each line in a terminal, and tends to be readable in the terminal. A multi-line command broken with backslashes will often be broken up in the terminal as well--unless you make each line very short--and be less readable there as a result.

But the backslash-broken command is probably more readable in your post.

But the backslash-broken command takes more room in your post, and could consequently make the post as a whole less readable. And it is easier for you (or anyone editing the code) to make a mistake.

But if you do make a mistake, it might be easier for people reading your post to see the mistake.

I don't think either way represents an identifiable best practice. It seems to me that you should use whichever way you prefer (or think is clearer in the context of your post). It's probably best to prefer one style or the other within a single post.

  • 3
    I adore your "But..." "But..." "But..." - that describes the way of thoughts so well :-)) – guntbert Nov 18 '13 at 17:32
  • All these easy ways to select the whole line. I've been clicking and dragging since I got here... I might stick to backslashes, tho. It's easier for others to see the entire code – kiri Nov 18 '13 at 20:19
1

Some inexperienced people will just copy the commands line by line, and they won't work after the first backslash. So if you put the whole stuff in one line and they're inexperienced, they will just copy the whole one-liner. And they won't even bother to check what the part of the command they can't see does (that's because they're newbies after all...). On the other side, experienced users won't find any difference.

Just my two cents...

Cheers.

  • 1
    Even if you copy it line by line, it works as long as you keep the backslash at the end. I do agree about being able to check the command first, though. Thanks for your answer. – kiri Nov 20 '13 at 20:41

You must log in to answer this question.

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