81

For example, in the following:

Testing "deleting" within quotes

With the cursor inside of deleting, how can I delete the text within the quotes and include the quotes, leaving:

Testing within quotes
1

3 Answers 3

141

You can use the following sequence to delete everything including the quotes:

da"

Keep in mind this only works on a single line, and will remove any trailing spaces after the last quote.

As pointed out by @James in the comments below, you can also use the delete inside sequence to delete the characters within the quotes without deleting the quotes:

di"

You can also use the change inside sequence to remove the characters and switch to insert mode, allowing you to easily replace text within quotes:

ci"
9
  • 9
    Fair enough, although I added this since it seems to be a common question for a beginner and it has not yet been asked or answered (although similar questions have been). It's typically easier to do a quick search than traverse the manual for a beginner (who might not know about Vim's :help). Feel free to edit the answer with your suggestions if you think that would be helpful.
    – blacktide
    Apr 25, 2016 at 14:37
  • 12
    Vim :help is only helpful if you already know what you are looking for. It is useful to have these operations documented somewhere where it is easy to find them.
    – Mad Wombat
    Apr 25, 2016 at 14:46
  • 12
    Added point: You can also do this without deleting the quotes with di". Very useful, especially with ci"
    – DJMcMayhem
    Apr 25, 2016 at 18:55
  • 4
    I know this is old, but comon. not everyone can find everything in the manual all the time. sometimes you just miss it. Also, Q&As like this make searching for answers easier. Jul 17, 2018 at 17:11
  • 1
    @NullVoxPopuli Yeah this answer was at the top of my google query which saved me the hassle. So it wasn't all for nothing. @blacktide Is it possible to get da" to work across multiple lines? Nov 24, 2019 at 9:34
39

We can use

da"

for your case or di" to delete ONLY the contents inside of ".

It is better to start with text-objects. Writing as an answer for completeness. Excerpt from :h text-objects, given below, suggest two forms i and a

This is a series of commands that can only be used while in Visual mode or after an operator. The commands that start with "a" select "a"n object including white space, the commands starting with "i" select an "inner" object without white space, or just the white space. Thus the "inner" commands always select less text than the "a" commands.

text-objects are useful to other character pair like (), {}, etc. For example, it is useful while changing

if ( i == true ) {
}

to

if (_) {
}

by using ci( or ci).

3
  • How to delete content in quotes start from current position? For example "abc|de", "|" is the position of the cursor, to delete the "de" only.
    – LCB
    Nov 30, 2018 at 7:40
  • It should be more than one word are enclosed in quotes, I just gave a bad example.
    – LCB
    Dec 2, 2018 at 12:24
  • 4
    How about dt"?
    – Max Coplan
    Sep 9, 2019 at 20:03
3

Keep your cursor in first quotes and press d, then type /"/e It should delete the content between both double quotes as well as quotes. This is applicable for muliple lines as well.

Another way: Keep the cursor in first quotes and type df" It will also delete the text inside quotes and quotes too! Not applicable for multiple lines.

In both the ways, you execute commands in normal mode.

1
  • 2
    These methods don't work when another double quotes is present in between these quotes.
    – SibiCoder
    Apr 26, 2016 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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