Less - Select to clipboard

6

1

In Less (Linux terminal), I can select lines with the mouse and then use Ctrl+Shift+C to copy to the clipboard. This only works within the displayed part of the terminal. How can I select more text? Are there any keyboard shortcuts for selecting text, such as select to the end or beginning or one page, etc.?

stepanian

Posted 2014-08-09T22:14:51.243

Reputation: 163

Answers

5

Generally, no.

Specifically, any such capabilities will depend on multiple factors.

less itself runs inside a terminal environment, and doesn't really know of the clipboard at all.

Your terminal emulator provides clipboard interaction, but probably doesn't have much intelligence in terms of what's currently displayed inside the emulated terminal.

It probably isn't impossible to write a text-mode application that can make use of the X APIs to eventually interact with the clipboard, but it'd likely be a lot of work for very little gain, given that it's often quite easy to enlarge the terminal emulator window, or copy and paste multiple times.

In the specific case of less, consider removing less from the equation entirely. If the file is not too large or otherwise inappropriate to just dump onto the terminal, just cat it to the terminal instead, and use the terminal emulator's scrollback buffer to select the text you want to. With an appropriately sized scrollback buffer, that will allow you to copy all the text you want to copy in one operation.

Since we've found that you really are trying to just get a section of a text file, you can use a command like sed -n M,Np infile > outfile to extract the range of lines M through N from the file named infile. For example, to extract lines 10,000 through 20,000, you'd use sed -n 10000,20000p infile > outfile. How can I extract a range of lines from a text file on unix? has additional suggested solutions for how to solve that problem.

a CVn

Posted 2014-08-09T22:14:51.243

Reputation: 26 553

Thank you so much. That answers my question. The file is very large, that's why I am not using an editor such as gedit. I was hoping that I can extract a section of the file that is smaller and easier to deal with by copying and pasting text into gedit. – stepanian – 2014-08-09T22:33:43.250

@stepanian Well, then why didn't you ask about that instead? sed -n M,Np infile > outfile will give you the range of lines M through N. – a CVn – 2014-08-09T22:39:34.583

Wow! That sounds really useful. How do I view the line numbers within Less? Also what is the "p" after "N"? – stepanian – 2014-08-09T22:42:12.733

@stepanian If you have further questions, please ask them as separate questions, not comments. Generally, comments are meant for suggesting improvements and requesting clarification. Also, state your goal in the question; XY problem type questions such as this one often do not allow for solutions to your actual problem, which in this case was how to extract a portion of a file, having nothing to do with less or the X clipboard.

– a CVn – 2014-08-09T22:44:55.657

2For everyone else's benefit, an -N switch in the Less command displays the line numbers. – stepanian – 2014-08-09T22:49:45.297