【问题标题】:Using normal-mode motions in command-line mode in Vim在 Vim 的命令行模式下使用普通模式的动作
【发布时间】:2011-11-03 10:51:36
【问题描述】:

是否可以在命令行模式下进行模态编辑?

一些例子:

  • 写完!ls ~/foo/bar我想db删除
  • 我执行了上面的命令,现在我想把ls改成mv,然后跳回$

【问题讨论】:

    标签: vim vi


    【解决方案1】:

    默认情况下,您可以在 Vim 命令行上按 Control + f(或查看 set cedit),这会打开命令行窗口,您可以在其中使用普通模式的 Vim 编辑键编辑命令。 Enter 将运行命令或Control + c 将返回标准命令行。

    因此,在您的具体示例中,您可以在 Vim 命令行上按 Control + f,然后按 db,它会执行您想要的操作。

    当我必须执行更复杂的编辑命令时,我会使用上述方法,因为我更熟悉 Vim 编辑键而不是其他方法。我还发现普通模式的 vim 键更强大。

    更多信息请参见:help c_ctrl-f

    【讨论】:

    • 改变生活。不知何故,我多年来一直在使用 vim 却不知道这一点。非常感谢。
    • q: 将从正常模式打开此窗口。还有q/q?变种
    【解决方案2】:

    vim的命令行模式下: <ctrl-w>删除一个字

    正常模式q: 进入命令历史记录(可以用 vim 命令)

    查看:help cmdline-editing:help cmdline-window 了解更多命令。

    【讨论】:

      【解决方案3】:

      用于在命令行模式下移动和编辑的内置热键

      <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 年起 - 从那时起可能已添加新的键盘快捷键

      【讨论】:

      • 我在 Bash 中没有找到类似 yank-last-arg History Command 的东西
      【解决方案4】:

      在 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
      

      或使用 Rene 提到的q:,它允许您在不同模式下编辑以前键入的命令。

      【讨论】:

      • 我在我的answer中对这些内置热键(用于在命令行模式下移动和编辑)进行了完整而结构化的概述
      【解决方案5】:

      用于“普通模式”的插件,例如命令行编辑

      emacscommandline 使命令行模式的行为更像 Unix 命令行, 通过添加 Emacs 样式的映射(如在 Bash shell 中)。某些 映射只是映射现有的 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

      您可以定义您自己的映射,而不是使用成熟的插件或作为它的补充。 → 见我的另一个answer

      【讨论】:

        【解决方案6】:

        复制普通模式命令的自定义映射

        您可以在命令行模式下手动将特定的 Normal 命令映射到击键。

        例如,以下一组映射将使命令行模式下的 Alt+h 左移一个字符,Alt+k 调用前面的 Ex 命令等等,类似于 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 来打开它)。


        而不是“重新发明轮子”,或者作为对您自己的映射的补充,您可以借鉴其他作者的全功能**插件**→查看我的其他 [答案](https://stackoverflow.com/questions/ 7186880/using-normal-mode-motions-in-command-line-mode-in-vim/21207196#21207196)。

        【讨论】:

          猜你喜欢
          • 2020-05-28
          • 1970-01-01
          • 2012-10-20
          • 1970-01-01
          • 1970-01-01
          • 2012-05-21
          • 2016-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多