Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
__NOTOC__
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1066
 
|id=1066
 
|previous=1065
 
|previous=1065
|next=1067
+
|next=1070
|created=December 4, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Ulfalizer
 
|author=Ulfalizer
 
|version=6.0
 
|version=6.0
 
|rating=17/19
 
|rating=17/19
  +
|category1=Usage
  +
|category2=
 
}}
 
}}
  +
Starting in normal mode, you can press <code>O</code> to insert a blank line before the current line, or <code>o</code> to insert one after. <code>O</code> and <code>o</code> ("open") also switch to insert mode so you can start typing.
When programming, I often find myself adding and deleting empty lines. The way I used to do it was to press o (or O) to open a new line, press Enter if I wanted additional lines, and then press Esc to exit insert mode. Often I wanted to find my way back to where I began the insertion, and so had to do a few <tt>k</tt> or <tt>j movements, or use <tt>'[</tt> or <tt>']</tt>. This was getting cumbersome, and so I came up with another mechanism.
 
   
  +
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.
The following remaps C-j to add empty lines below the cursor and C-k to remove empty lines below the cursor (you'll find these are intuitive "up/down" operations). A-k and A-j do the same thing above the cursor. Lines containing something other than whitespace are not deleted, and so the delete operations can be "hammered on" without risk. The cursor maintains its position in the window if possible. You should probably set scrolloffset to something other than 0 for optimum comfort.
 
   
  +
Put the following mappings in your [[vimrc]]:
 
<pre>
  +
" 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>
 
</pre>
  +
  +
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====
  +
*<code>m`</code> sets the context mark to the current cursor position.
  +
*<code>``</code> jumps to the context mark to restore the cursor position.
  +
*The <code>g/^\s*$/</code> commands search for a line matching <code>^</code> (begin line), then zero or more occurrences of whitespace, then <code>$</code> (end line), that is, blank lines.
 
*<code>\m</code> sets the 'magic' option so the pattern will work regardless of the current 'magic' option.
  +
*<code>+g/pattern/d</code> executes <code>d</code> (delete) on lines matching ''pattern'' in the range <code>+</code> (a single line after the current line).
  +
*<code>:noh</code> turns off any search highlighting. {{help|:nohlsearch}}
  +
*<code>:set paste</code> sets Paste mode to temporarily switch off auto indenting so program comments won't be inserted (for example, in a cpp file, <code>o</code> on a <code>//comment</code> may insert <code>//</code> on the next line).
  +
  +
==See also==
  +
*[[VimTip72|Remove unwanted empty lines]] to delete all blank lines.
  +
  +
==Alternative==
  +
{{Todo}}
  +
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.
 
<pre>
 
<pre>
 
function! AddEmptyLineBelow()
 
function! AddEmptyLineBelow()
Line 20: Line 54:
   
 
function! AddEmptyLineAbove()
 
function! AddEmptyLineAbove()
let l:scrolloffsave = &amp;scrolloff
+
let l:scrolloffsave = &scrolloff
" Avoid jerky scrolling with ^E at top
+
" Avoid jerky scrolling with ^E at top of window
" of window
 
 
set scrolloff=0
 
set scrolloff=0
 
call append(line(".") - 1, "")
 
call append(line(".") - 1, "")
 
if winline() != winheight(0)
 
if winline() != winheight(0)
silent normal! ^E
+
silent normal! <C-e>
 
end
 
end
let &amp;scrolloff = l:scrolloffsave
+
let &scrolloff = l:scrolloffsave
 
endfunction
 
endfunction
   
Line 52: Line 85:
 
let l:colsave = col(".")
 
let l:colsave = col(".")
 
.-1d
 
.-1d
silent normal! ^Y
+
silent normal! <C-y>
 
call cursor(line("."), l:colsave)
 
call cursor(line("."), l:colsave)
 
end
 
end
 
endfunction
 
endfunction
   
noremap &lt;silent&gt; &lt;C-j&gt; :call AddEmptyLineBelow()&lt;CR&gt;
+
noremap <silent> <C-j> :call DelEmptyLineBelow()<CR>
noremap &lt;silent&gt; &lt;C-k&gt; :call DelEmptyLineBelow()&lt;CR&gt;
+
noremap <silent> <C-k> :call DelEmptyLineAbove()<CR>
noremap &lt;silent&gt; &lt;A-j&gt; :call DelEmptyLineAbove()&lt;CR&gt;
+
noremap <silent> <A-j> :call AddEmptyLineBelow()<CR>
noremap &lt;silent&gt; &lt;A-k&gt; :call AddEmptyLineAbove()&lt;CR&gt;
+
noremap <silent> <A-k> :call AddEmptyLineAbove()<CR>
 
</pre>
 
</pre>
   
  +
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:
Note that ^E and ^Y should be the real things and not entered with ^ and E/Y as separate characters. Press C-V C-E, C-V C-Y to insert them (on Windows, use C-Q rather than C-V).
 
 
==Comments==
 
If you don't mind the safety error messages, you can do this without any functions:
 
 
 
<pre>
 
<pre>
  +
" Ctrl-]/[ deletes line below/above, and Alt-]/[ inserts.
noremap &lt;silent&gt;&lt;C-j&gt; mzo&lt;Esc&gt;`z
 
  +
nnoremap <c-]> mzj:s/^\\s*\\n//i<CR>`z:noh<CR>
noremap &lt;silent&gt;&lt;C-k&gt; mz:+g/^$/d&lt;CR&gt;`z
 
  +
nnoremap <c-[> mzk:s/^\\s*\\n//i<CR>`z:noh<CR>
noremap &lt;silent&gt;&lt;A-j&gt; mz:-g/^$/d&lt;CR&gt;`z
 
  +
nnoremap <a-]> :<CR>mzo<Esc>`z:<CR>
noremap &lt;silent&gt;&lt;A-k&gt; mzO&lt;Esc&gt;`z
 
  +
nnoremap <a-[> :<CR>mzO<Esc>`z:<CR>
 
</pre>
 
</pre>
 
==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?
The error messages can be avoided by prepending <tt>:silent</tt>:
 
  +
:*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?
  +
:--[[User:Fritzophrenic|Fritzophrenic]] 19:00, November 19, 2009 (UTC)
  +
  +
Sorry, it is my first time...
  +
Here is my .vimrc
 
<pre>
 
<pre>
  +
source $VIMRUNTIME/macros/matchit.vim "script for matlab
noremap &lt;silent&gt;&lt;C-j&gt; mzo&lt;Esc&gt;`z
 
noremap &lt;silent&gt;&lt;C-k&gt; mz:silent +g/^$/d&lt;CR&gt;`z
 
noremap &lt;silent&gt;&lt;A-j&gt; mz:silent -g/^$/d&lt;CR&gt;`z
 
noremap &lt;silent&gt;&lt;A-k&gt; mzO&lt;Esc&gt;`z
 
</pre>
 
   
  +
:filetype plugin on
I had no idea Vim remembered column positions for marks. That's pretty handy.
 
  +
: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
One drawback (which might also be considered a feature) with these bindings is that using C-j when the cursor is on i.e. a C++ // comment will make Vim insert another // on the next line, which unfortunately C-k will then not be able to delete.
 
  +
" Ctrl-j/k deletes blank line below/above, and Alt-j/k inserts.
 
Another drawback is that A-j and A-k will not preserve the cursor's position in the window.
 
   
 
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>
 
</pre>
  +
These nnoremap commands suggested before do not work for me...
  +
--[[User:IgorFobia|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?
The following version deletes lines containing only whitespace as well, but has the same minor problems:
 
   
  +
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:
 
<pre>
 
<pre>
  +
nnoremap <F2> m`:silent +g/\m^\s*$/d<CR>``:noh<CR>
noremap &lt;silent&gt;&lt;C-j&gt; mzo&lt;Esc&gt;`z
 
noremap &lt;silent&gt;&lt;C-k&gt; mz:silent +g/\m^\s*$/d&lt;CR&gt;`z
+
nnoremap <F3> m`:silent -g/\m^\s*$/d<CR>``:noh<CR>
  +
nnoremap <F4> :set paste<CR>m`o<Esc>``:set nopaste<CR>
noremap &lt;silent&gt;&lt;A-j&gt; mz:silent -g/\m^\s*$/d&lt;CR&gt;`z
 
  +
nnoremap <F5> :set paste<CR>m`O<Esc>``:set nopaste<CR>
noremap &lt;silent&gt;&lt;A-k&gt; mzO&lt;Esc&gt;`z
 
 
</pre>
 
</pre>
   
  +
Pressing F4 or F5 should insert blank lines, and F2 or F3 should delete blank lines. These mappings definitely work. [[User:JohnBeckett|JohnBeckett]] 22:37, November 21, 2009 (UTC)
\m turns on the 'magic' option for the rest of the pattern so that it will work regardless of what the user has set 'magic' to.
 
 
----
  +
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.
  +
--[[User:IgorFobia|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 [http://groups.google.com/group/vim_use vim_use mailing list]. --[[User:Fritzophrenic|Fritzophrenic]] 01:31, November 22, 2009 (UTC)
The mappings provided only maintain the cursor position in the buffer. To maintain the cursor position in the window, do this slight modification:
 
 
----
 
  +
I solved the problem. I had some problem with the Alt key.
  +
Instead of
 
<pre>
 
<pre>
  +
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
noremap &lt;silent&gt;&lt;A-k&gt; mzO&lt;Esc&gt;&lt;C-e&gt;`z
 
  +
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
 
</pre>
 
</pre>
  +
I put in my .vimrc these two lines,
 
It's hard to maintain the cursor position in the window for &lt;A-j&gt; due to the inherent deletion of the line before the current one.
 
 
I was unable to replicate the "&lt;C-j&gt; when cursor is on a C++ // comment" behavior.
 
 
----
 
To avoid the "comment" anomaly you can use the following mappings:
 
 
 
<pre>
 
<pre>
noremap &lt;silent&gt;&lt;C-j&gt; :set paste&lt;CR&gt;mzo&lt;Esc&gt;`z:set nopaste&lt;CR&gt;
+
nnoremap <silent>^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
noremap &lt;silent&gt;&lt;A-k&gt; :set paste&lt;CR&gt;mzO&lt;Esc&gt;`z:set nopaste&lt;CR&gt;
+
nnoremap <silent>^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>
 
</pre>
 
</pre>
  +
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
   
  +
--[[User:IgorFobia|IgorFobia]] 10:04, November 30, 2009 (UTC)
Also if you use the hlsearch option you'll probably want the following to hide
 
the highlight of empty lines:
 
   
  +
: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
<pre>
 
noremap &lt;silent&gt;&lt;C-k&gt; mz:silent +g/\m^\s*$/d&lt;CR&gt;`z:noh&lt;CR&gt;
 
noremap &lt;silent&gt;&lt;A-j&gt; mz:silent -g/\m^\s*$/d&lt;CR&gt;`z:noh&lt;CR&gt;
 
</pre>
 
 
----
 

Revision as of 06:06, 13 July 2012

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.

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>

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>

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