开发者

How to copy all hlsearch text to clipboard in Vim

file.txt

abc123
456efg
hi789j

command

:set hlsea开发者_开发知识库rch
/\d\+

I want to copy highlighted text bellow to clipboard (or register):

123
456
789

Just like

egrep -o '[0-9]+' file.txt

Thanks.


One can follow the below procedure.

  1. Empty a register (for instance, "a).

    qaq
    

    or

    :let @a = ''
    
  2. Run the command1

    :g/\d\+/norm!//e^Mv??^M"Ay
    

    If it is necessary to append a new line character after each of the matches, run this command instead:2

    :g/\d\+/norm!//e^Ma^M^[??^Mv$"Ayu
    

    Type ^M as Ctrl+V then Enter (or Ctrl+M), type ^[ as Ctrl+V then Esc (or Ctrl+[). In order not to retype the pattern that just have been used in search, one can press Ctrl+R, / to automatically insert last search pattern.

    Also one can record the command to execute on matched lines (the part following norm!) as a macro. This allows to see the actions immediately on a sample line and to make sure they are correct. Then, the macro can be applied using :global:

    :g/\d\+/norm!@z
    

1 At the top level, the command is a :global executing the Ex command norm!//e^Mv??^M"Ay on each of the lines that match the pattern \d\+. The Ex command begins with the norm! command to execute the Normal mode commands //e^Mv??^M"Ay. These are three commands separated by the carriage return symbol ^M. The first one, //e, looks for the search pattern (which is set to the pattern used in the global command) and put the cursor to the last symbol of the match (because of the flag e, see :help search-offset). Then v command starts Visual mode. The command ?? looks for the last search pattern backwards (and put the cursor to the first character of the match), thus selecting the text that match the last search pattern. The last command, "Ay, yanks the selected text appending it to the a register.

2 The second global command resembles the first one in outline. At each of the matched lines, it moves cursor to the last symbol of the match and inserts newline after that symbol. Then it puts the cursor to the start of the match and selects (in Visual mode) everything up to the end of line (including just inserted newline). Finally, the command appends the selected text to the register, and undoes newline inserting.

3 One can always see the actions recorded in particular macro by examining the contents of the corresponding register using :di z or "zp, for example.


If your text obeys the pattern you posted you can start visual mode blockwise with Ctrl+V and select from 1 in the first line to 9 in the last line. Then you just copy to the + register, which is the system clipboard, by typing "+y.

Edit:

I have tested this new solution for the text:

abc123
456efg
hi789j

Replace all non-digit by nothing with :%s/\D//g and the result will be:

123
456
789

Copy it to the clipboard typing "+y%, then revert the changes with u and you are done.


Use this command to extract all URLs, and append to the end of file:

:let urls = [] | %s/http:[^"]*/\=add(urls, submatch(0))[-1]/g | call setline(line('$')+1, urls)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜