Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim actions like "da" that don't remove whitespace

Tags:

vim

I have some text like this:

foo<space><space><space>'bar'

When I do da' inside 'bar', the whitespace is removed:

foo

How can I do da' without removing whitespace, so I get this as the result?

foo<space><space><space>
like image 858
Xyzzy Avatar asked Sep 17 '25 09:09

Xyzzy


2 Answers

I found this in vim's help topic on text-objects:

i"                          *v_iquote* *iquote*
i'                          *v_i'* *i'*
i`                          *v_i`* *i`*
            Like a", a' and a`, but exclude the quotes and
            repeating won't extend the Visual selection.
            Special case: With a count of 2 the quotes are
            included, but no extra white space as with a"/a'/a`.

Thus, doing the below would not delete the whitespace.

d2i'

For more help on text objects, do :help text-objects

like image 81
EvergreenTree Avatar answered Sep 19 '25 06:09

EvergreenTree


This is covered in :help v_i':

For the "a" commands: The operator applies to the object and the white space after the object. If there is no white space after the object or when the cursor was in the white space before the object, the white space before the object is included.

So, the problem is that you're at the end of the line. To change the behavior, you can add a whitespace, go back, and then issue the command: A<Space><Esc>hda'.

Alternatively, use the inner object, and remove the two quotes afterwards, as in di'hxx.

For the quote text objects, there's even a handy special case, so d2i' looks like the winner here:

  Special case: With a count of 2 the quotes are
  included, but no extra white space as with a"/a'/a`.
like image 27
Ingo Karkat Avatar answered Sep 19 '25 06:09

Ingo Karkat