9 ms·
To Master Vim, Use It Like Language
- allendoerfer 11y agoDoes work less well if you do not think in English.
- bdcravens 11y agoWouldn't the same apply to every programming language?
- setpatchaddress 11y agoKinda works in reverse for Ruby. I wish the Ruby maintainers had seen the need for proper English language documentation.
- barbs 11y ago> inoremap jk <ESC> Using two keystrokes instead of ESC to leave insert mode is definitely a personal preference and not necessarily more efficient or comfortable.
- octref 11y agoHaven't used ESC since finding out Ctrl-C.
- steve_b 11y agobonus is that Ctrl-C gets you out of pretty much any mode. Also, while in insert mode, using ctrl-h for backspace and ctrl-w for delete word are really nice.
- ThatGeoGuy 11y agoBetter yet, ^[ is actually ESC in vi/vim, and works without a config change. I started using this last year and haven't looked back. ^C works well, but I recall it having some limitations in certain environments (though for the life of me I can't recall what they were). In any case, I prefer ^[ to ^C, and hopefully you will too!
- mikerichards 11y agoouch...as others have said, CAPS LOCK is your friend
- nrivadeneira 11y agoWhy is caps lock your friend? I can't think of any situation where it'd be more useful than another key such as ESC or my personal preference, CTRL.
- 0942v8653 11y agoWay more useful (for me anyway) is mapping Capslock to ESC. It works with any vi anywhere as long as you are using your main machine to connect. Also with inoremap don't you have to wait for the letter j to show up until you type your next character? That would drive me crazy. Edit now that I have actually looked at the article: Yes, you can map it to Control but for me it's an awkward place to put a modifier. What if you want to do Ctrl-A? Ctrl-C? At least the way I type, I'm better off using the one in the corner.
- cryptonaut 11y agoWas just about to post this. Though I map Caps-Lock to Esc through xmodmap so it works everywhere instead of just in Vim.
- barbs 11y agoI'd actually like to map Caps-Lock to ESC as well, but I'd prefer to make it vim-specific rather than system-specific, and there's no way to do that :(.
- bonquesha99 11y agoI have a similar remapping of Caps: * When I tap Caps, it's `<esc>` * When I hold Caps OR hit it in combination with any other key, it's `<ctrl>` It really feels like the best of both worlds. I primarily develop on OSX, so I use https://pqrs.org/osx/karabiner/ https://pqrs.org/osx/karabiner/ to handle the remapping.
- jagger27 11y agoOh man I had no idea Karabiner could do that. Thanks!
- tremendo 11y agoThis works great on my Linux machine. On the Mac though I hate that all Mac apps use Command for well, all command-key combinations instead of Ctrl, so I map the Caps-lock to Command instead, but while Karabiner can do the dual "Esc on tap plus other on hold" for Ctrl, Shift and Option keys, it cannot frustratingly do it for the Command key. ITerm can switch caps-lock back to Ctrl, but then I lose the Esc double function. It's maddening.
- colordrops 11y agoI prefer to avoid remappings so that I can use vim at any machine without having to figure things out, so my preference for escape is Ctrl-[. It takes a bit of getting used to but once you get it, left hand never has to reach up anymore.
- coryfklein 11y agoI wish I could use this, but Dvorak moves that [ just a little too high for this to be practical. :(
- andrewstuart2 11y agoIt's definitely more efficient. I've had it mapped that way for years, and after a short time it basically became a single keystroke. The efficiency doesn't come from the fact that it's not as far to reach. The efficiency comes from the fact that you don't have to find home row with your left hand again and can continue your command sequence immediately.
- nrivadeneira 11y agoI actually use jj for the same thing. It's so much better than using ESC.
- DoggettCK 11y agoOne of the best things I've done, though I personally use 'jf', since they've both got little nubs on the keys, and I occasionally have to spell "Dijkstra".
- function_seven 11y agoSame here. I did both jk and kj. When I want to leave insert mode, I just mash those keys.
- Animats 11y agoVim is a regular expression system hooked to a 1970s user interface. If you need to apply regular expressions to text files, vim is useful. I used vi to write code for years, but that was decades ago. There's been some progress since the 1970s.
- scrollaway 11y agoThat's just not what vim is... I don't even really know where to begin, you're just flat out wrong. I'd say you're confusing vim with something else, but I don't even know what else fits that description.
- Animats 11y ago1,$s/a/b/
- colordrops 11y agoAll good editors support regular expressions.
- dllthomas 11y ago"Thbt" isn't even a word.
- vezzy-fnord 11y agoHe does have a point. Using vi derivatives does seem a tad spartan today, and I've personally never been a fan of vi, nor of emacs, but much of the community has this pseudo-elitist dichotomy set up around those two editor families. I'm not really a fan of what passes as IDEs, either, but I generally am not attached to any particular editor and hop around them. (The right answer to vi vs. emacs is, of course, Acme.)
- mkozlows 11y agoThere's nothing spartan about it. Vim has tons of plugins. Using it as a Javascript editor, I've got syntax highlighting, git integration, linting, code completion, and more.
- PeterWhittaker 11y agoGreat tutorial. About 20 years ago, I was a Level 4 user (by the article's definition), but that knowledge faded with disuse. When I switched back to a Linux/UNIX machine about a decade ago, I started using vim again (well, I started using vi again, but it was vim), and remembered a lot of Level 1 and 2 stuff, and could recall being able to do more, but never had the motivation to crawl through the man page to force myself to recall how I did more. This article is so logically laid out I think anyone could get to at least Level 3 with the article and a few hours diligent practice. Level 4 would then beckon.... When I became a serious sed - and then ed - user, I became a much better (faster, more productive) vi/vim user when I didn't need to be in visual mode all the time.
- blymphony 11y agoI consider myself at least an intermediate user of Vim, and I'm glad that you mentioned the "f" motion (along with its cousins "t" and ";"). I don't think these keys get enough exposure among beginner Vim users (they aren't mentioned in vimtutor), but they allow you to quickly and efficiently move across files and do things like "ct(" (change text up to first left parentheses), which is super useful for changing function names. Shameless plug: I wrote a plugin (https://github.com/unblevable/quick-scope https://github.com/unblevable/quick-scope) that facilitates the use of "f" and family that even beginners can take advantage of.
- 0942v8653 11y agoThe commands you mentioned along with "ci'", "ci[", and "ciW" to replace all text between delimiters have been some of my favorite to use. It really annoys me when my terminal's vi-mode doesn't support "ci" (I think it's vim-only).
- nitrogen 11y agoThe "ci" and "ca" commands are part of a Vim-only feature called Text Objects (type :help objects in Vim's command mode, or search the web). I like using yap and dap to copy/move paragraphs around when I'm editing markdown text.
- orodley 11y ago% is really useful for dealing with functions too. For example, deleting "foo(a + bar(x, y) + c)" by placing the cursor on the "f" and executing "d%".
- mdup 11y agoBy the way, in your specific example, "ct(" is not necessarily the most efficient, because most of the time a function name will be a nice alphanumeric string; hence you can use "ciw" which will work anywhere in the string, not only at the beginning.
- rajathagasthya 11y agoThanks for this tutorial. I'm at Level 1 having switched from an IDE (PyCharm) and I find this helpful. Any reason why you use Pathogen vs Vundle for managing plugins? Does it even matter?
- alaaibrahim 11y agoDepends on your needs, Pathogen only manages your plugins, but Vundle, will checkout the plugin, install it, and you can update all your plugins from inside vim. Edit: spelling
- mikerichards 11y agoYou're doing yourself a disservice. Intellij (PyCharm) has a vi plugin. Whatever extra stuff you give up from regular Vim, you gain an order of magnitude of productivity using a development environment that is "smart". Think about what PyCharm does for you and what Vim doesn't. Vi(m) is a model, not an implementation. I wish people would stop handicapping themselves with very bad implementations. Intellij, Visual Studio (Resharper) understands the languages your working in. Let the machine do what it does best and aid you in the language while you concentrate on domain.
- alaaibrahim 11y ago> Vi(m) is a model, not an implementation. I think you are talking about modal editing, vi is an application, vim is another application.
- mikerichards 11y agoYes, I understand the differences, thus the (m). The point I was trying to make (which irked somebody) is that the modal vi keybindings in modern IDEs is more important than Vi(m) itself.
- oneJob 11y agoIn that spirit, I find it hilarious that the entire industry proudly releases products weekly that do things like re-order laundry detergent at the press of a button or games that require the player to do nothing more than swipe their finger this way or that. On the other hand then there is this long raging religious war over the tools used to build these products where some claim the end all be all is this text only, esoteric, super high learning curve tool. Sure it's superpowerful once mastered, but as in so many other areas, the argument that you just made seems to carry the day: let the machine do the work. Also, Minority Report.
- DarkTree 11y agoMy issue with getting better at vim is that I've gotten to the point where I'm comfortable moving around text, copying/pasting, and everything I would normally do with a mouse. I know vim is so much more than that, but it's one of those things where "you don't know what you don't know" and because of that, I don't have the incentive to continue learning. Hopefully this post will inspire me to incrementally advance my vim usage.
- thesteamboat 11y agoAiming to combat "you don't know what you don't know", here's a list of things to consider. How comfortable are you with: * find/replace/regexes * registers and macros * vim settings (spell, list, highlight, number, wrap) * the plugin ecosystem * editing multiple files simultaneously (splits, buffers, moving between them) * folds Anyone else want to chime in with stuff I've either forgotten or don't know I don't know?
- base698 11y agoI asked on /r/vim about "Who uses folds?" Didn't get any takers. I always forget they exist and haven't used them in my workflow yet. Tag files would be another good one.
- colordrops 11y agoI use them. They don't help as much with new development, but when editing a large pre-existing code base it makes perusing the contents of a file much easier. It's like getting a table of contents for free if your code is cleanly organized.
- revscat 11y agoI don't use folds that often, but when I do they are be extremely helpful. When you run across that block of code that's way longer than it should be, and need to collapse it so you can see the context around it, folds can be very useful. Also for taking care of long comment blocks. So while I agree with you in that they aren't a part of my typical workflow, they are a piece of vim that I'm glad I familiarized myself with.
- PhasmaFelis 11y agoThe thing that always frustrated me about Vim (and command-line apps in general) is that it punishes you for being a GUI power user. The more mastery you have of conventional GUI keyboard shortcuts to highlight, jump words, copy/paste, etc., the more often you will reflexively hit them and do the wrong thing while trying to use Vim. You have to unlearn a lot of perfectly good skills before you can really master Vim, and that's frustrating. Of course it's not Vim's fault that the standards established by vi were not taken up by the early GUI systems, but I wish there was a better way to deal with this problem than a figurative grizzled old console cowboy going "suck it up, n00b, it builds character." I'm aware that people have made command sets that make Vim and the *nix console in general more GUI-standards-friendly, but these are never presented to actual CLI newbies, so by the time you're aware of them you've done half the work already...
- dghf 11y agoI have the opposite problem. When I'm writing stuff in MS Word at work, I'll often see a ":w" appear in the text when I automatically try to save the file Vim-style. Cream[0] is a configuration for Vim that makes it work in a way more familiar to people used to Word and other traditional Windows GUI programs. [0] http://cream.sourceforge.net/ http://cream.sourceforge.net/
- uxcn 11y agoThis is a really good introduction to vim. It is better to think of it as a grammar to manipulate text. A few additional things that have saved me keystrokes... replace mode (R) insert normal mode (<c-o>) paste from a register in insert mode (<c-r>reg) expression register (=) clipboard and active selection registers (+, * respectively) command ranges (e.g. 10,+5s/foo/bar/g) miscellaneous commands (:global, :argdo, :normal, etc...)
- melling 11y agoIs there a way to navigate words taking camel case into consideration? helloWorld should be treated as two words when I want to delete or move, at least sometimes when I want to edit.
- raindev 11y agoTake a look at https://github.com/bkad/CamelCaseMotion https://github.com/bkad/CamelCaseMotion
- nicholasferber 11y agoI am sure there is a 'normal' way to do it, but look into easy motion. I use space in normal mode as a shortcut to go to any word in the line and that includes camel case movement.
- melling 11y agoWhich plugins do people find valuable? EasyMotion was recently recommended to me: http://code.tutsplus.com/tutorials/vim-essential-plugin-easymotion--net-19223 http://code.tutsplus.com/tutorials/vim-essential-plugin-easy...
- thesteamboat 11y agoI like vim-surround[0] and vim-unimpaired[1]. [0]: https://github.com/tpope/vim-surround https://github.com/tpope/vim-surround [1]: https://github.com/tpope/vim-unimpaired https://github.com/tpope/vim-unimpaired
- zappo2938 11y agoVim Adventures is so much fun. http://vim-adventures.com/ http://vim-adventures.com/
- stevebmark 11y agoThis article is well written, but it has nothing to do with mastering Vim. It goes no further than running "vimtutor" at the command line. It has all the same tips and shortcuts as any other vanilla Vim tutorial. This is a common problem among Vim articles and tutorials. Authors title them as if it will give you Vim mastery, but instead it only offers the basics. Mastering Vim means going beyond vanilla movements and learning all of Vim's nasty warts to replace good GUI features. It means learning the wonky, dead Vimscript language. It means knowing how to use the arcane arglist. It means knowing how to go into insert-normal mode, and when to use the expression register. It means knowing everything about very magic search, and why default search is poorly designed. These are all randomly chosen examples, because mastering Vim involves mentally juggling a very large playbook of oddball features to complete editing tasks. The main resources to help you master Vim are the book "Practical Vim" by Drew Neil, and if you're ever in the unfortunate position of needing to write or edit a Vim plugin, "Learn Vimscript the Hard Way" is invaluable http://learnvimscriptthehardway.stevelosh.com/ http://learnvimscriptthehardway.stevelosh.com/
- melling 11y agoDrew Neil also has this worthwhile video: Vim - precision editing at the speed of thought https://vimeo.com/53144573 https://vimeo.com/53144573
- danielmiessler 11y agoArticle author here. The irony in your comment is that I recommend Drew's book in the primer, and Drew has recommended this primer as well. Maybe they're both good ways to learn Vim.
- stevebmark 11y agoThen title it "Vim Primer," which is accurate.
- coryfklein 11y agoThis is hardly a primer. If somebody is using the motions and actions described in the article they may not be a complete wizard in obscure vim features, but they are certainly far beyond being merely "primed" to use vim. I've been an avid vim proponent for years and I still picked up new things from this article.
- ridiculous_fish 11y agoLearning vim is easy, I've done it a thousand times! (As Mark Twain might say) This time around I've been keeping a list of the stuff I miss from my usual workflow. Here's two of the most maddening: 1. Searching for literal text. Is there a way to avoid having to manually escape special characters? 2. Maintaining cursor position when scrolling. Maybe I want to go look at another part of the file and come back. How can I scroll without it moving my cursor? As far as I can tell, I'm SOL on both counts.
- fphilipe 11y ago1. /\Vliteral search 2. C-e and C-y as long as the cursor position doesn't move out of the visible area
- deleted 11y ago[deleted]
- mdup 11y ago1. For the mnemonic, "\v" turns on "very magic" where all possibly special characters will have their special meaning without prefixing them with "\". Now, "\V" is just the opposite of "very magic", which is "very not magic", i.e. disable all special characters.
- jkn 11y ago2. Ctrl+o will bring you back to previous locations in many cases (e.g. after a search).
- tomsthumb 11y agoAssuming the stack grows up, ctrl+o moves you down the stack and ctrl-i moves you back up the stack. Pushing occurs on certain evens like searching and using marks. iirc, popping occurs when you push something onto the jump stack when you aren't at the top and the old <next> (ctrl-i) is replaced by the new current location.
- kqr 11y ago
- andhess 11y agoI have my interns setup Vim on their first day, and it's usually their editor of choice by the end of the summer. Apparently it gives them mad credit among their peers at school too.
- logn 11y agoFrom the article: "Arguably the most brilliant thing about vim is that as you use it you begin to think in it." Bill Joy, regarding vi: "It's like one of those pinatas--things that have candy inside but has layer after layer of paper mache on top. It doesn't really have a unified concept. I think if I were going to go back--I wouldn't go back, but start over again." Hunter S. Thompson, on an unrelated topic: "That was the fatal flaw [...] the desperate assumption that somebody--or at least some force--is tending the Light at the end of the tunnel." I think vim is a useful tool. It's available on most systems and is powerful. But trying to grok it and appreciate its UI isn't really something I want to do.
- bhagyas 11y agoThis has been one of my favourite articles on Vim for beginners. I'd recommend this to any beginner or a user of any other text editor. Great post.
- bootload 11y agofrom config: "I remap it to Ctrl at an operating system level. This way my left pinky can simply slide to the left by one key to execute Ctrl-whatever." Been a while since I used emacs, ctrl-everything is an emacs idiom [0] and a pinky killer. I guess the best bit is it's configurable. Really like this tute Daniel, found a nice add for my .vimrc already. [0] http://mally.stanford.edu/~sr/computing/emacs.html http://mally.stanford.edu/~sr/computing/emacs.html
- voidpointer 11y agoWhile a tweaked configuration can be a great productivity boost, it has a major drawback. My main reason for using vim is its ubiquitousness. Whenever I need to ssh into a box to check something out, I can trust that it's there. However, I cannot trust that its configuration has been tweaked like I expect. Hence, I stay away from getting used to non-standard key mappings.
- marklgr 11y agoChecking something out on some server does not require remapped keys or commands: you generally just browse some file and fix a couple of things. Tweaked configuration for more involved tasks is fine, to me.
- coryfklein 11y agoI think this follows the 90/10 rule. Most of my work is on my own machine. Occasionally I will be on a server and attempt to use some configuration from my personal .vimrc but instead have to fall back to slightly slower methods. The time lost in that 10% is totally worth the cost of the time saved in the 90%. And since everyone uses their text editors differently, it's not practical to move any one individual's personal configuration choice into vim core.
- enigmatic7 11y agoI agree that knowing basics of Vim is of great help and adds value to a programmer's skill set. I don't agree that it should be the only thing one uses for all development/writing tasks. Yes, vim is ubiquitous. However, no one uses vim without high levels of customization. So i think there is a bigger dependency issue here. Also, I've seen millennials struggling to cope with vim - it's like handing a cassette to a person who's grown up with an iPod. As long as the work gets done, an editor does not matter.
- hackatroll 11y agoWhat is a better editor VIM or emacs? Why?