简体   繁体   中英

Vim oddities in keymapping

I like to insert blank lines without entering insert mode and I used this keymapping:

nomap go o <esc>

This does create the blank line but introduces some weird behaviour. I have smart indent and autoindent set. The new line follows the indents but doesn't remove them even though doing so manually automatically removes the redundant whitespace. It also adds a single whitespace where the cursor is each time.

Anyone have any insights as to explain this behaviour?

Vim is very literal with how you write your mapping commands - it's actually processing the space in your mapping before it does the <ESC> . In other words, your mapping does this:

nnoremap go o<SPACE><ESC>

You should change it to:

nnoremap go o<ESC>

And make sure you don't have any extra spaces in the mapping!

I agree with "too much php". This is the relevant section from my .vimrc

nnoremap <A-o> o<ESC>k
nnoremap <A-O> O<ESC>j

I think it's faster since you get the cursor back at the original line (Although not on the original character).

As usual, the vim wiki has a useful tip: Quickly adding and deleting empty lines . The trick is to set paste before adding the new line and afterwards set nopaste . Additionally, this will set a mark to remember the cursor position and jump back to where you were.

nnoremap go :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap gO :set paste<CR>m`O<Esc>``:set nopaste<CR>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM