Copy specific line from less

邮差的信 提交于 2019-12-04 04:29:12

tl;dr, use m and |

Example:

  1. 7g
  2. mx
  3. 6g
  4. |x
    • xclip (Linux) or pbcopy (macOS), to copy to clipboard.
    • cat > file, to save to file, or cat >> file for append mode.

Within the man page of less, we would get:

   less - opposite of more    
    

The key things to learn are just two less commands: m (mark), and | (pipe).

Command m (mark)

Followed by any lowercase letter, marks the current position with that letter.

The marker we used above is x, as in step 2, it marked line 7 with x.

Command | (pipe)

| <m> shell-command

<m> represents any mark letter. Pipes a section of the input file to the given shell command.
The section of the file to be piped is between the first line on the current screen and the position marked by the letter.
<m> may also be ^ or $ to indicate beginning or end of file respectively. If <m> is . or <newline>, the current screen is piped.

Using |xpbcopy, we pipe the line-range [7, 6] into pbcopy, as line 6 is currently the first line on the screen, and line 7 is the one we marked as x, and pbcopy is the command to put text into the macOS clipboard.

Alternative, use xclip on Linux, or even dd of=/path/to/file to save as a file.

Note

Actually the text range is boundary inclusive, so both the beginning and the ending lines of the range, or at least 2 lines are copied.

We marked the range in a backward way, namely from bottom to top, otherwise, less might behave awkwardly, and throw the whole screen through the pipe.

I think I found the solution: it is using tmux. Tmux provides it's own clipboard ( correct me if I am wrong ). From tmux I can enter the copy-mode wherever I am ( in MAN pages, less, console output ) and let me to copy the content.

Emilien

Short answer: Ctrl+C and Ctrl+V are associated with other actions. For instance Ctrl+C sends an interrupt signal to the foreground process. Usually you need to use Ctrl+Shift+C and Ctrl+Shift+V in order to copy and paste from a terminal.

Long answer: This very good thread from superuser.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!