Whitespace hungry backspace/delete
From Vim Tips Wiki
created 2005 · complexity basic · author brailsmt · version 6.0
I think I figured out a good whitespace hungry delete.
imap <BS> <Esc>d?\S?e1<CR>i
It deletes from the cursor position backwards in the file until it finds the first non-whitespace character, and then changes the match end offset to avoid deleting that non-whitespace char.
Can you make one that will delete (or backspace) in tab-space chunks?
I use soft-tabs so it would be nice to be able to backspace over 4 whitespaces at a time (if they exist).
Unfortunately that doesn't faithfully reproduce <BS> under all circumstances. Try something like this (still not perfect).
function! HungryBackspaceWrapper() let column = col('.') if column == 1 return "\<Esc>kJxi" elseif column >= 2 && getline('.')[column - 2] =~ '\S' return "\<BS>" else return "\<Esc>d?\\S?e1\<CR>i" endif endfunction inoremap <silent> <BS> <c-r>=HungryBackSpaceWrapper()<CR>
Are you sure you don't want to
Yeah, that seems to only work if the indentations have been automatically inserted. Example, I can type 8 spaces at the beginning of a line and hit backspace, it will only delete one space.
Also, this will not work for removing "tabs" in the middle or end of lines.
Why not use
v - visual, ge - backward to the end of the Nth word l - move right d - delete.
It doesn't span blank lines.
set softtabstop=4 helps. Not sure whether set smarttab is also necessary to make bs delete 4 Spaces
It seems like it is.
Was looking for something like this, because whenever I paste code with the middle mouse button, the tab's get converted to 8x space's. Figured out a regex switch that works for me
Change "%" to "." to work only on the current line, may remove "g" if all your 8x spaces are at line beginnings.