Vim Tips Wiki
Advertisement
Tip 1066 Printable Monobook 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.

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

See also

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>

Comments

It does not work for me... it works like if I didn't configure anything

I am sorry this tip did not work for you, anonymous user. We would obviously like all our tips to work right away, to be correct, concise, and informative. However, without more information, we cannot fix any problems in this tip.
  • What is "it"? Which script did you try using?
  • What did you do to try to get "it" to "work"? I.e. how did you install and invoke the script?
  • What didn't work about "it"? Did it do nothing at all? Did it do something incorrect? In other words, what were you expecting to happen, and what happened instead?
If these questions are not answered in a timely fashion, we will delete this comment as unhelpful.
--Fritzophrenic 19:00, November 19, 2009 (UTC)

Sorry, it is my first time... Here is my .vimrc

source $VIMRUNTIME/macros/matchit.vim "script for matlab

:filetype plugin on
:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab
:set cindent
:set smartindent
:set autoindent
"" from http://vim.wikia.com/wiki/Quickly_adding_and_deleting_empty_lines
" 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>


These nnoremap commands suggested before do not work for me...

--IgorFobia 18:00, November 21, 2009 (UTC)
Advertisement