Ubuntu – Copy a long single-line text from a terminal with undesired change-line

terminaltext;Ubuntu

locate ... | less outputs a long path, which is shown as several lines in the terminal, although it is actually a single line.

I copy the path from the terminal by selecting the text with mouse and hitting Ctrl+Shift+C.
When I paste it into a text file, I get unwanted change-line in the text, exactly in the same way that it is shown in the terminal.

But I remember sometimes I can copy a long path output by locate ... | less, without introducing unwanted line-change, and sometimes I can't. I don't realize if I do something differently.

So I wonder how to make sure the problem will not happen? Thanks!

My OS: Ubuntu 12.04.

My terminal: Gnome Terminal 3.4.1.1

Best Answer

Instead of copying what is displayed on the terminal (only what fits on the screen, with), copy the actual text. Use one of the external utilities xsel or xclip (they have mostly the same features, I'll use xsel in this answer) to copy data from or to the X clipboard. To copy to the clipboard, pass the desired content on standard input. When pasting from the clipboard, the content is written to standard output.

In less, use the | command to pipe a bunch of lines through a command. Scroll to the first line you want to act on, type mm to set a mark, scroll to the last line, and type |mxsel -b and press Enter. Two marks are predefined: ^ for the beginning of the file, $ for the end of the file. Thus, to copy the whole file, use <|$xsel -b. To copy a single line, use mm|mxsel -b and Enter.

Remove the -b option to copy to the primary selection instead of the clipboard.

Related Question