How do I specify “the word under the cursor” on VIM's commandline?

前端 未结 8 960
后悔当初
后悔当初 2021-01-30 02:14

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, i

相关标签:
8条回答
  • 2021-01-30 02:35
    ywPx
    

    will do what you describe.

    ywPxw
    

    will also advance the cursor to the next word.

    0 讨论(0)
  • 2021-01-30 02:40

    While in command-line mode, CTRL+R CTRL+W will insert the word under the cursor.

    See the help for c_CTRL-R for a listing of all the other special registers:

    :help c_CTRL-R
    
    0 讨论(0)
  • 2021-01-30 02:42

    You need to escape the backslashes within the mapping. You can also include the substitution string within the mapping.

    :nmap <leader>w :s/\\(<c-r>=expand("<cword>")<cr>\\)/\\1\\1<cr>
    
    0 讨论(0)
  • 2021-01-30 02:43

    <cword> is the word under the cursor (:help <cword>).

    Sorry, I should have been more complete in this answer.

    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
    

    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.

    You can also make a command for this like:

    :nmap <leader>w :s/\(<c-r>=expand("<cword>")<cr>\)/
    

    Which will map hitting '\' and 'w' at the same time to replace your command line with

    :s/\(<currentword>\)/
    
    0 讨论(0)
  • 2021-01-30 02:43
    " count word  (case sensitive)
    nmap <F4> :%s/\(<c-r>=expand("<cword>")<cr>\)//gn<cr>
    
    0 讨论(0)
  • 2021-01-30 02:45
    yiwP
    

    yiw: Yank inner word (the word under the cursor). This command also moves the cursor to the beginning of the word.

    P: Paste before the cursor.

    You can then map the e.g.: < ALT > - D to this command:

    :nmap < ALT >-D yiwP
    
    0 讨论(0)
提交回复
热议问题