
Vim Text Objects : The Definitive Guide - rudyjahchan
http://blog.carbonfive.com/2011/10/17/vim-text-objects-the-definitive-guide/
======
stevelosh
Vim's text objects are one of the fundamental things that make using it so
awesome.

I've had a bit of code in my vimrc that creates "next/previous" text objects
for a while. Someone came along and helped me make it more compact a while
ago, but I can't remember who.

It lets you use "din(" for "delete in next parens (on the current line)",
which saves a bit of typing.

[https://bitbucket.org/sjl/dotfiles/src/1b6ffba66e9f/vim/.vim...](https://bitbucket.org/sjl/dotfiles/src/1b6ffba66e9f/vim/.vimrc#cl-1023)

Maybe I should turn it into a pathogen-installable plugin one of these days.

~~~
s3graham
Ah, I've loved this for ever! I didn't know other people did that too. :)

    
    
        noremap H ^
        noremap L $
    

Stealing your K-kill idea too!

~~~
rane
You don't find the default H/M/L that useful?

To be honest I never really got around using them but it might a nice addition
to my vim usage.

------
CamT
A couple of text object related plugins also worth checking out:

Easymotion - <https://github.com/Lokaltog/vim-easymotion>

Surround - <https://github.com/tpope/vim-surround>

~~~
esk
For those of us hacking in languages with significant whitespace, I made a
simple plugin for treating indent levels as text objects.

It's not _nearly_ as cool as Easymotion or Surround, but here it is if you're
interested:

<https://github.com/esk/aye-aye>

~~~
joakin
Thanks for this :) It's going to be really useful

------
beaumartinez
Don't forget Vim's built-in help and user manual:

    
    
        :h text-objects
        :h usr_04

~~~
sixtofour
Related, and more generally:

    
    
        :help object<CTRL-D>
        objects                 text-objects            vimwiki-text-objects
        object-select           sql-object-motions      sql-predefined-objects
        object-motions          text-objects-changed    +textobjects
        :help object
    

shows all help subjects with "object" in their name.

Then change the help query to whichever subject looks interesting.

------
dschobel
question about text objects:

given the following string and with the cursor over f:

    
    
         function(1,2,"3")
    

why does di" yield:

    
    
         function(1,2,"")
    

but di( has no effect unless you are within the parenthesis already?

Why doesn't di( when issued over the f in function yield the following?

    
    
         function()

~~~
cdavoren
It looks like the 'i(' motion has special meaning in Vim - it corresponds to
the _previous_ unmatched '(' to the next matched ')'. Type 'help motion.txt'
and look for i(.

Edit: Actually, the description for i" implies that its behaviour should be
the same. Which one works correctly, I wonder?

~~~
thristian
Since brackets come in matchable pairs and double quotes do not, and because
brackets can be nested while double quotes cannot, it wouldn't surprise me if
this were a deliberate difference. If the user hits "di(" on a line and
there's no "(" before the cursor, you can search backwards over previous lines
looking for an unmatched "(" without too much worry you'll get confused by a
stray "(" and delete something the user didn't expect. If the user hits 'di"'
on a line and there's no '"' before the cursor, searching any further is quite
likely to hit a false positive and do something ridiculous (like deleting all
the code between two string literals).

~~~
cdavoren
That's a fair interpretation. I'd be more comfortable if this difference were
reflected in the documentation - it could at least state that if no previous
quote is found, the next quote on the line will be used.

------
coliveira
Something related but not mentioned in the article is that lines are the
standard object in vim (and vi). The syntax is not the same, but the concept
is fully supported. For example, change the whole line = cc. Delete the whole
line = dd.

------
xutopia
I've been using vim for just a few months and this article is a revelation to
me.

~~~
sixtofour
The revelations never stop. :)

------
mwynholds
This stuff is nuts. I really need to get deeper in to vim.

------
morsch
Eclipse lets you expand your selection to ever increasing expressions in Java
and back again (Shift-Alt-Up/Down). Press up often enough, and you end up
selecting the entire outer class, and after that the entire file. Press down
and you get back right where you started.

It's super useful, particularly for understanding and refactoring old code. In
fact it makes refactoring very enjoyable, almost game like. It seems as if
that Eclipse feature is a pale shadow of vim text objects.

------
s3graham
I really wish there was "ac"/"ic" (or something) for comments. Especially for
a gq motion it would be really helpful.

------
xtacy
Are there such equivalents for emacs? What's the best one out there?

~~~
erikcw
I've been learning emacs and hunting for the same thing. Aside from Viper
mode, these two manual pages are the best I've found:

<http://www.gnu.org/s/libtool/manual/emacs/Expressions.html>

[http://www.gnu.org/software/libtool/manual/emacs/Moving-
by-P...](http://www.gnu.org/software/libtool/manual/emacs/Moving-by-
Parens.html)

It has helped, but I'm still wishing there were more equivalents. This just
seems to scratch the surface of what Vim can do in this area. If you find
anything else that is helpful for this sort of editing task, please post it
here...

~~~
nyellin
Do not use viper mode. Use evil, a much better emulation layer from the
authors of vim-mode and vimpulse. I also recommend evil-surround.

~~~
bostonvaulter2
Oh, that looks really interesting. It looks like it didn't exist last time I
was looking.

------
dbbo
Putting the article's content aside, how many "beginner's guides" to Vim do we
really need? It seems like every other week I find a new author writing about
basic vim commands. Now, considering the article's content-- I'm not sure it
contained anything that couldn't be learned from `vimtutor`, but it's been
quite a while since I've run it.

~~~
s00pcan
Pretty much nothing from this article is in vimtutor, as I learned using that
and don't recognize any of these commands. It teaches cw, dw, things like
that, but it doesn't go into depth like this article.

~~~
dbbo
You're right. I mistook vimtutor for the first chapter of a Vim book I have,
although I did make a point to indicate that I wasn't 100% sure in the OP.

