在vim中使用插入模式的时候,除了通过使用箭头,是否还会有别的方法来遍历文本并将一些字符左右移动?
如果在插入模式下摁h、j、k和l,则会将实际字符打印在屏幕上,而不是在文本中移动。
我想要通过Ctrl+[(Esc)来遍历文本,但显然这是无效的。
发布于 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
https://stackoverflow.com/questions/-100007680
复制相似问题