13

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.

9

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

  • 2
    Mac users can use pbcopy if they don't have xclip installed. – Mike Hedman Jan 23 '18 at 21:35
3

Terminal multiplexer like tmux or screen usually have a functionality like this (i.e. to save part of the scrollback buffer to file or pipe it to some command) and using them is usually a good idea anyway, so you might want to have a look at one of those.

2

As outlined on Nixcraft:

  1. Get xclip:

    sudo apt-get install xclip 
    

    or

    yum install xclip
    

    on an RPM-based system.

  2. man man | your_magic_here | xclip -selection clipboard
  3. Ctrl+V to your heart's content. You can remove -selection clipboard to choose pasting by middle mouse click instead (called primary selection).

Note that your_magic_here is a placeholder for a text filter (sed/awk/perl/...) that selects the desired portion of the man page according to your specific use case, which you didn't describe so I'm afraid I can't be specific with an example.

  • Apart from xclip there is also xsel (and others). You may also want to have a look at this clipboard discussion. – peterph Jan 19 '14 at 0:38
  • Is there a way to do so using the mouse ? So that when the mouse reaches the end of the page, the page would scroll. – Muhammad Gelbana Jan 19 '14 at 6:46
0

It depends on your terminal program, but most terminal programs do not have the functionality to save selected text.

LXTerminal, uxterm, Konsole and GNOME Terminal all seem to lack this 'feature', but there might be others that do have that. Look at your terminal program's menu structure and there might be something like "Save selected"

0

Assuming you use gnome-terminal and less as a pager for man you can:

  • Run less for man with -X key to disable alternate screen, for example: LESS=-X man less

This will allow to append output of man to terminal emulator history buffer. See this answer for more details https://unix.stackexchange.com/a/38638/87918.

  • Scroll to the last page of text you want to copy. (All scrolled text will be saved in history buffer while scrolling)
  • Select text with mouse in gnome-terminal beginning from last page to first, dragging mouse to the up edge of window. Detailed process of text selection is described here:

This doesn't work in Gnome Terminal. Instead what you have to do is start the selection with the left mouse button and while making it, drag the mouse cursor to the edge of the window (or outside the window). G-T will scroll things for you, extending the selection in the process. G-T's scrolling is sufficiently rapid that this is a reasonably convenient and intuitive process, arguably better than xterm's.

  • Now you have text selected (whatever pages you want) which you can copy & paste wherever you want.
0

Use the | (pipe) command in less with marks as has been answered by Wumpus Q. Wumbley in his second option, but redirecting using tee to append to an existing file already there for the purpose:

|tee >> ~/helpaggregator.txt

I suggest an existing file as one usually needs to consult when learning new commands and one likes to keep them consolidated. Moreover, tab completion comes in handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.