而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
而在插入模式在Vim,有任何方法来遍历文本移动一些字符向前和向后除了使用方向键?
如果我在插入模式下按下h, j, k和l,实际字符将打印在屏幕上,而不是在文本中移动。
我现在这样做的方式是不得不诉诸Ctrl + [(Esc)和遍历文本;但很明显,这是没有成效的。
当前回答
抱歉,vim不是这样工作的。
你应该切换到“正常”模式,导航,然后返回插入。
其他回答
可以使用imap将插入模式下的任何键映射到其中一个游标键。像这样:
imap h <Left>
现在h的工作就像在正常模式下一样,移动光标。(以这种方式映射h显然是一个糟糕的选择)
话虽如此,我并不认为使用VIM在文本中移动的标准方式是“低效的”。在普通模式下有很多非常强大的遍历文本的方法(比如使用w和b,或/和?,或f和f,等等)。
抱歉,vim不是这样工作的。
你应该切换到“正常”模式,导航,然后返回插入。
我相信Home和End(以及PageUp/PageDn)在插入模式下也能正常工作,但除此之外,我不相信还有其他为文本遍历定义的标准键。
Vim社区的许多人认为不应该在插入模式下导航,因为这不是Vim的方式。我认为这是在从标准编辑器过渡到Vim时学到的错误观点。
当您使用Vim的工具创建原子的、可重复的操作或查找时,Vim是最强大的。
如果您正在修复在同一插入会话中所犯的错误,则可以在插入模式下导航。您不应该在您修改的文本范围之外导航。
如果您在输入文本时出错,并退出插入模式来修复它,您将无法重复预期的操作。将重复修正。
Vim支持许多插入模式导航键。显然有方向键、Home键和结束键,但还有许多其他快捷键。参见:h ins-special-keys。
插入模式
运动
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