在“插入”模式下遍历文本

本文翻译自:Traversing text in Insert mode

While in Insert Mode in Vim, is there any way to traverse the text moving some characters forward and backward other than using the arrow keys? 在Vim的插入模式下 ,除了使用箭头键之外,有没有办法遍历文本向前和向后移动一些字符?

If I press h , j , k and l while in Insert mode, the actual characters are printed on screen instead of moving through the text. 如果我在插入模式下按hjkl ,则实际字符将打印在屏幕上而不是在文本中移动。

The way I'm doing it at the moment is having to resort to Ctrl + [ ( Esc ) and traversing the text then; 我现在这样做的方式是不得不求助于Ctrl + [Esc )然后遍历文本; but obviously that is not productive. 但显然这不是很有成效。


#1楼

参考:https://stackoom.com/question/7Hul/在-插入-模式下遍历文本


#2楼

To have a little better navigation in insert mode, why not map some 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

If you can work around making the Meta key work in your terminal, you can mock emacs mode even better. 如果您可以在终端中使Meta键工作,则可以更好地模拟emacs模式。 The navigation in normal-mode is way better, but for shorter movements it helps to stay in insert mode. 正常模式下的导航方式更好,但对于较短的移动,它有助于保持插入模式。

For longer jumps, I prefer the following default translation: 对于更长的跳跃,我更喜欢以下默认翻译:

<Meta-b>    maps to     <Esc><C-left>

This shifts to normal-mode and goes back a word 这转移到正常模式并返回一个单词


#3楼

You can create mappings that work in insert mode. 您可以创建在插​​入模式下工作的映射。 The way to do that is via inoremap. 这样做的方法是通过inoremap。 Note the 'i' at the beginning of the command (noremap is useful to avoid key map collisions). 注意命令开头的'i'(noremap对于避免键映射冲突很有用)。 The corollary is 'n' for 'normal' mode. “正常”模式的推论是'n'。 You can surmise what vim thinks is 'normal' ;) 你可以推测vim认为'正常';)

HOWEVER, you really want to navigate around in text using 'normal' mode. 但是,您真的想要使用“普通”模式在文本中导航。 Vim is super at this kind of thing and all that power is available from normal mode. Vim在这种情况下非常出色,所有功能都可以从普通模式中获得。 Vim already provides easy ways to get from normal mode to insert mode (eg, i, I, a, A, o, O). Vim已经提供了从普通模式到插入模式的简单方法(例如,i,I,a,A,o,O)。 The trick is to make it easy to get into normal mode. 诀窍是让它很容易进入正常模式。 The way to do that is to remap escape to a more convient key. 这样做的方法是将转义重新映射到更方便的密钥。 But you need one that won't conflict with your regular typing. 但是你需要一个不会与你的常规打字冲突的。 I use: 我用:

inoremap jj <Esc>

Since jj (that's 2 j's typed one after the other quickly) doesn't seem to appear in my vocabulary. 因为jj(那是2 j的一个接一个地快速输入)似乎没有出现在我的词汇表中。 Other's will remap to where it's comfortable. 其他的将重新映射到舒适的地方。

The other essential change I make is to switch the CAPSLOCK and CONTROL keys on my keyboard (using the host computer's keyboard configuration) since I almost never use CAPSLOCK and it has that big, beautiful button right where I want it. 我做的另一个重要的改变是切换键盘上的CAPSLOCK和CONTROL键(使用主机的键盘配置),因为我几乎从不使用CAPSLOCK,并且它在我想要的地方有那么大,漂亮的按钮。 (This is common for Emacs users. The downside is when you find yourself on an 'unfixed' keyboard! Aaarggh!) (这对于Emacs用户来说很常见。缺点是当你发现自己处于'不固定'键盘时!Aaarggh!)

Once you remap CAPSLOCK, you can comfortably use the following insert mode remappings: 重新映射CAPSLOCK后,您可以轻松使用以下插入模式重映射:

Keeping in mind that some keys are already mapped in insert mode (backwards-kill-word is Cw (Control-w) by default, you might already have the bindings you want. That said, I prefer Ch so in my .vimrc I have: 请记住,某些键已经在插入模式下映射(默认情况下向后杀死单词是Cw(Control-w),您可能已经拥有了所需的绑定。也就是说,我更喜欢Ch所以在我的.vimrc中我有:

inoremap <C-h> <C-w>

BUT, you probably want the same muscle memory spasm in normal mode, so I also map Ch as: 但是,你可能想要在正常模式下相同的肌肉记忆痉挛,所以我也将Ch映射为:

nnoremap <C-h> db

(d)elete (b)ackwards accomplishes the same thing with the same key chord. (d)elete(b)ackwards使用相同的和弦完成同样的事情。 This kind of quick edit is one that I find useful in practice for typos. 这种快速编辑是我在拼写错误的实践中发现的一种。 But stick to normal mode for moving around in text and anything more than killing the previous word. 但坚持正常模式在文本中移动,而不是杀死前一个单词。 Once you get into the habit of changing modes (using a remap of course), it will be much more efficient than remapping insert mode. 一旦你养成改变模式的习惯(当然使用重映射),它将比重新映射插入模式更有效。


#4楼

While in insert mode , use Ctrl O to go to normal mode for just one command: 插入模式下 ,使用Ctrl O进入正常模式只需一个命令:

CTRL-O h  move cursor left 
CTRL-O l  move cursor right
CTRL-O j  move cursor down
CTRL-O k  move cursor up

which is probably the simplest way to do what you want and is easy to remember. 这可能是做你想做的事情最容易记住的最简单方法。

Other very useful control keys in insert mode: 插入模式中其他非常有用的控制键:

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

these will eliminate many wasteful switches back to normal mode. 这些将消除许多浪费的开关回到正常模式。


#5楼

Many people in the Vim community argue that you should not navigate in Insert mode, that it is not the Vim way. Vim社区中的许多人认为你不应该在插入模式下导航,它不是Vim方式。 I think this is an incorrect sentiment learned when transitioning from standard editors to Vim. 我认为从标准编辑器转换到Vim时,这是一种不正确的情绪。

Vim is most powerful when you use its tools to create atomic, repeatable actions or finds. 当您使用其工具创建原子,可重复的动作或查找时,Vim是最强大的。

It is ok to navigate while in Insert mode if you are fixing a mistake you made in the same Insert session. 如果要修复在同一个插入会话中所犯的错误,可以在“插入”模式下导航 You should not navigate outside of the range of text you modified. 您不应在您修改的文本范围之外导航。

If you make a mistake while entering text and escape out of Insert mode to fix it you will not be able to repeat the intended action, . 如果您在输入文本时出错并退出插入模式以修复它,您将无法重复预期的操作, . will repeat the correction. 将重复更正。

Vim does support many Insert mode navigation keys. Vim确实支持许多插入模式导航键。 Obviously there are the arrow keys, Home, and End, but there are also many other shortcuts. 显然有箭头键,Home和End,但也有许多其他快捷方式。 See :h ins-special-keys . 请参阅:h ins-special-keys


#6楼

我相信HomeEnd (以及PageUp / PageDn )在插入模式下也能正常工作,但除此之外,我不相信为文本遍历定义了任何其他标准键。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值