Eric Fortis
Eric Fortis

Reputation: 17360

vim sed match more than one newline and replace it with one newline

I'm having some trouble with vim, gg=G doesn't remove extra newlines, I'm trying with

:%s/\(\n\)\n\+/\1/g

but it's not working in the whole file. Any help appreciated.

Upvotes: 4

Views: 1416

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11830

" Put the function bellow in your vimrc
" remove extra newlines keeping the cursor position and search registers
fun! DelBlank()
   let _s=@/
   let l = line(".")
   let c = col(".")
   :g/^\n\{2,}/d
   let @/=_s
   call cursor(l, c)
endfun
" the function can be called with "leader" d see :h <leader>
map <special> <leader>d :keepjumps call DelBlank()<cr>

Upvotes: 0

Mike Pennington
Mike Pennington

Reputation: 43097

This should work in vim...

:g/^\s*$/d

Upvotes: 4

Related Questions