created September 9, 2010 · complexity basic · author jeet · version 7.0
O open commands are useful to insert a blank line below or above the current line, and start insert mode. This tip shows how to easily insert multiple blank lines so new text that you enter will have at least one blank line before and after. A count controls how many blank lines are inserted.
Using this tip, and assuming the default backslash leader key, typing
5\o will insert five blank lines below the current line, and will enter insert mode on the second blank line. The
S command is used to start insert mode, so the cursor is automatically indented according to the rules for the current file type (if any). Typing
5\O performs a similar operation, above the current line.
The minimum count is 3, so typing
\o will insert three blank lines (ensuring that one blank line is before and after the text that will be entered).
Without this tip, you could type
5o then press Esc. That would insert five blank lines, but you would then need to enter commands to position the cursor and start insert mode. While easy to do, that process can break your train of thought.
Script[edit | edit source]
~/.vim/plugin/insertmultiple.vim (Unix) or
$HOME/vimfiles/plugin/insertmultiple.vim (Windows) containing the script below, then restart Vim. Alternatively, add the script to your vimrc and restart Vim.
" Open multiple lines (insert empty lines) before or after current line, " and position cursor in the new space, with at least one blank line " before and after the cursor. function! OpenLines(nrlines, dir) let nrlines = a:nrlines < 3 ? 3 : a:nrlines let start = line('.') + a:dir call append(start, repeat([''], nrlines)) if a:dir < 0 normal! 2k else normal! 2j endif endfunction " Mappings to open multiple lines and enter insert mode. nnoremap <Leader>o :<C-u>call OpenLines(v:count, 0)<CR>S nnoremap <Leader>O :<C-u>call OpenLines(v:count, -1)<CR>S