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

41,913

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
41,913

Related videos on Youtube

Muhammad Gelbana
Author by

Muhammad Gelbana

I bare witness there is no god but Allah, and Muhammad is his final prophet and messenger I'm a Muslim Egyptian Java developer. I have great passion for Java software development. I don't believe there is something known as a bad question. A question needs to be answered.

Updated on September 18, 2022

Comments

  • Muhammad Gelbana
    Muhammad Gelbana over 1 year

    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.

  • peterph
    peterph over 10 years
    Apart from xclip there is also xsel (and others). You may also want to have a look at this clipboard discussion.
  • Muhammad Gelbana
    Muhammad Gelbana over 10 years
    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.
  • Mubashar
    Mubashar about 3 years
    this is helpful if you don't want to install xclip which may not be available by default.