Text-object for quoted strings
From Vim Tips Wiki
This tip is deprecated for the following reasons:
Vim 7 introduced all these text objects as built-in features.
Tip 956 Previous Next Created: July 2, 2005 Complexity: intermediate Author: Bheeshmar Redheendran Version: 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).
[edit] Comments
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>
Vim7 has this built-in.
Here is another possibility. It works for multi-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>
Very interesting mappings, and seems very useful, too.
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"
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 thinking?
Since you probably tested mapping before sending, is there a change it has semthing to to with the platform ?
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.
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", ...)
Yes, you are right, this seems like a good enhancement.
Another thing that can be useful is to make the abbreviation restore the value of the '/' register.
After all, the user does not excpect his search pattern to be altered.
