Linux – Less – Select to clipboard

lesslinux

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.?

Best Answer

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.

Related Question