delete word after or around cursor in VIM

前端 未结 16 609
一向
一向 2020-12-07 06:59

I\'m now switching to VIM from TextMate. I found ^+W in INSERT mode very useful. However, I\'d like to delete not only the word before cursor, but

相关标签:
16条回答
  • 2020-12-07 07:15

    The accepted answer fails when trying to repeat deleting words, try this solution instead:

    " delete current word, insert and normal modes
    inoremap <C-BS> <C-O>b<C-O>dw
    noremap <C-BS> bdw
    

    It maps CTRL-BackSpace, also working in normal mode.

    0 讨论(0)
  • 2020-12-07 07:15

    I'd like to delete not only the word before cursor, but the word after or around cursor as well.

    In that case the solution proposed by OP (i.e. using <c-w>) can be combined with the accepted answer to give:

    inoremap <c-d> <c-o>daw<c-w>
    

    alternatively (shorter solution):

    inoremap <c-d> <c-o>vawobd
    

    Sample input:

    word1 word2 word3 word4
                   ^------------- Cursor location
    

    Output:

    word1 word4
    
    0 讨论(0)
  • 2020-12-07 07:16

    Insert mode has no such command, unfortunately. In VIM, to delete the whole word under the cursor you may type viwd in NORMAL mode. Which means "Visual-block Inner Word Delete". Use an upper case W to include punctuation.

    0 讨论(0)
  • 2020-12-07 07:18

    What you should do is create an imap of a certain key to a series of commands, in this case the commands will drop you into normal mode, delete the current word and then put you back in insert:

    :imap <C-d> <C-[>diwi
    
    0 讨论(0)
  • 2020-12-07 07:19

    Normal mode:

    daw : delete the word under the cursor    
    caw : delete the word under the cursor and put you in insert mode 
    
    0 讨论(0)
  • 2020-12-07 07:20

    In old vi, b moves the cursor to the beginning of the word before cursor, w moves the cursor to the beginning of the word after cursor, e moves cursor at the end of the word after cursor and dw deletes from the cursor to the end of the word.

    If you type wbdw, you delete the word around cursor, even if the cursor is at the beginning or at the end of the word. Note that whitespaces after a word are considerer to be part of the word to be deleted.

    0 讨论(0)
提交回复
热议问题