9

In vim, by pressing Ctrl+V you can enter Visual block mode. When I select multiple lines by moving around with h,j,k and l, and press delete using d or D, the text gets deleted, but the underlying lines remain there.

Which means although the text is gone, the surrounding code is still spaced out a lot.

Is there any way to delete the lines + the text?

1
  • Please paste the before and after text. It will give a clear picture what are you trying to do?
    – Thushi
    Nov 25, 2016 at 9:39

2 Answers 2

15

If you want to select lines use linewise Visual mode (V). If you want to select characters use character Visual mode (v). If you want to select a rectangular block use blockwise Visual mode (Ctrl-V).

Deleting a visual rectangular block does not delete the lines. This is the expected behavior. If you want to select lines visually and delete them, use linewise visual mode (V), select the lines with j and k (or down-arrow and up-arrow), then press d to delete them.

0

I use this function in an autocmd that before saving a file it removes spaces and empty lines.

" Removes trailing whitespace and blank lines leaving 1 blank line between functions.

function! TrimWhitespace()

    " skip these filetypes
    if (&ft=='markdown' || &ft=='help')
        return
    endif
    let l:save = winsaveview()
    " remove whitespace
    %s/\s\+$//e
    " remove blank lines
    %s/\(\s*\n\)\{3,}/\r\r/ge
    call winrestview(l:save)
endfunction

command! TWs call TrimWhitespace()

" this autocmd is called when any of the following files is saved

" edit filetypes as needed.

au BufWritePre *.c,*.sh,*.h,*.vim,*.vimrc,*.txt,*.local,.*rc,.*profile :TWs

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .