How to copy multi-page text from the terminal into the clipboard

clipboardcopy/pastelessman

I was viewing a man page for a software on my machine and I needed to copy-paste some of that text but the text was much more than one page. I'm aware I can direct the output to a file and use UI based software such as gedit to copy the text I need. But can this be done from the terminal ?

Joseph suggested a command line method (Little bit complicated to me, I'm not yet good with regular expressions on Linux). But I'm wondering if there is a way to do so while I'm still on the terminal, using the mouse.

I hope I get a generic answer, however, I'm using Ubuntu.

Best Answer

I have a couple of imperfect, but maybe useful, ideas.

Method 1 - mousey.

Use the terminal scrollbar.

Details (assuming xterm; adjustments will be required for other terminals.)

  1. Enable the scrollbar if it isn't already. (In xterm, it's in the menu you get with Ctrl+Button2.)
  2. Make sure less is not running with the -c option (I have this option enabled in my LESS environment variable, but it interferes with what we're about to do, so I have to type -c to turn it off.)
  3. Using whatever less commands you like, scroll up or down so the first line you want to copy is visible on the screen.
  4. triple-Button1 your chosen starting line to select it.
  5. Scroll down until the last line you want to copy is visible on the screen. You must use only the space bar or other simple scrolling keys, not a / search - the point here is to get less to send all the lines to the terminal so they can be copied.
  6. Button3 on your chosen end line to extend the selection.

Possible difficulty: your scrollback buffer may not be big enough. That can be changed in xterm with the saveLines resource or -sl command line option, but I don't know of a way to change it in an xterm that's already running.

Method 2 - non-mousey.

Use the | (pipe) command in less to send the text to xclip.

Details:

  1. Using whatever less commands you like, scroll up or down to position the last line you want to copy at the top of the screen. You can skip this step and the next step if you want to copy all the way to the end.
  2. Use the m (mark) command to set a mark at your chosen end position. Marks are a useful feature by themselves, and you should know them already from vi, but just in case you don't: mx sets a mark, where the x can be any letter, and 'x returns you there later.
  3. Now scroll up so the first line you want to copy is at the top of the screen.
  4. Use the pipe command: |x which will bring up a prompt for you to enter an external command. The x should be the same mark letter you used in the m command, or $ for "all the way to the end". Type xclip there.

The simple case of copying the entire man page reduces to g|$xclipEnter.

Warning (i.e. the horrible thing that just happened to me): xclip will be semi-backgrounded, running as part of the less process group. If you try to suspend less, start another job on the same terminal, and paste into it, it will not work. Then when you later foreground the man/less job, suddenly the paste will come through. I think this should be considered an xclip bug...

Related Question