Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(5 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
|previous=1045
 
|previous=1045
 
|next=1048
 
|next=1048
|created=November 15, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=brailsmt
 
|author=brailsmt
|version=5.7
+
|version=6.0
 
|rating=1/9
 
|rating=1/9
 
|category1=
 
|category1=
Line 14: Line 14:
 
I think I figured out a good whitespace hungry delete.
 
I think I figured out a good whitespace hungry delete.
 
<pre>
 
<pre>
imap &lt;bs&gt; &lt;esc&gt;d?\S?e1&lt;cr&gt;i
+
imap <BS> <Esc>d?\S?e1<CR>i
 
</pre>
 
</pre>
   
Line 25: Line 25:
   
 
----
 
----
Unfortunately that doesn't faithfully reproduce &lt;bs&gt; under all circumstances. Try something like this (still not perfect).
+
Unfortunately that doesn't faithfully reproduce <BS> under all circumstances. Try something like this (still not perfect).
 
 
<pre>
 
<pre>
 
function! HungryBackspaceWrapper()
 
function! HungryBackspaceWrapper()
 
let column = col('.')
 
let column = col('.')
 
if column == 1
 
if column == 1
return "\&lt;esc&gt;kJxi"
+
return "\<Esc>kJxi"
elseif column &gt;= 2 &amp;&amp; getline('.')[column - 2] =~ '\S'
+
elseif column >= 2 && getline('.')[column - 2] =~ '\S'
return "\&lt;bs&gt;"
+
return "\<BS>"
 
else
 
else
return "\&lt;Esc&gt;d?\\S?e1\&lt;CR&gt;i"
+
return "\<Esc>d?\\S?e1\<CR>i"
 
endif
 
endif
 
endfunction
 
endfunction
inoremap &lt;silent&gt; &lt;bs&gt; &lt;c-r&gt;=HungryBackSpaceWrapper()&lt;cr&gt;
+
inoremap <silent> <BS> <c-r>=HungryBackSpaceWrapper()<CR>
 
</pre>
 
</pre>
   
 
----
 
----
Are you sure you don't want to <tt>set backspace=indent,eol,start</tt>
+
Are you sure you don't want to <code>set backspace=indent,eol,start</code>
   
 
----
 
----
Line 66: Line 65:
 
----
 
----
   
set softtabstop=4 helps. Not sure whether set smarttab is also nessessary to make bs delete 4 Spaces
+
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
  +
<pre> :%s/\s\{8\}/\t/g </pre>
  +
Change "%" to "." to work only on the current line, may remove "g" if all your 8x spaces are at line beginnings.
  +
:It would be better to use the <code>:retab</code> command. See [[Super retab]]. [[User:JohnBeckett|JohnBeckett]] 07:56, June 22, 2011 (UTC)

Latest revision as of 06:05, 13 July 2012

Tip 1046 Printable Monobook Previous Next

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.

Comments[]

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 set backspace=indent,eol,start


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

vgeld
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

 :%s/\s\{8\}/\t/g 

Change "%" to "." to work only on the current line, may remove "g" if all your 8x spaces are at line beginnings.

It would be better to use the :retab command. See Super retab. JohnBeckett 07:56, June 22, 2011 (UTC)