0

Is there a simple way to delete all text except newlines ?

I found one that removes all empty lines :g/^$/d.

but I am looking at the opposite functionality, some command that deletes everything except newlines ?

If you are wondering why, it's mainly because once you delete everything, you have to again press i and <Enter> a couple of times to recreate the space you are working with.

sourcevault
  • 101
  • 2

2 Answers2

3

To remove all content of a line, you can use the :substitute command (see :help substitute) like this:

:s/.*//

It will substitute any character (.), repeated 0 or more times (*) with nothing. It will preserve line endings.

You can prepend :substitute with a range, e.g. the whole buffer (%) or a visual selection.

As an alternative to get several empty lines, you can prefix i or o with a count. Both 23i<CR><Esc> or 23o<Esc> will create 23 empty lines.

Friedrich
  • 1,846
  • 1
  • 11
  • 21
2

I am not sure why you want to do it, but here we go:

:%norm! D

This keeps the newlines, but makes every line empty.

Or if you want to delete the whole buffer completely, including the line breaks:

:%d

Or if you want to only delete non-empty lines (you can use other patterns like all lines starting with a #, so commented lines: ^#):

:g/./d
Christian Brabandt
  • 25,820
  • 1
  • 52
  • 77
  • Might be worth mentioning :v, as in :v/^$/d, since OP had a command that almost worked but wanted to, well, invert the pattern – D. Ben Knoble May 17 '23 at 16:48