10

I'm using the a" motion rather often in vim; e.g. using ca" while refactoring a piece of code to replace a hardcoded argument with a variable name. The problem with this approach is that a" includes leading whitespace if there is no trailing whitespace:

Any trailing white space is included, unless there is none, then leading white space is included.

Thus, when I edit a function call like this:

aFunction(arg1, "toBeReplaced", arg3) #original
aFunction(arg1, replacedArg, arg3)    #refactored

I have to manually re-insert the space before replacedArg as ca" deletes it.

Is there any similar motion that does not include this white space, or is there anything else I can use instead of ca" that saves me from having to type an extra space?

Notes:

  • cf" does what I want as long as the string doesn't contain any escaped quotes, but requires me to have the cursor at the beginning of the string. I'd like something I can use from anywhere within the string, and ?"<Enter>cf" is rather awkward to type.
  • It's less about the single <Space> keystroke and more about the fact that I often initially forget to include the space, costing me at least four extra keystrokes (bi<Space><Esc>) and breaking my focus. Thus the length of the replacement command is not as important, as long as it deletes only the text between the given chars and then enters insert mode.
  • 1
    Instead of using ?"<Enter> to search for a quote backward on a line, you can use F". F is the same as f but searches to the left. The answers below look better for your question, but F is a useful command on its own. – Lithis Jul 8 '15 at 14:33
1

From https://superuser.com/questions/537453/vim-copy-quoted-string-without-whitespace : use 2i" instead of a". Also see :help i'.

  • Works like a charm! I've chosen to accept this answer over the others because it works with a plain vim without relying on plugins. – l4mpi Apr 4 '17 at 13:54
10

There is, in Wellle's targets.vim plugin. To quote the relevant part (inside the section "A Quote" ):

a' a" a

  • Select a quote.

  • This overrides Vim's default text object to support seeking.

  • Unlike Vim's quote text objects, this includes no surrounding whitespace.

  • It looks like this plugin overrides a", and uses A" for the original behaviour? – Martin Tournoij Jul 2 '15 at 12:53
  • ... while, I believe, also allowing seeking "to the left" – VanLaser Jul 2 '15 at 12:56
5

As an alternative you can use an argument/parameter text object. Now Vim does not have native argument text objects but there are a few plugins out there that do:

Personally I use my own plugin, argumentative.vim, however I have heard good things about both sideways.vim and targets.vim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.