簡體   English   中英

在 Vim 的命令行模式下使用普通模式運動

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

在命令行模式下可以進行模態編輯嗎?

一些例子:

  • 寫入!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

vim的命令行模式下: <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:正如 Rene 提到的那樣,它允許您在不同模式下編輯以前鍵入的命令。

用於“正常模式”的插件,類似於命令行編輯

emacscommandline通過添加 Emacs 風格的映射(如在 Bash shell 中)使命令行模式的行為更像 Unix 命令行。 一些映射只是映射現有的 vim 命令,但其余的實現了本機不可用的功能。

conomode.vim在命令行之上實現了一種普通模式(“Cmdline-Normal 模式”)。 目的類似於 cmdline-window (q:),但導航和編輯可以就地完成。 當然,cmdline-window 更強大。

Conomode 並沒有取代 Vim 的原生 emacs 風格的命令行編輯,它只是增加了一個鍵:CTRL-O(見下文)。

  • 用 c_ 輸入(在命令行模式下按 CTRL-O)
  • 模式指示符是一個冒號“:”,隨着光標移動,隱藏在它下面的字符
  • 使用 I、i、a、A、: 或任何未映射的鍵(然后執行或插入自身)退出到 Cmdline 模式,或等待 60 秒。
  • 使用 Esc 退出普通模式

(誠​​然,我不明白這個插件的意義,因為不是按 Ctrl + O進入它的 Cono 模式,你可以用Ctrl + F快速彈出命令行窗口。)

可能還有其他插件。 https://www.google.com/webhp?q=command-line+editing+plugin+vim

您可以定義自己的映射,而不是使用成熟的插件或作為它的補充。 → 請參閱我的其他答案

復制普通模式命令的自定義映射

您可以在命令行模式下手動將特定的普通命令映射到擊鍵。

例如,下面的一組映射將使命令行模式下的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>'

由於默認情況下 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以正常模式啟動來打開它)。


與其“重新發明輪子”,或者作為您自己映射的補充,您可以借鑒其他作者的全功能 **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