在vim中使用插入模式的时候,除了通过使用箭头,是否还会有别的方法来遍历文本并将一些字符左右移动?
如果在插入模式下摁h、j、k和l,则会将实际字符打印在屏幕上,而不是在文本中移动。
我想要通过Ctrl+[(Esc)来遍历文本,但显然这是无效的。
发布于 2018-03-20 00:24:24
可以通过摁ESC来解决这个问题,然后在出现错误的地方对它进行修正,然后再返回继续编辑。这很有效,因为Vim比向前/向后/向上/向下的字符来说移动更灵活
以下有几个用例:
但是如果你仍然想在不离开插入模式的情况下执行小文本遍历,可以采纳rson的建议来使用Ctrl+O。例如,ctrl+of+f会将你移到前面的f
字符并让你进入插入模式。
发布于 2018-03-20 01:31:14
遍历文本输入-更可取的做法是转义插入模式,以下是一组用于插入模式内快速导航的映射示例:
" 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。
每次启动vim时,都必须将该代码复制到vimrc文件中才能加载(你可以在正常模式下输入:new $myvimrc
)。
由于Alt修饰符键在默认情况下没有映射(到一些重要的东西),所以你可以以同样的方式将其他(或全部)功能从正常模式拉到插入模式。例如:
通过Alt+b来将当前语句移动到开头:
inoremap <A-b> <C-o>b
inoremap <A-w> <C-o>w
值得一提的是,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
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
发布于 2018-03-20 01:54:25
在插入模式,使用Ctr+lO访问正常模式只有一个命令:
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
https://stackoverflow.com/questions/-100007680
复制相似问题