9 ms·
Vim Text Objects : The Definitive Guide
- mwynholds 15y agoThis stuff is nuts. I really need to get deeper in to vim.
- xutopia 15y agoI've been using vim for just a few months and this article is a revelation to me.
- sixtofour 15y agoThe revelations never stop. :)
- city41 15y agoinner words were the turning point for me with vim. The first time I did ci" I knew I had found a lifelong partner :)
- samvelst 15y agoText objects along with movement by search were the two things that propelled my editing capabilities with Vim the most so far.
- xtacy 15y agoAre there such equivalents for emacs? What's the best one out there?
- erikcw 15y agoI'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/s/libtool/manual/emacs/Expressions.html http://www.gnu.org/software/libtool/manual/emacs/Moving-by-Parens.html http://www.gnu.org/software/libtool/manual/emacs/Moving-by-P... 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...
- briantrice 15y agoI don't understand. Emacs blows vim away here. Of course, obviously both emacs and vim users are unaware of these symbolic modes. But Emacs modes are designed to have this idea of customizing the word, sentence, paragraph, and symbolic expression per language/syntax. And using the mark and region and operating on them with commands is exactly what's described in this vim article. That said, I didn't know that vim could do these things, either, because the default vim objects are lines, which are hideously crude and turned me off.
- nyellin 15y agoDo 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 15y agoOh, that looks really interesting. It looks like it didn't exist last time I was looking.
- agumonkey 15y agoI'm not an emacs guru, but it feels especially sad to see that emacs grammar/syntax aware (emacs does a lot in this department) structures aren't used to provide this kind of operations by default (of course with some elisp-fu it's doable). The combinator orientation of vim commands is just plain cute.
- coliveira 15y agoSomething 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.
- CamT 15y agoA couple of text object related plugins also worth checking out: Easymotion - https://github.com/Lokaltog/vim-easymotion https://github.com/Lokaltog/vim-easymotion Surround - https://github.com/tpope/vim-surround https://github.com/tpope/vim-surround
- mcrittenden 15y agoThanks for that - this is the first time I've come across easymotion and I can already tell it's going to be used heavily. Great plugin. Highly recommended to anyone not familiar with it.
- esk 15y agoFor 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 https://github.com/esk/aye-aye
- joakin 15y agoThanks for this :) It's going to be really useful
- beaumartinez 15y agoDon't forget Vim's built-in help and user manual: :h text-objects :h usr_04
- sixtofour 15y agoRelated, 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.
- stevelosh 15y agoVim'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/.vimrc#cl-1023 https://bitbucket.org/sjl/dotfiles/src/1b6ffba66e9f/vim/.vim... Maybe I should turn it into a pathogen-installable plugin one of these days.
- solutionyogi 15y agoWow. That's a huge vimrc config (1350 lines!) filled with gems, I am going to go through it and steal them for my vimrc! :)
- s3graham 15y agoAh, 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 15y agoYou 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.
- crazydiamond 15y agoNot clear what this does: " Stop it, hash key. inoremap # X<BS># I tried it too. It's in insert mode, so it types an X, backspaces and then types a '#'.
- looklookatme 15y agoI've got something similar in mine; The use-case is to allow indented comments (in languages where '#' is the comment delimiter) Normally, when using auto-indent/smart-indent, pressing '#' on a blank line will change the column to zero, deleting any existing whitespace. By typing a character and deleting it, you override this behavior, allowing you to have comments at the same indentation level as the rest of the code.
- dbbo 15y agoPutting 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 15y agoPretty 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 15y agoYou'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.
- bsergean 15y agovimtutor was very bare-bone in my memories, and wasn't going through much details on text objects. This was a rather good article, text objects are awesome.
- johncoltrane 15y agoI'm not the author. My guess is that it's a combination of a) a lot of people are learning Vim right now and b) a lot of these people have blogs. Coming to Vim from some other editor is an awesome experience for them and they want to share. I know: I've been using Vim daily since one year now (coming from TextMate) and I've posted 20+ random tricks as I learned them. I'm currently drafting my "one year with Vim" post.
- LVB 15y agoIf one person (even the author) learns one thing from any of those beginners guides, then it was worth it.
- dschobel 15y agoquestion 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()
- deleted 15y ago[deleted]
- cdavoren 15y agoIt 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 15y agoSince 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 15y agoThat'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.
- s3graham 15y agoI really wish there was "ac"/"ic" (or something) for comments. Especially for a gq motion it would be really helpful.
- morsch 15y agoEclipse 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.