ipwnponies
ipwnponies

Reputation: 219

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?

Upvotes: 2

Views: 241

Answers (3)

innaM
innaM

Reputation: 47829

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>

Upvotes: 0

Mosh
Mosh

Reputation: 2628

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).

Upvotes: 0

too much php
too much php

Reputation: 90978

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!

Upvotes: 5

Related Questions