Quickly adding and deleting empty lines
From Vim Tips Wiki
Tip 1066 Previous Next created December 4, 2005 · complexity basic · author Ulfalizer · version 6.0
Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing.
Here is an alternative method that allows you to easily insert or delete blank lines above or below the current line. Your cursor position is not changed, and you stay in normal mode.
Put the following mappings in your vimrc:
" Ctrl-j/k deletes blank line below/above, and Alt-j/k inserts. nnoremap <silent><C-j> m`:silent +g/\m^\s*$/d<CR>``:noh<CR> nnoremap <silent><C-k> m`:silent -g/\m^\s*$/d<CR>``:noh<CR> nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR> nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
In normal mode, the mappings perform these operations without moving the cursor:
- Ctrl-j deletes the line below the current line, if it is blank.
- Ctrl-k deletes the line above the current line, if it is blank.
- Alt-j inserts a blank line below the current line.
- Alt-k inserts a blank line above the current line.
[edit] Explanations
- m` sets the context mark to the current cursor position.
- `` jumps to the context mark to restore the cursor position.
- The g/^\s*$/ commands search for a line matching ^ (begin line), then zero or more occurrences of whitespace, then $ (end line), that is, blank lines.
- \m sets the 'magic' option so the pattern will work regardless of the current 'magic' option.
- +g/pattern/d executes d (delete) on lines matching pattern in the range + (a single line after the current line).
- :noh turns off any search highlighting. :help :nohlsearch
- :set paste sets Paste mode to temporarily switch off auto indenting so program comments won't be inserted (for example, in a cpp file, o on a //comment may insert // on the next line).
[edit] See also
- Remove unwanted empty lines to delete all blank lines.
[edit] Alternative
TO DO
Following is from the original tip. Decide whether this is useful. If not, delete it.
The cursor maintains its position in the window if possible. You should probably set scrolloffset to something other than 0 for optimal comfort.
function! AddEmptyLineBelow()
call append(line("."), "")
endfunction
function! AddEmptyLineAbove()
let l:scrolloffsave = &scrolloff
" Avoid jerky scrolling with ^E at top of window
set scrolloff=0
call append(line(".") - 1, "")
if winline() != winheight(0)
silent normal! <C-e>
end
let &scrolloff = l:scrolloffsave
endfunction
function! DelEmptyLineBelow()
if line(".") == line("$")
return
end
let l:line = getline(line(".") + 1)
if l:line =~ '^\s*$'
let l:colsave = col(".")
.+1d
''
call cursor(line("."), l:colsave)
end
endfunction
function! DelEmptyLineAbove()
if line(".") == 1
return
end
let l:line = getline(line(".") - 1)
if l:line =~ '^\s*$'
let l:colsave = col(".")
.-1d
silent normal! <C-y>
call cursor(line("."), l:colsave)
end
endfunction
noremap <silent> <C-j> :call DelEmptyLineBelow()<CR>
noremap <silent> <C-k> :call DelEmptyLineAbove()<CR>
noremap <silent> <A-j> :call AddEmptyLineBelow()<CR>
noremap <silent> <A-k> :call AddEmptyLineAbove()<CR>
