Traversing text in Insert mode

后端 未结 12 1296
逝去的感伤
逝去的感伤 2020-12-02 03:00

While in Insert Mode in Vim, is there any way to traverse the text moving some characters forward and backward other than using the arrow keys?

If I

12条回答
  •  情话喂你
    2020-12-02 04:03

    Insert mode

    Movement

    hjkl

    Notwithstanding what Pavel Shved said - that it is probably more advisable to get used to Escaping Insert mode - here is an example set of mappings for quick navigation within Insert mode:

    " provide hjkl movements in Insert mode via the  modifier key
    inoremap  h
    inoremap  j
    inoremap  k
    inoremap  l
    

    This will make Alt+h in Insert mode go one character left, Alt+j down and so on, analogously to hjkl in Normal mode.

    You have to copy that code into your vimrc file to have it loaded every time you start vim (you can open that by typing :new $myvimrc starting in Normal mode).


    Any Normal mode movements

    Since the Alt modifier key is not mapped (to something important) by default, you can in the same fashion pull other (or all) functionality from Normal mode to Insert mode. E.g.:
    Moving to the beginning of the current word with Alt+b:

    inoremap  b
    inoremap  w
    


    (Other uses of Alt in Insert mode)

    It is worth mentioning that there may be better uses for the Alt key than replicating Normal mode behaviour: e.g. here are mappings for copying from an adjacent line the portion from the current column till the end of the line:

    " Insert the rest of the line below the cursor.
    " Mnemonic: Elevate characters from below line
    inoremap  
        \
        \jl
            \y$
        \hk
            \p
            \a
    " Insert the rest of the line above the cursor.
    " Mnemonic:  Y depicts a funnel, through which the above line's characters pour onto the current line.
    inoremap  
        \
        \kl
            \y$
        \hj
            \p
            \a
    

    (I used \ line continuation and indentation to increase clarity - the commands are interpreted as if written on a single line.)


    Built-in hotkeys for editing

    CTRL-H   delete the character  in front of the cursor (same as )
    CTRL-W   delete the word       in front of the cursor
    CTRL-U   delete all characters in front of the cursor (influenced by the 'backspace' option)
    

    (There are no notable built-in hotkeys for movement in Insert mode.)

    Reference: :help insert-index


    Command-line mode

    This set of mappings makes the upper Alt+hjkl movements available in the Command-line:

    " provide hjkl movements in Command-line mode via the  modifier key
    cnoremap  
    cnoremap  
    cnoremap  
    cnoremap  
    

    Alternatively, these mappings add the movements both to Insert mode and Command-line mode in one go:

    " provide hjkl movements in Insert mode and Command-line mode via the  modifier key
    noremap!  
    noremap!  
    noremap!  
    noremap!  
    


    The mapping commands for pulling Normal mode commands to Command-line mode look a bit different from the Insert mode mapping commands (because Command-line mode lacks Insert mode's Ctrl+O):

    " Normal mode command(s) go… --v <-- here
    cnoremap   &cedit. 'h' .''
    cnoremap   &cedit. 'j' .''
    cnoremap   &cedit. 'k' .''
    cnoremap   &cedit. 'l' .''
    
    cnoremap   &cedit. 'b' .''
    cnoremap   &cedit. 'w' .''
    


    Built-in hotkeys for movement and editing

    CTRL-B       cursor to beginning of command-line
    CTRL-E       cursor to end       of command-line
    
    CTRL-F       opens the command-line window (unless a different key is specified in 'cedit')
    
    CTRL-H       delete the character  in front of the cursor (same as )
    CTRL-W       delete the word       in front of the cursor
    CTRL-U       delete all characters in front of the cursor
    
    CTRL-P       recall previous command-line from history (that matches pattern in front of the cursor)
    CTRL-N       recall next     command-line from history (that matches pattern in front of the cursor)
             recall previous command-line from history (that matches pattern in front of the cursor)
           recall next     command-line from history (that matches pattern in front of the cursor)
           recall previous command-line from history
         recall next     command-line from history
         recall previous command-line from history
       recall next     command-line from history
    
         cursor one word left
         cursor one word left
        cursor one word right
        cursor one word right
    
      cursor at mouse click
    

    Reference: :help ex-edit-index

提交回复
热议问题