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.
||July 2, 2005 13:26
||as of Vim:
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).
<< Switch between the two styles of Pan Shizhu's color scheme by a single key stroke |
Preventing a second copy of VIM from starting >>
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>
July 3, 2005 17:43
|Just a note, Vim7 ALPHA has this built-in.
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>
July 17, 2005 0:41
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.
Putting the cursor on the letter 'O' here:
and performing vi", selects "Jonathan Orle"
This of course also effect the on the Operator-pending mapping i"
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.
July 19, 2005 5:36
|To Henry (and others):
I now know that this is a platform.
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.
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", ...)
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.