命令行模式下是否支持模态编辑?
以下是一些示例:
- 在输入
!ls ~/foo/bar
后,我希望用db
来删除bar - 执行上述命令后,现在我想将
ls
更改为mv
并跳回$
默认情况下,当您在Vim命令行上时,您可以按Control + f
(或者查看set cedit
)来打开命令行窗口,您可以使用正常模式Vim编辑键编辑命令。
Enter
将运行命令,而Control + c
将返回标准命令行。
因此,在您的特定示例中,您可以在Vim命令行上按Control + f
,然后输入db
,这样它就会执行您想要的操作。
当我必须执行更复杂的编辑命令时,我使用上述方法,因为我对Vim编辑键比其他替代方案更熟悉。我也发现正常模式vim键更加强大。
有关更多信息,请参见:help c_ctrl-f
。
<ctrl-w>
删除一个单词。q:
转到命令历史记录(可使用 vim 命令进行编辑)。:help cmdline-editing
和 :help cmdline-window
。<Esc> abandon command-line without executing it
CTRL-C abandon command-line without executing it
CTRL-B cursor to begin 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 <Backspace>)
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)
<Up> recall previous command-line from history (that matches pattern in front of the cursor)
<Down> recall next command-line from history (that matches pattern in front of the cursor)
<S-Up> recall previous command-line from history
<S-Down> recall next command-line from history
<PageUp> recall previous command-line from history
<PageDown> recall next command-line from history
<S-Left> cursor one word left
<C-Left> cursor one word left
<S-Right> cursor one word right
<C-Right> cursor one word right
<LeftMouse> cursor at mouse click
参考资料: :help ex-edit-index
(很抱歉与旧答案有所重叠。希望这份完整、结构化的概述仍然有用。)
°截至2014年 - 自那时以来可能已添加了新的键盘快捷键
在Vim中搜索:help cmdline-editing
。
它会给出一些在命令行模式下使用的快捷方式列表。
以下是与您当前问题相关的摘录:
CTRL-B or <Home> *c_CTRL-B* *c_<Home>*
cursor to beginning of command-line
CTRL-E or <End> *c_CTRL-E* *c_<End>*
cursor to end of command-line
<S-Left> or <C-Left> *c_<C-Left>*
cursor one WORD left
*c_<S-Right>*
<S-Right> or <C-Right> *c_<C-Right>*
cursor one WORD right
q:
,它允许您在不同模式下编辑先前输入的命令。emacscommandline 可以使命令行模式更像Unix命令行,增加了类似Bash shell中的Emacs风格映射。其中一些映射只是将现有的vim命令映射过来,但其余的则实现了本地不可用的功能。
conomode.vim 在命令行顶部实现了一种"正常模式(Cmdline-Normal mode)",目的类似于命令行窗口(q:),但可以在原地进行导航和编辑,当然命令行窗口更为强大。
Conomode并不替代Vim本身的emacs风格命令行编辑,它只添加了一个按键: CTRL-O(见下文)。
- 使用c_进入(在命令行模式下按CTRL-O)
- 模式指示器是一个冒号 ":", 随着光标移动而移动,隐藏其下面的字符
- 使用I、i、a、A、:或任何未映射的键(然后执行或插入它本身),或等待60秒,退出到命令行模式
- 使用Esc退出到正常模式
(诚然,我不明白这个插件的意义,因为你只需按下Ctrl+F即可弹出命令行窗口,进入其Cono模式需要按Ctrl+O。)
可能还有其他插件可用。 → https://www.google.com/webhp?q=command-line+editing+plugin+vim
除了使用完整的插件之外,或者作为补充,您可以定义自己的映射。→ 参见我的另一个答案。
您可以在命令行模式下手动将特定的Normal命令映射到按键。
例如,以下一组映射将使命令行模式下的Alt+h向左移动一个字符,Alt+k调用上一个Ex命令等等,类似于Normal模式中的hjkl。
" provide hjkl movements in Command-line mode via the <Alt> modifier key
cnoremap <expr> <A-h> &cedit. 'h' .'<C-c>'
cnoremap <expr> <A-j> &cedit. 'j' .'<C-c>'
cnoremap <expr> <A-k> &cedit. 'k' .'<C-c>'
cnoremap <expr> <A-l> &cedit. 'l' .'<C-c>'
由于 Alt 修改键默认情况下未映射到任何重要功能,因此您可以通过相同的方式将其他(或全部)功能从正常模式转移到插入模式。例如: 使用 Alt+b 将光标移动到当前单词的开头:
" Normal mode command(s) go… --v <-- here
cnoremap <expr> <A-b> &cedit. 'b' .'<C-c>'
cnoremap <expr> <A-w> &cedit. 'w' .'<C-c>'
" make <Alt>+<i> change the first word (typically
" the last run Ex :command or the last range, given the case)
cnoremap <expr> <A-i> &cedit. '^cw' .'<C-c>'
您需要将该代码复制到您的vimrc文件中,以便每次启动vim时都加载它(您可以在正常模式下开始键入:new $myvimrc
来打开它)。
q:
将打开正常模式下的窗口。还有q/
和q?
变体。 - D. Ben Knoble