而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
当前回答
我相信Home和End(以及PageUp/PageDn)在插入模式下也能正常工作,但除此之外,我不相信还有其他为文本遍历定义的标准键。
其他回答
插入模式
运动
hjkl
尽管Pavel Shved说过——习惯转义插入模式可能更可取——这里有一个在插入模式下快速导航的映射示例集:
" provide hjkl movements in Insert mode via the <Alt> modifier key
inoremap <A-h> <C-o>h
inoremap <A-j> <C-o>j
inoremap <A-k> <C-o>k
inoremap <A-l> <C-o>l
这将使插入模式下的Alt+h向左移动一个字符,Alt+j向下,以此类推,类似于普通模式下的hjkl。
您必须将该代码复制到vimrc文件中,以便在每次启动vim时加载它(您可以通过键入:new $myvimrc starting in Normal mode打开该文件)。
任何正常模式移动
因为默认情况下Alt修饰键没有映射到(重要的东西),所以您可以以同样的方式将其他(或所有)功能从普通模式拉到插入模式。例如: 用Alt+b移动到当前单词的开头:
inoremap <A-b> <C-o>b
inoremap <A-w> <C-o>w
(插入模式下Alt的其他用法)
值得一提的是,Alt键可能有比复制Normal模式行为更好的用途:例如,下面是从相邻行复制当前列到行尾部分的映射:
" Insert the rest of the line below the cursor.
" Mnemonic: Elevate characters from below line
inoremap <A-e>
\<Esc>
\jl
\y$
\hk
\p
\a
" Insert the rest of the line above the cursor.
" Mnemonic: Y depicts a funnel, through which the above line's characters pour onto the current line.
inoremap <A-y>
\<Esc>
\kl
\y$
\hj
\p
\a
(我使用了行续和缩进来增加清晰度。这些命令就像写在一行上一样。)
内置热键编辑
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 (influenced by the 'backspace' option)
(在插入模式下,没有明显的内置热键用于移动。)
参考:help insert-index
命令行模式
这组映射使得上Alt+hjkl移动在命令行中可用:
" provide hjkl movements in Command-line mode via the <Alt> modifier key
cnoremap <A-h> <Left>
cnoremap <A-j> <Down>
cnoremap <A-k> <Up>
cnoremap <A-l> <Right>
或者,这些映射将移动同时添加到插入模式和命令行模式:
" provide hjkl movements in Insert mode and Command-line mode via the <Alt> modifier key
noremap! <A-h> <Left>
noremap! <A-j> <Down>
noremap! <A-k> <Up>
noremap! <A-l> <Right>
将普通模式命令拉到命令行模式的映射命令看起来与插入模式映射命令有点不同(因为命令行模式缺少插入模式的Ctrl+O):
" Normal mode command(s) go… --v <-- here
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>'
cnoremap <expr> <A-b> &cedit. 'b' .'<C-c>'
cnoremap <expr> <A-w> &cedit. 'w' .'<C-c>'
内置热键移动和编辑
CTRL-B cursor to beginning 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
抱歉,vim不是这样工作的。
你应该切换到“正常”模式,导航,然后返回插入。
如果你是一个纯粹的vim主义者,跳过阅读这个答案。OTOH,如果你是vim的新手,正在寻找一些有用的技巧,你在数百个vim教程和博客中找不到,请继续阅读…: -)
一些非正统的(vim)方法
现在是2014年,作为一个最近又回到vim的人,我可以提供一些潜在的逆向观点和技巧。
使用shift+左或shift+右遍历单词
While repetition is a powerful concept in vim, I (personally) find it strange that using it either forces me to count (lines, characters, words, etc.) or make guesses. My brain usually works like "I want the cursor there" and not like "I want the cursor _5_words_to_the_left_". Quickly being able to move the cursor, and visually observe where the insertion point this way allows me to keep my mind on what I'm editing instead of having to count how many hops I need to make to get to where I need to edit.
打开鼠标模式,使用鼠标滚轮并单击
...在大量文本中导航。
大多数(所有)现代电脑都有一个与键盘紧密结合的触摸板(例如macbook)。工业设计师们花费了多年的时间来优化这些设计,因此必须将手从键盘上移开的老问题不再是一个真正的问题。好吧,如果你习惯了鼠标,不喜欢切换,但对于vim的新手(比如那些可能通过搜索找到这篇文章的人),这应该不是什么大问题。
作为奖励,点击+拖动使您进入可视模式
在启用鼠标的情况下,单击和拖动可以切换到可视模式,并标记一个区域以进行拉拽。
使用滚轮
使用鼠标(滚轮)来滚动,然后点击来定位光标(废话)。更多信息请参见http://usevim.com/2012/05/16/mouse/。
所以…
这些是我所说的更现代的(使用鼠标、滚轮等)在vim中导航的方式,根据您的输入偏好同样有效。
HTH
虽然您应该能够使用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”字符,并使您处于插入模式。
我相信Home和End(以及PageUp/PageDn)在插入模式下也能正常工作,但除此之外,我不相信还有其他为文本遍历定义的标准键。