首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在插入模式下遍历文本?

如何在插入模式下遍历文本?
EN

Stack Overflow用户
提问于 2018-03-19 23:52:36
回答 10查看 0关注 0票数 0

在vim中使用插入模式的时候,除了通过使用箭头,是否还会有别的方法来遍历文本并将一些字符左右移动?

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

我想要通过Ctrl+[(Esc)来遍历文本,但显然这是无效的。

EN

Stack Overflow用户

发布于 2018-03-20 01:31:14

插入模式

Movement

hjkl

遍历文本输入-更可取的做法是转义插入模式,以下是一组用于插入模式内快速导航的映射示例:

代码语言:javascript
复制
" 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来将当前语句移动到开头:

代码语言:javascript
复制
inoremap <A-b> <C-o>b
inoremap <A-w> <C-o>w

(Alt在插入模式下的其他用途)

值得一提的是,Alt键的使用可能比复制正常模式的办法更好,以下代码是从当前列的部分复制到行尾的映射。

代码语言:javascript
复制
" 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

vim documentation

编辑键

代码语言:javascript
复制
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上移:

代码语言:javascript
复制
" 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>

或者,这些映射一次添加移动到插入模式命令行模式:

代码语言:javascript
复制
" 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):

代码语言:javascript
复制
" 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>'

用于移动和编辑的内置键

代码语言:javascript
复制
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

票数 0
EN
查看全部 10 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档