sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #956: Text-object for quoted strings

 tip karma   Rating 35/11, Viewed by 1456 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   July 2, 2005 13:26      complexity:   intermediate
author:   Bheeshmar Redheendran      as of Vim:   6.0

I really like the text-objects in Vim (:h text-objects), like ciw, or da>, but I really wanted one that would work on quoted sentences.  Here's what I came up with (add to your vimrc):

" Quote motions for operators: da" will delete a quoted string.
omap i" :normal vT"ot"<CR>
omap a" :normal vF"of"<CR>
omap i' :normal vT'ot'<CR>
omap a' :normal vF'of'<CR>

Now, with your cursor on the "a" of "you are here", you can type ci" and add new text between the quotation marks!  Note that it doesn't work for visual mode (vi" only puts you in visual mode).

 rate this tip  Life Changing Helpful Unfulfilling 

<< Switch between the two styles of Pan Shizhu's color scheme by a single key stroke | Preventing a second copy of VIM from starting >>

Additional Notes

bheeshmarNO@SPgmailAM.com, July 2, 2005 19:20
Just a little clarification:  These mappings only work on single-line quotes.  They will not work on quotes that span multiple lines...  putting ?"? and /"/ didn't work in the mapping for some reason...

The way the mappings work is by taking advantage of the fact that operator-pending mode (:h omap) can take a ":" command as a motion.  Further, if you want to change the default way motion works, the help suggests using Visual mode to clarify the selection (:h exclusive).

Putting these two behaviors together, I issue a command in normal mode to enter character-visual mode, search backward to the first quote, use "o" to change the visual selection "end" and extend the visual area to the end quote.
The mapping could be simplified (by one character) to:

:omap i" :normal vT"o,<CR>
heptite@yahoo.com, July 3, 2005 17:43
Just a note, Vim7 ALPHA has this built-in.
henry.vanroessel@ualberta.ca, July 10, 2005 14:49
Here is another possibility.  It works for muli-line quotes and also works in visual mode.  Note that the mappings involving "$" and "$$" are useful for "tex" files.

nmap vi' /'<cr>hvNl
nmap vi, /,<cr>hvNl
nmap vi" /"<cr>hvNl
nmap vi$ /\$<cr>hvNl
nmap vid /\$\$<cr>gevNw

nmap va` /`<cr>vN
nmap va' /'<cr>vN
nmap va, /,<cr>vN
nmap va" /"<cr>vN
nmap va$ /\$<cr>vN
nmap vad /\$\$<cr>lvNN

omap i` :normal vi`<cr>
omap i' :normal vi'<cr>
omap i, :normal vi,<cr>
omap i" :normal vi"<cr>
omap i$ :normal vi$<cr>
omap id :normal vid<cr>

omap a` :normal va`<cr>
omap a' :normal va'<cr>
omap a, :normal va,<cr>
omap a" :normal va"<cr>
omap a$ :normal va$<cr>
omap ad :normal vad<cr>
JonathanOrlev_REMOVEME_@yahoo.com, July 17, 2005 0:41
To Henry:

Very interesting mappings, and seems very useful, to.

But look at this mapping (for example):

nmap vi" /"<cr>hvNl

When I put the cursor between quotes, all the text _but_  the one in the right to the closing quote is selected.

For example:
Putting the cursor on the letter 'O' here:
"Jonathan Orlev"

and performing vi", selects "Jonathan Orle"

This of course also effect the on the Operator-pending mapping i"


Jonathan Orlev
JonathanOrlev_REMOVEME_@yahoo.com, July 17, 2005 8:40
To Henry, again:

I forgot to write:

It seems that the N movment command, when used with an operator, is not inclusive (it's exclusive).

Therefore, the 'h' movment should be removed. What do you thinkg ?

Since you probably tested mapping before sending, is there a change it has semthing to to with the platform ?

I use VIM 6.2 (and also 6.3) on Windows.


Jonathan Orlev
JonathanOrlev_REMOVEME_@yahoo.com, July 19, 2005 5:36
To Henry (and others):

I now know that this is a platform.
JonathanOrlev_REMOVEME_@yahoo.com, July 19, 2005 7:58
And hopefully the answer is (see previous remarks):

It seems that I found out where the "problem" is. It is not a bug in VIM or one of it's components.

In Vim for Window's _vimrc, they use the command 'behave mswin', which change, among other things, the option 'selection' to: 'exclusive'.

Use 'help :behave' and 'help 'selection'' for more information.

I don't know exactly why these options are used, but now I can use it to make the mappings work under Windows to.

emdot@seznam.cz, July 20, 2005 0:40
Thanks a lot for tips. Rated as lifechanging since I didn't even know about text-objects :)
Maping could be enhanced by deselecting e.g. quote mark.

nmap vi" /"<cr>hvNl<ESC>:noh<CR>gv

So all " are not selected after pressing vi" (or ci", ...)
JonathanOrlev_REMOVEME_@yahoo.com, July 20, 2005 3:06
Yes, you are right, this seems like a good enhancement.

Another thing that can be usefull is to make the abbreviation restore the value of the '/' register.

After all, the user does not excpect his search pattern to be altered.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo