如何指定“光标下的字”在VIM的命令行? [英] How do I specify "the word under the cursor" on VIM's commandline?
问题描述
我想在VIM中写一个指定光标下的字的命令。例如,让我们把光标放在一个单词上,让它出现两次。例如,如果单词是abc,我想要abcabc,那么我可以键入:
I want to write a command that specifies "the word under the cursor" in VIM. For instance, let's say I have the cursor on a word and I make it appear twice. For instance, if the word is "abc" and I want "abcabc" then I could type:
:s/\(abc\)/\1\1/
但我想能够将光标移动到def并使用相同的命令将其更改为defdef:
But then I'd like to be able to move the cursor to "def" and use the same command to change it to "defdef":
:s/\(def\)/\1\1/
:s/\(*whatever is under the commandline*\)/\1\1
推荐答案
< cword>
是光标下面的字词(:help < cword>
)。
<cword>
is the word under the cursor (:help <cword>
).
对不起,我应该在这个答案更完整。
Sorry, I should have been more complete in this answer.
你可以nmap一个命令,或者这个系列的懒惰的键盘工作:
You can nmap a command to it, or this series of keystrokes for the lazy will work:
b #go to beginning of current word
yw #yank to register
然后,当您输入模式时,您可以按< control-r> 0< enter& code>它会在你的命令中粘贴第0个寄存器的内容。
Then, when you are typing in your pattern you can hit <control-r>0<enter>
which will paste in your command the contents of the 0-th register.
你也可以像这样命令:
:nmap <leader>w :s/\(<c-r>=expand("<cword>")<cr>\)/
这将映射命中'\'和'w'用
Which will map hitting '\' and 'w' at the same time to replace your command line with
:s/\(<currentword>\)/
这篇关于如何指定“光标下的字”在VIM的命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!