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?
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?
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 quotesciw
- change inside a wordci(
- change inside parenthesesdit
- delete inside an HTML tag, etc.More about different vim text objects here.
From already inside the quotes you can do
Read it as delete inside "
I've made a plugin
vim-textobj-quotes
: https://github.com/beloglazov/vim-textobj-quotesIt provides text objects for the closest pairs of quotes of any type and supports quotes spanning multiple lines. Using only
iq
oraq
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
|
):foo '1, |2, 3' bar
; after pressingdiq
:foo '|' bar
foo| '1, 2, 3' bar
; after pressingdiq
:foo '|' bar
foo '1, 2, 3' |bar
; after pressingdiq
:foo '|' bar
foo '1, |2, 3' bar
; after pressingdaq
:foo | bar
foo| '1, 2, 3' bar
; after pressingdaq
:foo | bar
foo '1, 2, 3' |bar
; after pressingdaq
: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.
You can select between quotes and then delete (
d
), change (c
) etc. usingSimilarly, you can substitute braces, brackets, XML elements etc. thus:
or to simply change/delete, do the corresponding
di"
,ci"
etc. Substitutinga
fori
will encompassing the surrounding elements (so you mark or change the brackets and contents, for example)An addition to Brian's answer, you can also
p
(paste) andy
(yank) the new value, so if you want to replace the value inside quotes with another value, you could doyi"
on the selection that you want to copy,vi"
to select the area that you want to replace and then justp
to properly replace the value.