简体   繁体   English

从vim的每一行中取出第一个单词

[英]Yank the first word from each line in vim

I'm using vim and I'd like to copy the first word for a range of consecutive lines. 我正在使用vim,我想复制一系列连续行的第一个单词。 The only thing that I could think of is :4,32yw (for lines 4-32), but it didn't work. 我唯一能想到的是:4,32yw (对于4-32行),但它没有用。 Any help is appreciated in advance, thanks! 任何帮助都提前感谢,谢谢!

You can do it, but it might be a little bit more complicated than using a simple command as you did. 你可以做到,但它可能比你使用简单的命令要复杂一点。

What you tried to run, is normal mode commands as execute commands. 您尝试运行的是正常模式命令作为执行命令。 That won't work as you did it. 这不会像你那样工作。 Commands in the normal mode (like pressing yw ) won't work es ex-commands. 正常模式下的命令(如按yw )将无法运行es ex-commands。


To run the normal mode commands in the ex-command mode, you should use the normal command (for more information, you can see :help normal ). 要在ex-command模式下运行正常模式命令,应使用normal命令(有关更多信息,请参阅:help normal )。 However, even if you would change you command to use the normal mode command (something like :4,32 normal! yw ) it would still not work, since this would run for every line, each time running over the previous yanked value. 但是,即使您将命令更改为使用普通模式命令(例如:4,32 normal! yw ),它仍然不起作用,因为这将针对每一行运行,每次都在上一个被拉动的值上运行。

You can do the desired action, using a vim register that would append the word in each loop (for more info about registers you can read :help registers ). 您可以使用vim寄存器执行所需的操作,该寄存器将在每个循环中附加该字(有关您可以读取的寄存器的更多信息:help registers )。


To do it, you should change the command to be something like :3,32 normal! ^"Ayw 要做到这一点,你应该将命令更改为:3,32 normal! ^"Ayw :3,32 normal! ^"Ayw . :3,32 normal! ^"Ayw

To break down this command: 要分解此命令:

  • : - enter ex-command mode. : - 进入ex-command模式。
  • 3,32 - The range to run the command over. 3,32 - 运行命令的范围。
  • normal! - The actual command to run. - 要运行的实际命令。 Run it without any custom mapping, to avoid mappings that run over the wanted action. 在没有任何自定义映射的情况下运行它,以避免在所需操作上运行的映射。
  • ^ - Go to the start of the line. ^ - 转到该行的开头。
  • "A - Run the yank into the a register, appending the data to the previously saved data in it. "A - 将yank运行到一个寄存器中,将数据附加到之前保存的数据中。
  • yw - Yank the current word. yw - Yank当前的单词。

Later, to print the copied values, you should use "ap (in the normal mode) at the desired location. 稍后,要打印复制的值,您应该在所需位置使用"ap (在正常模式下)”。

Drawbacks 缺点

Note that the command inserts the new word into the registers, ignoring the first value of the register. 请注意,该命令将新字插入寄存器,忽略寄存器的第一个值。 It means that if the register wasn't empty once you run this command, all the words would be appended to the current value of the register. 这意味着如果在运行此命令后寄存器不为空,则所有字都将附加到寄存器的当前值。

To clear the previous value of the register, you can run the command: 要清除寄存器的先前值,可以运行以下命令:

:let @a=''

before running you yank command. 在运行你的yank命令之前。

Automation 自动化

In case you want to do it many times, you might want to use a simple function that would do it all for you, instead make you run both commands every time. 如果你想多次这样做,你可能想要使用一个简单的函数来完成所有这些操作,而不是让你每次都运行这两个命令。


A simple function that would do this: 一个简单的功能,可以做到这一点:

function! CopyFirst(register) range
    execute "let @" . a:register. "=''"
    execute a:firstline . "," . a:lastline . "normal! ^\"" . toupper(a:register) . "yw"
endfunction

Usage: 用法:

:3, 32 call Copyfirst('a')
"ap

This function uses a function range (more info of this feature in :help func-range ): 此函数使用函数范围(有关此功能的更多信息:help func-range ):

" Get the first word of the line for a given range of lines
function! GetFirstWord() range
    let @a=""
    execute a:firstline.",".a:lastline."g/.*/y A"
    echo split(substitute(@a," *\\(\\w\\+\\).\\{-}\\n","\\1 ","g"))
endfunction

The first line cleans register a . 第一行清理注册a Second line yanks the lines inside the range into register a . 第二行将范围内的线条拉入寄存器a The last line prints a List with the first words of each line. 最后一行打印一个List,其中包含每行的第一个单词。 Sample input file: 示例输入文件:

some sample
   text abc
   123 456
function
yy xx

Sample calling and output: 样本调用和输出:

:2,4call GetFirstWord()
['text', '123', 'function']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM