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

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

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

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

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

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

EN

回答 10

Stack Overflow用户

发布于 2018-03-20 00:24:24

可以通过摁ESC来解决这个问题,然后在出现错误的地方对它进行修正,然后再返回继续编辑。这很有效,因为Vim比向前/向后/向上/向下的字符来说移动更灵活

以下有几个用例:

  • 你不小心输入了“accifentally”,不过序列EscFfrdA会纠正错误,并将你带回原来编辑它的位置。Ff移动会使你回到遇到的第一个“f”字符。 与Ctrl + < - > - > - > - > deldEnd进行比较,它在随便编辑器中的功能几乎相同,但点击键盘的次数会增多,使你可以将手移出键盘的字母数字空间。
  • 你不下心地输入了“你意外键入的内容”,但想要将其改正为“你有意输入的内容”。 可以通过Esc2bcw来删除你想修复的单词并进入插入模式,以便你立即重新输入。 可以通过A而不是End来返回编辑状态。
  • 你不小心输入了了“mouse”而不是“mice”。Ctrl + W会删除前一个单词,但并不会从插入模式中移出。
  • 重复计数在很大程度上被利用不足。在进行移动之前,你可以输入一个数字; 并且运动将重复这个次数。 例如,15小时将带回15个字符,4j将向下滚动4行。 而且你会发现你按10次< - 键会比移动光标的迭代方法快,当你输入12h时,注意你会返回一个错误,并且可以立即用ll来更正。

但是如果你仍然想在不离开插入模式的情况下执行小文本遍历,可以采纳rson的建议来使用Ctrl+O。例如,ctrl+of+f会将你移到前面的f字符并让你进入插入模式。

票数 0
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

Stack Overflow用户

发布于 2018-03-20 01:54:25

插入模式,使用Ctr+lO访问正常模式只有一个命令:

代码语言:javascript
复制
CTRL-O h  move cursor left 
CTRL-O l  move cursor right
CTRL-O j  move cursor down
CTRL-O k  move cursor up

在插入模式下的其他控制键:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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