18

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.

Joseph R.
  • 39,549
Muhammad Gelbana
  • 1,643
  • 8
  • 20
  • 26

7 Answers7

11

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

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.

Joseph R.
  • 39,549
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.

peterph
  • 30,838
2

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.

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"

Timo
  • 6,332
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.
ks1322
  • 1,656
0

Wrap up

Try with a clipboard like xclip. If that does not work, try the Alacritty shortcuts.

terminal plugin: Tmux + clipboard

Install Tmux and a clipboard like xclip or xsel:

sudo apt-get update -y 
sudo apt-get install -y xclip

and put this in your ~/.tmux.conf to bind y to the xclip copy:

bind -T copy-mode-vi y send -X copy-pipe "xclip -selection c"

Open the Terminal or Alacritty or whatever terminal you have, Tmux commands should now work. With the Tmux shortcut Ctrl+B-->[, you can start marking whatever you want with Space and then Arrows or picture up/down. After you marked all of the needed text, press y that you bound to "copy to xclip". That makes it possible to copy anything, no matter what size.

Alacritty

With the mouse-scrolling selection in the "Alacritty" terminal (or use right click to mark the end), and Ctrl+Shift+C for copy, it worked. This has nothing to do with the editor you use (less, vim or whatever), since the output of the editor is embedded in the terminal text in "Alacritty". You can also zoom out in "Alacritty", then you can copy all of the text without scrolling. You can also try the vim mode: press Ctrl+Shift+Space, you can then move through the console history and mark what you need, if you need all, type the vim command ggVG, afterwards, you can paste it in a text file and then back in terminal, you get out of the vim mode by pressing Ctrl+Shift+Space again.

Story (Alacritty)

I needed to do this as well on an old legacy server that I entered using ssh, to get quite a few pages of log text of a file copied to my system. I wanted this to be done without sending that file around, just with the clipboard.

xclip gives: Sorry, command-not-found has crashed! so that I cannot use less myfile | xclip.

When opening the file in vim that is installed (outdated 2012 version), checking :version, I see -clipboard and -X11, so that I cannot copy to the system clipboard:

enter image description here

I can mark text with the mouse, holding shift, and copy with the right-click menu of the terminal, yet, I cannot scroll while doing so, so that I cannot copy many pages of text in one go.

I then got the hint to use a terminal called "Alacritty" to ssh into the server. And yes, this program has the advantage that it shows the opened text file together with the code that opened it so that you do not have a new vim window. In other words: you keep the vim text in the terminal even if you quit vim again. That again gives you the chance to use terminal tools that you do not have in vim, which in "Alacritty" are also to use the mouse for marking and scrolling at the same time. I can therefore press G as the vim key to get to the end of the file (only then, it is also loaded in the terminal text), then scroll back or type :1, mark item 1 of the first line of the file, hold that marking mouse-click, and scroll down to the end so that everything is marked just with the mouse (copying will not work when using vim keys like ggVG), then press Ctrl+Shift+C and paste it in my system.

You can also zoom out in "Alacritty", then you can copy all of the text without scrolling.