I created two functions in my ~/. vimrc
The first aims to execute commands without changing the position of the
cursor and search records, because when we locate consecutive blank lines in order to delete them this record is stored, that is, the Preserve function can be used in other situations.
The second is the function that will actually delete the blank lines.
if !exists('*Preserve')
function! Preserve(command)
" Preparation: save last search, and cursor position.
let save_cursor = getpos(".")
let old_query = getreg('/')
execute a:command
" Clean up: restore previous search history, and cursor position
call setpos('.', save_cursor)
call setreg('/', old_query)
endfunction
endif
" remove consecutive blank lines
" see Preserve function definition
fun! DelBlankLines()
keepjumps call Preserve("g/^\\n\\{2,}/d")
endfun
command! -nargs=0 DelBlank :call DelBlankLines()
If you wish you can even map to <leader>d
thus:
:nnoremap <leader>d :call DelBlankLines()
OBS: <leader>
by default is backslash but on many systems is set to "Comma" ,
.
I’ve tried a way now, but there’s certainly an easier way to do it. I was able to first remove all the blank lines
:g/^$/:delete
then positioned at the beginning of the archivegg
and recorded a macro with the content:qsA<CR>j<Esc>q
after saving the macro, I saw that the file was left with, for example 520 lines, so I applied the macro 520 times via command520@s
. The problem with this approach is that I must know the number of lines after removing the blank ones, i.e., is not effective.– Hans Zimermann