Insert empty lines without entering insert mode
I often find myself bouncing on o or O and ctrl{ to insert blank lines and get back out of insert mode.
Thinking there must be a simpler way, and hoping to retain my cursor position, I hacked together these sloppy macros:
map <Leader>O :let cursorpos = getpos(".")<CR>:i<CR><CR>.<CR>:let cursorpos[1] = cursorpos[1] + 1<CR>:call setpos('.', cursorpos)<CR>
map <Leader>o :let cursorpos = getpos(".")<CR>:a<CR><CR>.<CR>:call setpo开发者_开发技巧s('.', cursorpos)<CR>
However, this doesn't allow for ranges. It would be nice to be able to go 5\O and get 5 blanks above my current line.
Any suggestions on how I can fix this to allow ranges and still return to the original cursor position when done?
You can use :call append(linenumber, repeat([''], 5))
. It won't move the cursor.
The unimpaired plugin adds mappings that do what you want. You can insert a blank line above the cursor with [<space>
, or below the cursor with ]<space>
. Both of these mappings can be prepended with a count.
I added this to my .vimrc
"create new line below current line without leaving normal mode or moving cursor
noremap - m`o<Esc>``
"create new line above current line without leaving normal mode or moving cursor
noremap _ m`O<Esc>``
I prefer this solution for several reasons:
- I don't normally use the - or _ keys in normal mode and they are located on the same physical key on my keyboard.
- It just seems like installing a plugin for this is overkill. I avoid downloading plugins for simple things since they introduce new code to my environment that I don't yet fully understand or trust and it may interfere with other customizations or plugins.
- I tried the solution from the author of this question located in his comment under the accepted answer, but after using this, the . command does strange and disturbing things.
- I usually prefer pressing . multiple times over passing a numeric argument, so that instead of sitting there counting things on my fingers, I just keep pressing . until it looks right (less conceptual visualization and more real vision). Unfortunately even this solution does not work the way I want with the . command. However, by mapping - and _, these can be pressed repeatedly for the same effect. Just a shame it is not closer to the home row...
WARNING: Marking the ` key affects the jump list. You can use any other mark if that makes your pleasure++
精彩评论