在插入模式下遍历文本

2024-02-21

而在插入模式在Vim中,除了使用箭头键之外,还有什么方法可以遍历文本,向前和向后移动一些字符?

If I press h, j, k and l while in Insert mode, the actual characters are printed on screen instead of moving through the text.

The way I'm doing it at the moment is having to resort to Ctrl + [ (Esc) and traversing the text then; but obviously that is not productive.


While it may make sense that you should be able to use the h j k l keys to traverse the editor in insert mode, but that is actually not the way Vim is intended to be used! There are many commands that Vim provides to make editing faster and easier.

The right way is to press Esc, go where you want to do a small correction, fix it, go back and keep editing. It is effective because Vim has much more movements than usual character forward/backward/up/down. After you learn more of them, this will happen to be more productive.

以下是一些用例:

  • You accidentally typed "accifentally". No problem, the sequence EscFfrdA will correct the mistake and bring you back to where you were editing. The Ff movement will move your cursor backwards to the first encountered "f" character. Compare that with Ctrl+DeldEnd, which does virtually the same in a casual editor, but takes more keystrokes and makes you move your hand out of the alphanumeric area of the keyboard.
  • You accidentally typed "you accidentally typed", but want to correct it to "you intentionally typed". Then Esc2bcw will erase the word you want to fix and bring you to insert mode, so you can immediately retype it. To get back to editing, just press A instead of End, so you don't have to move your hand to reach the End key.
  • You accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl+w will delete the previous word without leaving insert mode. And it happens to be much faster to erase a small word than to fix errors within it. I'm so used to it that I had closed the browser page when I was typing this message...!
  • Repetition count is largely underused. Before making a movement, you can type a number; and the movement will be repeated this number of times. For example, 15h will bring your cursor 15 characters back and 4j will move your cursor 4 lines down. Start using them and you'll get used to it soon. If you made a mistake ten characters back from your cursor, you'll find out that pressing the key 10 times is much slower than the iterative approach to moving the cursor. So you can instead quickly type the keys 12h (as a rough of guess how many characters back that you need to move your cursor), and immediately move forward twice with ll to quickly correct the error.

But, if you still want to do small text traversals without leaving insert mode, follow rson https://stackoverflow.com/users/155299/rson's advice https://stackoverflow.com/questions/1737163/vim-traversing-text-in-insert-mode#comment1620134_1737259 and use Ctrl+O. Taking the first example that I mentioned above, Ctrl+OFf will move you to a previous "f" character and leave you in insert mode.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在插入模式下遍历文本 的相关文章

  • vim 命令可以映射到具有 >1 修饰符的组合键(例如 ctrl-alt-v)

    是否可以将 vim 命令映射到涉及多个修饰符的按键组合 例如 ctrl alt v 据我所知 唯一可以与另一个组合使用的修饰键是shift 例如
  • 如何只在当前行进行搜索和替换?

    I see 如何在特定行中搜索和替换 https stackoverflow com questions 17319557 search and replace in vim in specific lines 按行号指定 以及如何使用当前
  • Emacs Evil“重复”(点)行为

    我的中有以下行 emacs define key evil normal state map M j lambda interactive evil next line 5 这使得文件中的导航变得更加容易 例如 与M j我在下面 5 行 所
  • Vim NerdCommenter:在 vimrc 中添加新文件类型

    有没有办法在 vimrc 中为 NerdCommenter 定义新的文件类型注释 它已经可以工作了 但是如果我可以将其直接放入 vimrc 中 备份配置会更容易 Thanks NERDCommenter 能够解析commentstring选
  • Vim 中的类和函数名称高亮显示

    在沉迷于它的模态输入之后 我最近刚刚从 Textmate 设置了我的 Vim 环境 不过 Vim 中的语法高亮似乎不太美观 我用 C 编写代码 由于函数调用和类名无法突出显示 因此代码更难以阅读 我玩了一下配色方案 但找不到任何与 类名 或
  • vim:视觉明星搜索未按预期工作

    我复制了这个函数来进行视觉搜索 and function s VSetSearch cmdtype let temp s norm gv sy let V substitute escape s a cmdtype n n g let s
  • 在Vim中,如何删除单词的后缀?

    在vim中 在正常模式下 如果光标位于单词中 而不是最后一个字母 de从光标位置删除单词的后缀 如果光标位于最后一个字母上 x也这样做 同时de会跳到下一个单词的末尾 您将使用什么命令在这两种情况下都有效 无论最后一个字母与否 目的是将命令
  • 有没有办法用 Vim 自动重新格式化大括号?

    我想重新格式化一些代码 如下所示 if cond foo to if cond foo 由于这是C代码 所以我一直在看cindent cinoptions与使用 但它似乎不处理多行规则 我一直在看formatoptions与使用gq 而且似
  • 当vim进入可视模式时运行命令

    我使用一个小脚本来触发插入模式以更改行号颜色 function CursorLineNrColorInsert mode Insert mode blue if a mode i highlight CursorLineNr ctermfg
  • 插件 (vim-latex) 在启动时导致 gVim 崩溃

    我正在尝试使用在 Windows XP 上使用 gVim 但是一旦我打开 tex 文件 或者一旦我set ft tex gVim 崩溃且没有可见的错误消息 控制台 vim 中不会发生崩溃 我已经按照设置说明进行了操作 据我所知 并且我尝试禁
  • 在 vim 中,+register 和 +register 与 .命令?

    我很难理解这个问题的解决方案维姆高尔夫挑战赛 http vimgolf com challenges 5192f96ad8df110002000002 最好的建议解决方案是 cw
  • Pathogen 不加载插件

    病原体对我不起作用 我正在遵循 Adam Lowe 发布的提示here http www adamlowe me 2009 12 vim destroys all other rails editors html 还有更多 没有任何成功 我
  • 在 Vim 中的缩进线上按“Home”

    我有一个坏习惯 就是使用 home 键返回到行首 当我最近开始使用 vim 时 我注意到当我在缩进的行上按 home 键时 它会让我回到该行的开头 在 Notepad 我曾经使用的编辑器 中 它会让我返回到该行代码的开头 就在缩进之后 有没
  • 带有 Viper 和 Vimpulse 的 Emacs 缺少哪些 Vim 功能?

    Emacs 的一些重要功能在 Vim 中是缺失的 例如 comint 模式 并且没有脚本 插件可以替代它们 与 Emacs 相比 Vim 也有一些优点 例如模式编辑和通常更好的默认快捷键 然而 Viper 模式让我两者兼而有之 Vimpul
  • Python 解释器 Shell 可以与 Vi(m) 集成吗?

    我喜欢使用 bpython 但在 Ruby 中 有一个名为 Interactive editor 的 gem 它可以将 Vi m 与 Ruby shell 结合起来 从而使开发过程更加舒适 Interactive editor 的一个很好的
  • 如果文件类型 == tex

    如果文件是乳胶文件 我想在 vimrc 中运行命令 我想我的语法有问题 但它不起作用 有什么线索吗 if filetype tex set spell endif 您可以使用自动命令来实现您想要的 autocmd BufNewFile Bu
  • 禁用 VIM 中的警告?

    有没有办法禁用 VIM 中的警告 特别是 当文件从只读变为可写时 我想禁用警告 12 我有一个脚本可以打开文件进行编辑 但 vim 认为文件已更改并发出警告 Thanks 我的 vimrc 中有以下内容 你应该只需要第二个 它将消息回显到状
  • vim 退出时恢复 shell

    我刚刚在我的新计算机上安装了 Arch 但我不知道需要向 vimrc 添加什么命令 以便它在退出时恢复 shell 内容 在调用 vim 之前 也就是说 我希望我的 shell 看起来像这样 whoami root who root tty
  • 将 vim 的 vertsplit 字符更改为 │

    我认为这与代码页相关 但询问也无妨 在 windows xp 的 cmd 上 gvim 7 2 如何更改 vertsplit 字符 而不是默认的 它是 因此它是一条完整的线 而不是一条分割线 该字符通常会更改为 set fillchars
  • 在Vim函数中获取不带文件扩展名的文件名

    我想在Vim中获取不带文件扩展名的文件名 我在我的中编写了以下函数 vimrc文件来编译并运行Java程序 function JAVA RUN javac M endfunction map

随机推荐