而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
当前回答
Vim社区的许多人认为不应该在插入模式下导航,因为这不是Vim的方式。我认为这是在从标准编辑器过渡到Vim时学到的错误观点。
当您使用Vim的工具创建原子的、可重复的操作或查找时,Vim是最强大的。
如果您正在修复在同一插入会话中所犯的错误,则可以在插入模式下导航。您不应该在您修改的文本范围之外导航。
如果您在输入文本时出错,并退出插入模式来修复它,您将无法重复预期的操作。将重复修正。
Vim支持许多插入模式导航键。显然有方向键、Home键和结束键,但还有许多其他快捷键。参见:h ins-special-keys。
其他回答
为了在插入模式下有更好的导航,为什么不映射一些键呢?
imap <C-b> <Left>
imap <C-f> <Right>
imap <C-e> <End>
imap <C-a> <Home>
" <C-a> is used to repeat last entered text. Override it, if its not needed
如果您可以在您的终端中使用元键,那么您可以更好地模拟emacs模式。正常模式下的导航更好,但对于较短的移动,它有助于保持插入模式。
对于较长的跳跃,我更喜欢以下默认翻译:
<Meta-b> maps to <Esc><C-left>
这将切换到正常模式并返回一个单词
您可以创建在插入模式下工作的映射。方法是通过inoremap。注意命令开头的“i”(noremap有助于避免键映射冲突)。推论是“n”代表“正常”模式。你可以猜测vim认为什么是“正常的”;)
然而,你真的想在文本中使用“正常”模式导航。Vim在这方面非常出色,所有这些功能都可以从普通模式中获得。Vim已经提供了从普通模式切换到插入模式的简单方法(例如,i, i, a, a, o, o),诀窍是让它更容易进入正常模式。这样做的方法是将escape重新映射到一个更方便的键。但你需要一个不会与你的常规打字冲突的键盘。我使用:
inoremap jj <Esc>
因为jj(两个j一个接一个的快速输入)似乎没有出现在我的词汇表中。其他的会重新映射到舒适的地方。
我做的另一个重要的改变是切换我键盘上的CAPSLOCK和CONTROL键(使用主机的键盘配置),因为我几乎从不使用CAPSLOCK,它有一个大而漂亮的按钮,就在我想要它的地方。(这对于Emacs用户来说很常见。缺点是当你发现自己在一个“不固定”的键盘上时!Aaarggh !)
一旦你重新映射CAPSLOCK,你可以轻松地使用以下插入模式重新映射:
请记住,有些键已经在插入模式下映射(默认情况下,反向kill-word是C-w (Control-w)),您可能已经有了想要的绑定。也就是说,我更喜欢C-h,所以在我的。vimrc中,我有:
inoremap <C-h> <C-w>
但是,你可能想在正常模式下获得同样的肌肉记忆痉挛,所以我也将C-h映射为:
nnoremap <C-h> db
(d)elete (b)ackwards用相同的关键和弦完成了同样的事情。这种快速编辑是我在实践中发现的一种有用的错别字。但是在文本中移动和删除前一个单词以外的任何事情都要使用正常模式。一旦你养成了改变模式的习惯(当然是使用重映射),它将比重映射插入模式更有效。
虽然您应该能够使用h j k l键在插入模式下遍历编辑器可能有意义,但这实际上不是Vim的使用方式!Vim提供了许多命令来使编辑变得更快更容易。
正确的方法是按Esc键,去你想做一个小的修正,修正它,返回并继续编辑。这是有效的,因为Vim有更多的运动比通常字符向前/向后/向上/向下。当你学到更多的时候,你就会更有效率。
下面是一些用例:
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.
但是,如果您仍然想在不离开插入模式的情况下进行小型文本遍历,请遵循rson的建议并使用Ctrl+O。以我上面提到的第一个例子为例,Ctrl+OFf将移动到前面的“f”字符,并使您处于插入模式。
可以使用imap将插入模式下的任何键映射到其中一个游标键。像这样:
imap h <Left>
现在h的工作就像在正常模式下一样,移动光标。(以这种方式映射h显然是一个糟糕的选择)
话虽如此,我并不认为使用VIM在文本中移动的标准方式是“低效的”。在普通模式下有很多非常强大的遍历文本的方法(比如使用w和b,或/和?,或f和f,等等)。
是的,有办法。您可以在Insert Mode中使用gj和gk在单个逻辑行中向上(向后)和向下(向前)移动,该逻辑行环绕着许多可视线条。如果你有很长的行和/或宽度较窄的终端,这是很方便的,可以比方向键或按ESC键来使用其他导航快捷键快得多。还可以使用CTRL-O发出一个普通模式命令。这两种导航动作都不会使您退出插入模式。
按ESC键可能会破坏工作效率(在发明Vi时,它在终端键盘上的home行之外)。我处理的文本文件有非常长的逻辑行,整个段落的句子都是纯文本。这两个选择对我来说是一个真正的时间节省。如果我需要在段落中寻找某个特定的单词,切换到正常模式仍然很方便,但很多时候我只需要向上或向下移动一条可视线,然后继续插入。
实际上,我编辑了.vimrc来映射几乎无用的ALT键,所以我的左手按下ALT,而右手使用方向键。(并且它不会转义出插入模式:请注意每个条目末尾的“i”)。
imap <A-UP> <ESC>gki
imap <A-DOWN> <ESC>gji
我还在.vimrc中添加了行,以便我的方向键在正常模式下工作,在由一个逻辑行所包含的许多可视行中上下遍历(代码与上面的相同,只是您使用的是map或nmap而不是imap),我使用键盘的其他导航键ALT-KEY,都在插入模式中。
我为我的ALT键添加了额外的导航行,以便与键盘上预先分配的导航键一起使用,以便在插入模式下更快地导航。这些都不会让你退出插入模式:
" <ALT> navigation within insert-mode
imap <A-UP> <ESC>gki
imap <A-DOWN> <ESC>gji
imap <A-LEFT> <ESC>bi
imap <A-RIGHT> <ESC>ea
imap <A-END> <ESC>A
imap <A-PageUp> <ESC>(i
imap <A-PageDown> <ESC>l)i
imap <A-Home> <Esc>I
" so that <ALT> behaves the same in normal mode
nmap <A-UP> gk
nmap <A-DOWN> gj
nmap <A-LEFT> b
nmap <A-RIGHT> le
nmap <A-END> $
nmap <A-PageUp> (
nmap <A-PageDown> )
nmap <A-Home> ^
My "within" insert-mode navigation short cuts utilize the ALT key with PgUp, PgDn, Home & End keys too, to jump around far more quickly. I use alt-pgup and alt-pgdn presses to jump forward or backward by sentences. And, I use alt-home and alt-end to jump to the beginning and end of my paragraph long logical lines. None of this escapes me out of insert mode. (I figured that I might as well program all the preassigned navigation keys to work quickly within insert-mode, without leaving it). And if I want to move one character at a time, I just let up on the ALT key while still using the arrow keys.
所有这些都是对.vimrc的简单修改。您可以发挥创意,做各种事情来改进插入模式下的导航。否则,如果在长行中使用插入模式下的普通ole箭头键,则需要在右手按住方向键的同时,左手拿着一杯咖啡来啜饮。那要花很长时间!
顺便说一下,这个键已经与所有其他正常模式导航命令一起工作了,比如h,j,k,l和w, w, b, b, e和search: /,%等。除了它会自动退出插入模式,而无需按ESC键。因此,使用正常模式导航字符会让你陷入正常模式,直到你努力再按i, i, a, a, o, o, o,我发现这很容易忘记。
Since the keyboards preassigned navigation commands: arrows: UP, DOWN, RIGHT, LEFT and PGUP, PGDN, HOME, END don't do anything special with the key, I found that I might as well map them out in .vimrc to help my navigation within insert-mode, while staying within insert-mode. And, to reiterate, its helpful to me because I work with very long text documents that have paragraph long logical-lines that span across a lot of visual-lines, and the arrow keys and h,j,k,l don't acknowledge the visual-lines. This fixes that particular problem, both within insert-mode and normal-mode.
我把这归功于我在Kim Schulz的HACKING VIM 7.2一书中找到的知识