简体   繁体   English

在 Vim 的命令行模式下使用普通模式运动

[英]Using normal-mode motions in command-line mode in Vim

Is modal editing possible in command-line mode?在命令行模式下可以进行模态编辑吗?

Some examples:一些例子:

  • After writing !ls ~/foo/bar I want to db to delete bar写入!ls ~/foo/bar我想db删除bar
  • I executed the above command and now I want to change ls to mv and jump back to $我执行了上面的命令,现在我想将ls更改为mv并跳回 $

By default you can press Control + f (or otherwise see set cedit ) when on the Vim command-line, which opens the command-line window where you can edit the command using normal-mode Vim editing keys.默认情况下,您可以在 Vim 命令行上按Control + f (或以其他方式查看set cedit ),这会打开命令行窗口,您可以在其中使用普通模式 Vim 编辑键编辑命令。 Enter will run the command or Control + c will return you to the standard command-line. Enter将运行命令或Control + c将返回到标准命令行。

So in your specific example, you could press Control + f on the Vim command-line then db and it would do what you want.因此,在您的具体示例中,您可以在 Vim 命令行上按Control + f然后按db ,它会执行您想要的操作。

When I have to do more sophisticated editing commands I use the above approach because I'm more familiar with Vim editing keys than I am with the alternatives.当我必须执行更复杂的编辑命令时,我会使用上述方法,因为我对 Vim 编辑键的熟悉程度比对替代方法的熟悉程度更高。 I also find the normal-mode vim keys more powerful.我还发现普通模式 vim 键更强大。

See :help c_ctrl-f for more information.有关更多信息,请参阅:help c_ctrl-f

in vim 's command line mode: <ctrl-w> deletes a wordvim的命令行模式下: <ctrl-w>删除一个单词

in normal mode : q: goes to the command history (which can be edited with vim commands)正常模式下q:转到命令历史记录(可以用 vim 命令编辑)

see :help cmdline-editing and :help cmdline-window for more commands.有关更多命令,请参阅:help cmdline-editing:help cmdline-window

Built-in hotkeys for movement and editing in Command-line mode用于在命令行模式下移动和编辑的内置热键

<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

Reference: :help ex-edit-index参考:: :help ex-edit-index

(Sorry for the overlap with older answers. Hopefully this complete°, structured overview is useful nevertheless.) (抱歉与旧答案重叠。希望这个完整的°,结构化概述仍然有用。)
° as of 2014 – new keyboard shortcuts may have been added since then ° 截至 2014 年 - 从那时起可能添加了新的键盘快捷键

Search for :help cmdline-editing in Vim.在 Vim 中搜索:help cmdline-editing

It will give a list of shortcut working in command line mode.它将给出在命令行模式下工作的快捷方式列表。

An extract for your current problem :您当前问题的摘录:

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

or use q: as mentioned by Rene which allows you to edit previous typed commands in different modes.或使用q:正如 Rene 提到的那样,它允许您在不同模式下编辑以前键入的命令。

Plugins for "Normal mode"-like Command-line editing用于“正常模式”的插件,类似于命令行编辑

emacscommandline makes the command-line mode behave more like the Unix command line, by adding Emacs-style mappings (like in the Bash shell).emacscommandline通过添加 Emacs 风格的映射(如在 Bash shell 中)使命令行模式的行为更像 Unix 命令行。 Some of the mappings just map existing vim commands, but the rest implement functionality that is not available natively.一些映射只是映射现有的 vim 命令,但其余的实现了本机不可用的功能。

conomode.vim implements a kind of Normal mode ( "Cmdline-Normal mode" ) on top of the Command line.conomode.vim在命令行之上实现了一种普通模式(“Cmdline-Normal 模式”)。 Purpose is similar to the cmdline-window (q:), but navigation and editing can be done in-place.目的类似于 cmdline-window (q:),但导航和编辑可以就地完成。 Of course the cmdline-window is much more powerful.当然,cmdline-window 更强大。

Conomode does not replace Vim's native emacs-style command line editing, it just adds one key: CTRL-O (see below). Conomode 并没有取代 Vim 的原生 emacs 风格的命令行编辑,它只是增加了一个键:CTRL-O(见下文)。

  • enter with c_ (press CTRL-O while in Command-line mode)用 c_ 输入(在命令行模式下按 CTRL-O)
  • mode indicator is a colon ":", moved along with the cursor, hiding the char under it模式指示符是一个冒号“:”,随着光标移动,隐藏在它下面的字符
  • quit to Cmdline-mode with I, i, a, A, : or any unmapped key (which then executes or inserts itself), or wait 60 seconds.使用 I、i、a、A、: 或任何未映射的键(然后执行或插入自身)退出到 Cmdline 模式,或等待 60 秒。
  • quit to Normal mode with Esc使用 Esc 退出普通模式

(Admittedly, I don't understand the point of this plugin, since instead of hitting Ctrl + O to enter its Cono-mode, you could just as quickly pop up the command-line window with Ctrl + F .) (诚​​然,我不明白这个插件的意义,因为不是按 Ctrl + O进入它的 Cono 模式,你可以用Ctrl + F快速弹出命令行窗口。)

There might be other plugins out there.可能还有其他插件。 https://www.google.com/webhp?q=command-line+editing+plugin+vim https://www.google.com/webhp?q=command-line+editing+plugin+vim

Instead of employing a full-blown plugin, or as a complement to it, you can define your own mappings .您可以定义自己的映射,而不是使用成熟的插件或作为它的补充。 → See my other answer . → 请参阅我的其他答案

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 h j k l in Normal mode.例如,下面的一组映射将使命令行模式下的Alt + h向左移动一个字符, Alt + k调用之前的 Ex 命令等等,类似于普通模式下的h j k l

" 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>'

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.由于默认情况下 Alt 修饰键未映射(到重要的东西),您可以以相同的方式将其他(或所有)功能从正常模式拉到插入模式。 Eg: Moving to the beginning of the current word with Alt + b :例如:使用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>'

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).您必须将该代码复制到您的 vimrc 文件中,以便在每次启动 vim 时加载它(您可以通过键入:new $myvimrc以正常模式启动来打开它)。


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](https://stackoverflow.com/questions/7186880/using-normal-mode-motions-in-command-line-mode-in-vim/21207196#21207196). 与其“重新发明轮子”,或者作为您自己映射的补充,您可以借鉴其他作者的全功能 **plugins** → 参见我的其他 [答案](https://stackoverflow.com/questions/ 7186880/using-normal-mode-motions-in-command-line-mode-in-vim/21207196#21207196)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM