I quite often find I have a need to insert a blank line either below or above the current line when editing in vim. o and O will do this, but they subsequently switch into insert mode, which is annoying. Is there any built-in command to do this which will remain in normal mode?
- 2,264
5 Answers
I've been using these
map <Enter> o<ESC>
map <S-Enter> O<ESC>
in my .vimrc for years.
Press Enter to insert a blank line below current, Shift + Enter to insert it above.
- 2,394
Both Tim Pope's unimpaired plugin as well as my own LineJuggler plugin provide [<Space> and ]<Space> mappings to add [count] blank lines above / below the current line.
Basically, it boils down to this:
nnoremap <silent> ]<Space> :<C-u>put =repeat(nr2char(10),v:count)<Bar>execute "'[-1"<CR>
nnoremap <silent> [<Space> :<C-u>put!=repeat(nr2char(10),v:count)<Bar>execute "']+1"<CR>
- 23,523
Yet another way to insert lines above or below:
nnoremap <Enter> :call append(line('.'), '')<CR>
nnoremap <S-Enter> :call append(line('.')-1, '')<CR>
Note that the solution from romainl and Mr Shunz will move the cursor to the newly inserted line, whereas this and also the one from Ingo Karkat will keep the cursor at the same spot.
- 550
No, there's no built-in command for that.
These mappings do what you want:
nnoremap <leader>o o<Esc>
nnoremap <leader>O O<Esc>
- 23,415
I know this is an old thread, but I was having the same issue.
I really wanted a quick modifier like Ctrl-O / Ctrl-Shift-O (or CMD) but many emulators are setup so they can't detect something like Ctrl-Shift.
I ended up going with this below.
vim.keymap.set("n", "<o><o>", "<esc>o<esc>", { desc = "Enter new line below" })
vim.keymap.set("n", "<S-O><S-O>", "<esc>O<esc>", { desc = "Enter new line above" })
I used this in conjunction with
vim.o.timeoutlen = 250
You can adjust that timeout length for you, I found 250 milliseconds is good for me, the default is 1000 or 1 second.
I then took this to the next level as well...
map("n", "<o><o>", function()
local count = vim.v.count1
vim.cmd("normal! " .. count .. "o<esc")
end, { desc = "Insert new line below (count aware" })
map("n", "<S-O><S-O>", function()
local count = vim.v.count1
vim.cmd("normal! " .. count .. "O")
end, { desc = "Insert new line above (count aware)" })
This will make it "count aware" so you could do 5oo or 7OO (those are captial o's not zeros) to enter 5 lines below or 7 lines above.