而在插入模式在Vim中,有没有什么办法来遍历文本移动一些字符向前和向后除了使用箭头键?
如果我按h,j,k和l在插入模式下,实际的字符显示在屏幕上,而不是在文本上移动.
我现在这样做的方式是不得不求助于Ctrl+ [(Esc)然后遍历文本; 但显然这不是很有成效.
P S*_*ved 626
你似乎误用了vim,但这可能是由于对它不太熟悉.
正确的方法是按Esc,去你想做的小修正,修复它,返回并继续编辑.这是有效的,因为Vim比通常的前进/后退/上/下有更多的动作.在您了解更多内容之后,这将更有成效.
这里有几个用例:
但是,如果你仍然希望在不离开插入模式的情况下进行小文本遍历,请遵循rson的建议并使用Ctrl+ O.例如,Ctrl+ OF+ f将移动到前一个__CODE__字符并让您处于插入模式.
fel*_*lix 134
在插入模式下,只使用一个命令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
Run Code Online (Sandbox Code Playgroud)
这可能是做你想做的事情最容易记住的最简单方法.
插入模式中其他非常有用的控制键:
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
Run Code Online (Sandbox Code Playgroud)
这些将消除许多浪费的开关回到正常模式.
Aar*_*oma 92
尽管Pavel Shved 说过 - 可能更习惯于习惯于Esc插入模式 - 这里是一组示例,用于在插入模式下快速导航:
" 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
Run Code Online (Sandbox Code Playgroud)
这将使Alt+ h在插入模式向左走一个字符,Alt+ j下降等,类似于hjkl普通模式.
您必须将该代码复制到vimrc文件中,以便在每次启动vim时加载它(您可以通过:new $myvimrc在Normal模式下键入来打开它).
由于Alt默认情况下未将修改键映射(对于重要的事物),因此您可以以相同的方式将其他(或所有)功能从"正常"模式拉到"插入"模式.例如:
用Alt+ 移动到当前单词的开头b:
inoremap <A-b> <C-o>b
inoremap <A-w> <C-o>w
Run Code Online (Sandbox Code Playgroud)
值得一提的是,Alt密钥的使用可能比复制普通模式行为更好:例如,这里是从相邻行复制从当前列到行尾的部分的映射:
" 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
Run Code Online (Sandbox Code Playgroud)
(我使用\ 行继续和缩进来增加清晰度 - 命令被解释为写在一行上.)
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)
Run Code Online (Sandbox Code Playgroud)
(在插入模式下没有值得注意的内置热键用于移动.)
这组映射使命令行中的上部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>
Run Code Online (Sandbox Code Playgroud)
或者,这些映射一次性添加插入模式和命令行模式的移动:
" 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>
Run Code Online (Sandbox Code Playgroud)
对于映射命令拉动普通模式命令,以命令行模式看起来有点从插入模式映射命令不同(因为命令行模式缺乏插入模式的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>'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Shy*_*ada 24
如果你是vim purist,请跳过阅读这个答案.OTOH,如果你是vim的新手,正在寻找一些你在一百个vim教程和博客中找不到的有用技巧,请继续阅读... :-)
这是2014年,作为最近回到vim我身边的人可以提供一些,可能是逆向的观点和提示.
虽然重复是vim中一个强大的概念,但我(个人)发现使用它或者强迫我计算(行,字符,单词等)或猜测是很奇怪的.我的大脑通常像"我想要光标在那里 "而不是"我想要光标_5_words_to_the_left_".能够快速移动光标,并以可视方式观察插入点的位置,这样我就可以将注意力集中在我正在编辑的内容上,而不必计算需要进行多少跳,才能到达需要编辑的位置.
...导航大量文本.
大多数(所有)现代计算机都有一个与键盘紧密集成的触摸板(例如MacBooks).工业设计师花了很多年时间来优化这些设计,因此不得不将手从键盘上移开的老问题不再是真正的问题.好吧,如果你已经习惯了鼠标并且不喜欢切换,但是对于任何刚接触vim的人(比如可能通过搜索找到这篇文章的人),这应该不是什么大问题.
启用鼠标后,单击并拖动可以切换到可视模式并标记区域以进行移动.
使用鼠标(滚轮)滚动,然后单击以定位光标(duh)就可以了.有关详细信息,请参见http://usevim.com/2012/05/16/mouse/.
这些是我称之为更现代(使用鼠标,滚轮等)在vim中导航的方式,根据您的输入偏好同样有效.
HTH
eve*_*992 14
Vim社区中的许多人认为你不应该在插入模式下导航,它不是Vim方式.我认为从标准编辑器转换到Vim时,这是一种不正确的情绪.
当您使用其工具创建原子,可重复的动作或查找时,Vim是最强大的.
如果要修复在同一个插入会话中所犯的错误,可以在"插入"模式下导航.您不应在您修改的文本范围之外导航.
如果您在输入文本时出错并退出插入模式以修复它,您将无法重复预期的操作,.将重复更正.
Vim确实支持许多插入模式导航键.显然有箭头键,Home和End,但也有许多其他快捷方式.见: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
Run Code Online (Sandbox Code Playgroud)
如果您可以让 Meta 键在您的终端中工作,您就可以更好地模拟 emacs 模式。正常模式下的导航要好得多,但对于较短的移动,它有助于保持插入模式。
对于更长的跳跃,我更喜欢以下默认翻译:
<Meta-b> maps to <Esc><C-left>
Run Code Online (Sandbox Code Playgroud)
这将切换到正常模式并返回一个词