Wikia

Vim Tips Wiki

Quickly adding and deleting empty lines

Talk0
1,610pages on
this wiki

Redirected from VimTip1066

Tip 1066 Printable Monobook Previous Next

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

ExplanationsEdit

  • 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 alsoEdit

AlternativeEdit

 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>

Following is a version that works in viemu and also doesn't conflict with preset keybindings hence this isn't strictly a VIM tip - delete it if you don't want it:

" Ctrl-]/[ deletes line below/above, and Alt-]/[ inserts.
nnoremap <c-]> mzj:s/^\\s*\\n//i<CR>`z:noh<CR>
nnoremap <c-[> mzk:s/^\\s*\\n//i<CR>`z:noh<CR>
nnoremap <a-]> :<CR>mzo<Esc>`z:<CR>
nnoremap <a-[> :<CR>mzO<Esc>`z:<CR>

CommentsEdit

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


Hi Igor. "do not work for me" does not convey as much information as you might hope. What key did you press? What did you expect to happen? What did happen?

I am not aware of a system where Ctrl-j or Ctrl-k or Alt-j or Alt-k do not work, but you might have such a system. You could try the following as a simple check:

nnoremap <F2> m`:silent +g/\m^\s*$/d<CR>``:noh<CR>
nnoremap <F3> m`:silent -g/\m^\s*$/d<CR>``:noh<CR>
nnoremap <F4> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <F5> :set paste<CR>m`O<Esc>``:set nopaste<CR>

Pressing F4 or F5 should insert blank lines, and F2 or F3 should delete blank lines. These mappings definitely work. JohnBeckett 22:37, November 21, 2009 (UTC)


Thank you for your interest. After a better control, I realized that Ctrl-j and Ctrl-k work. But Alt-j and Alt-k do not work.

Unfortunately, these four suggested mappings are not working for me. Using or not these mappings, <F2> acts like B <F3> acts like C <F4> acts like D <F5> acts like E I got this effect on the server (that is using the .vimrc posted before) and on my pc as well. --IgorFobia 00:34, November 22, 2009 (UTC)


I tried with your .vimrc as you posted, and to my surprise the mappings seemed to have almost no effect. However, the simple addition of "set nocompatible" at the top of your .vimrc fixed it on Ubuntu Karmic, gvim 7.2.293. I had been led to believe that this would get set automatically with the presence of a .vimrc of any kind, but apparently this is not the case. Regardless, 'nocompatible' allows access to most of Vim's powerful features. If there is any "essential" Vim option, this one is it. If this does not fix it for you, I would recommend moving this discussion to the vim_use mailing list. --Fritzophrenic 01:31, November 22, 2009 (UTC)


I solved the problem. I had some problem with the Alt key. Instead of

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>

I put in my .vimrc these two lines,

nnoremap <silent>^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent>^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>

where ^[j is created pressing Crtl-v and then Alt-j, as shown in http://vim.wikia.com/index.php?title=Get_Alt_key_to_work_in_terminal&oldid=23162

--IgorFobia 10:04, November 30, 2009 (UTC)

gvim recognizes A-j and terminal vim doesn't; be careful ^[j is the same as escape, j - you shouldn't be using escape in the normal mode too much though
Advertisement | Your ad here

Around Wikia's network

Random Wiki