Using normal-mode motions in command-line mode in Vim

前端 未结 6 824
旧巷少年郎
旧巷少年郎 2021-01-30 02:07

Is modal editing possible in command-line mode?

Some examples:

  • After writing !ls ~/foo/bar I want to db to delete bar
6条回答
  •  心在旅途
    2021-01-30 02:39

    Custom mappings that replicate Normal mode commands

    You can manually map specific Normal commands to keystrokes in Command-line mode.

    For example the following set of mappings will make Alt+h in Command-line mode go one character left, Alt+k recall the previous Ex command and so on, analogously to hjkl in Normal mode.

    " provide hjkl movements in Command-line mode via the  modifier key
    cnoremap   &cedit. 'h' .''
    cnoremap   &cedit. 'j' .''
    cnoremap   &cedit. 'k' .''
    cnoremap   &cedit. 'l' .''
    

    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:

    " Normal mode command(s) go… --v <-- here
    cnoremap   &cedit. 'b' .''
    cnoremap   &cedit. 'w' .''
    
    " + changes the first word (typically 
    "   the last run Ex :command or the last range, given the case)
    cnoremap   &cedit. '^cw' .''
    

    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).


    Instead of "reinventing the wheel", or as a complement to your own mappings, you can draw on full-featured plugins from other authors → see my other answer.

提交回复
热议问题