而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?

如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。

我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。


当前回答

在插入模式下,使用CtrlO切换到正常模式,只执行一个命令:

CTRL-O h  move cursor left 
CTRL-O l  move cursor right
CTRL-O j  move cursor down
CTRL-O k  move cursor up

这可能是做你想做的事情的最简单的方法,也很容易记住。

插入模式中其他非常有用的控制键:

CTRL-W    delete word to the left of cursor
CTRL-O D  delete everything to the right of cursor
CTRL-U    delete everything to the left of cursor
CTRL-H    backspace/delete
CTRL-J    insert newline (easier than reaching for the return key)
CTRL-T    indent current line
CTRL-D    un-indent current line

这将消除许多浪费的开关回到正常模式。

其他回答

在插入模式下,使用CtrlO切换到正常模式,只执行一个命令:

CTRL-O h  move cursor left 
CTRL-O l  move cursor right
CTRL-O j  move cursor down
CTRL-O k  move cursor up

这可能是做你想做的事情的最简单的方法,也很容易记住。

插入模式中其他非常有用的控制键:

CTRL-W    delete word to the left of cursor
CTRL-O D  delete everything to the right of cursor
CTRL-U    delete everything to the left of cursor
CTRL-H    backspace/delete
CTRL-J    insert newline (easier than reaching for the return key)
CTRL-T    indent current line
CTRL-D    un-indent current line

这将消除许多浪费的开关回到正常模式。

可以使用imap将插入模式下的任何键映射到其中一个游标键。像这样:

imap h <Left>

现在h的工作就像在正常模式下一样,移动光标。(以这种方式映射h显然是一个糟糕的选择)

话虽如此,我并不认为使用VIM在文本中移动的标准方式是“低效的”。在普通模式下有很多非常强大的遍历文本的方法(比如使用w和b,或/和?,或f和f,等等)。

如果你是一个纯粹的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

对于一些常用的动作和动作,我定义了以下映射。与CTRL+O组合相比,这节省了一次击键,因为我经常需要它们,它们在长期内是有回报的 运行。

inoremap <A-$> <C-o>$
inoremap <A-^> <C-o>^
inoremap <A-h> <Left>
inoremap <A-l> <Right>
inoremap <A-O> <C-O>O
inoremap <A-o> <C-o>o

抱歉,vim不是这样工作的。

你应该切换到“正常”模式,导航,然后返回插入。