126

Say I have this line of code:

$query = "SELECT * FROM table";

Is there a command in vi/vim which can instantly delete everything between quotes and position the cursor between them so I can start typing?

Nikola
  • 14,888
  • 21
  • 101
  • 165

6 Answers6

227

Use ci", which means: change what inside the double quotes.

You can also manipulate other text objects in a similar way, e.g.:

  • ci' - change inside the single quotes
  • ciw - change inside a word
  • ci( - change inside parentheses
  • dit - delete inside an HTML tag, etc.

More about different vim text objects here.

kabirbaidhya
  • 3,264
  • 3
  • 34
  • 59
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
29

You can select between quotes and then delete (d), change (c) etc. using

vi"

Similarly, you can substitute braces, brackets, XML elements etc. thus:

vi(
vi{
vit

or to simply change/delete, do the corresponding di", ci" etc. Substituting a for i will encompassing the surrounding elements (so you mark or change the brackets and contents, for example)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
6

An addition to Brian's answer, you can also p(paste) and y(yank) the new value, so if you want to replace the value inside quotes with another value, you could do yi" on the selection that you want to copy, vi" to select the area that you want to replace and then just p to properly replace the value.

Ulisses Caon
  • 430
  • 6
  • 11
5

I've made a plugin vim-textobj-quotes: https://github.com/beloglazov/vim-textobj-quotes

It provides text objects for the closest pairs of quotes of any type and supports quotes spanning multiple lines. Using only iq or aq it allows you to operate on the content of single ('), double ("), or back (`) quotes that currently surround the cursor, are in front of the cursor, or behind (in that order of preference). In other words, it jumps forward or backwards when needed to reach the quotes.

It's easier to understand by looking at examples (the cursor is shown with |):

  1. Before: foo '1, |2, 3' bar; after pressing diq: foo '|' bar
  2. Before: foo| '1, 2, 3' bar; after pressing diq: foo '|' bar
  3. Before: foo '1, 2, 3' |bar; after pressing diq: foo '|' bar
  4. Before: foo '1, |2, 3' bar; after pressing daq: foo | bar
  5. Before: foo| '1, 2, 3' bar; after pressing daq: foo | bar
  6. Before: foo '1, 2, 3' |bar; after pressing daq: foo | bar

The examples above are given for single quotes, the plugin works exactly the same way for double (") and back (`) quotes.

You can also use any other operators: ciq, diq, yiq, viq, etc.

Please have a look at the github page linked above for more details.

Anton Beloglazov
  • 4,939
  • 1
  • 21
  • 9
4

From already inside the quotes you can do

di"

Read it as delete inside "

Sam Peacey
  • 5,964
  • 1
  • 25
  • 27
  • 9
    The cursor is not required to be within the double-quoted string, as the command defaults to changing the first such string in the line – Eugene Yarmash Jul 24 '12 at 12:22
  • 1
    Thanks that's very good to know! Just goes to show I should read things a bit more carefully. :) – Sam Peacey Jul 25 '12 at 02:27
2

The chosen answer is suitable ONLY for ViM but NOT for vi. The question is inaccurate as well because the author did not mention what is initial position of the cursor. If we assume that the cursor is inside the double quotes then for vi the answer will be:

T"ct"

Where:

T" - move back just after the " character

c - change command

t" - provide end position for c command, where it should stop erasing characters, in other words the range to change

user1337
  • 175
  • 7