Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: delete empty lines around cursor

Tags:

vim

Suppose I'm editing the following document (* = cursor):

Lions
Tigers


Kittens
Puppies


*


  Humans

What sequence can I use to delete the surrounding white space so that I'm left with:

Lions
Tigers


Kittens
Puppies
*
  Humans

Note: I'm looking for an answer that handles any number of empty lines, not just this exact case.

EDIT 1: Line numbers are unknown and I only want to effect the span my cursor is in.

EDIT 2: Edited example to show I need to preserve leading whitespace on edges

Thanks

like image 728
Dane O'Connor Avatar asked Mar 12 '13 18:03

Dane O'Connor


3 Answers

Easy. In normal mode, dipO<Esc> should do it.

Explanation:

  • dip on a blank line deletes it and all adjacent blank lines.
  • O<Esc> opens a new empty line, then goes back to normal mode.

Even more concise, cip<Esc> would roll these two steps into one, as suggested by @Lorkenpeist.

like image 131
glts Avatar answered Nov 12 '22 23:11

glts


A possible solution is to use the :join command with a range:

:?.?+1,/./-1join!

Explanation:

  • [range]join! will join together a [range] of lines. The ! means with out inserting any extra space.
  • The starting point is to search backwards to the first character then down 1 line, ?.?+1
  • As the 1 in +1 can be assumed this can be abbreviated ?.?+
  • The ending point is to search forwards to the next character then up 1 line, /./-1
  • Same as before the 1 can be assumed so, /./-
  • As we are using the same pattern only searching forward the pattern can be omitted. //-
  • The command :join can be shorted to just :j

Final shortened command:

:?.?+,//-j!

Here are some related commands that might be handy:

1) to delete all empty lines:

  • :g/^$/d
  • :v/./d

2) Squeeze all empty lines into just 1 empty line:

  • :v/./,//-j

For more help see:

:h :j
:h [range]
:h :g
:h :v
like image 7
Peter Rincker Avatar answered Nov 12 '22 23:11

Peter Rincker


Short Answer: ()V)kc<esc>

In normal mode, if you type () your cursor will move to the first blank line. ( moves the cursor to the beginning of the previous block of non-blank lines, and ) moves the cursor to the end (specifically, to the first blank line after said block). Then a simple d) will delete all text until the beginning of the next non-blank line. So the complete sequence is ()d).

EDIT: You're right, that deletes the whitespace at the beginning of the next non-blank line. Instead of d) try V)kd. V puts you in visual line mode, ) jumps to the first non-blank line (skipping the whitespace at the beginning of the line), k moves the cursor up one line. At this point you've selected all the blank lines, so d deletes the selection.

Finally, type O (capital O) followed by escape to crate a new blank line to replace the ones you deleted. Alternatively, replacing dO<Escape> with c<Escape> does the same thing with one less keystroke, so the entire sequence would be ()V)kc<Esc>.

like image 3
Lorkenpeist Avatar answered Nov 12 '22 23:11

Lorkenpeist