Hacker News new | ask | show | jobs
by maligree 3173 days ago
I've had a completely different approach to learning vim, which I wholeheartedly recommend:

- learn the bare minimum: up, down, left, right, deleting words and lines, stuff you do everyday

- DON'T just look for tutorials learn stuff out in the void, you'll waste time since you'll end up forgetting 95% of it. don't distract yourself. Screw four week plans, the basics you can get in an hour.

- work. write code/stuff. whenever you catch yourself pressing a button > 3 times (you know, left-left-left-left or something like that) THEN look for a better solution. you'll discover plenty ways to navigate around the code you're working with. find a new way, stop looking, start using it.

- happy? no pain points? good. keep using vim, wait until you find something you'd like to improve. look for a solution.

The point is... just because vim can do something, doesn't mean you need to know it. It doesn't mean you need to use it. It's a tool, it's optional.

The other point I guess is... stop worrying so much about if you're using something "right". Are you happy with your workflow? Good! That's what we're aiming for.

These plans and intros and guides... it's almost like they're meant to overwhelm. It's a damn text editor, you can use 4 commands and be happy. There are no rewards and no raises for "knowing" 60%+ of vim's feature set. And after the basic what, 20%? it's diminishing returns anyway.

6 comments

I agree whole-heartedly. I'm far from proficient, but my vim skills did increase dramatically when I just started using it. A lot of that was driven by getting a Digital Ocean droplet and not having many other choices. I could've used nano, but I figured it was time for something new. I mainly use it for editing config files and the like, but it's been a good experience.

If anyone's interested in learning vim, I'd suggest just using it anywhere you'd otherwise use nano. You don't have to jump in head-first and use it like an IDE for coding or anything.

One thing I don't get, how do you navigate with hjkl when you're in insert mode?

I've switched to neovim as my main editor months ago. I like it, but I still use the arrows to navigate, or sometimes the `w` to jump words and `10j` to go 10 lines down for example.

But I go in and out of insert mode all the time, and hjkl is not useful at all there. So I never use hjkl to navigate. Never. So how come most people use those as the main navigation?

Am I missing something?

You don't navigate in insert mode. This is why its important to have some alternative to esc. Personally I use alt-anyKey to exit insert mode and do that key command in normal mode. In some terminals, this will be the basic function. Using gvim or a more modern terminal will require manual rebinding.

jk are usefull for navigating lines, especially with 'dd' 'yy' and 'p'. hl are for navigating either within a word, or when dealing with a syntactic mess where the standard movements aren't very useful. Outside that, bwe and their capital variants are great. In text, so are ( and ).

Beyond that, I'd look into text objects. Things like ciw ca) or ci] are great for just doing what you want to do. (Change the word below the cursor, change an entire () expression, or change the inside of a [ expression).

> You don't navigate in insert mode. This is why its important to have some alternative to esc.

There's one that's already built in: ctrl-[.

I found remapping Esc to ‘jk’ (as a right-hander) to be solid gold. Index-middle fingers. Practically instantaneous: they’re already on the keys if you’re doing it right.
But don't do that when you plan to use Vim to write in Dutch. I've also tried 'jk' for a while and it worked great until I started writing some texts in my native language: 'ijk' appears to be a pretty common ending for a lot of Dutch words :)

These days I've mapped CAPSLOCk to ESC and super happy with it.

Yup, this mapping is pure gold. I also mapped jj in insert to save (and put me back into insert again). I'm not as good at remembering to use it though.
You simply don't navigate in insert mode. Inserts are meant to be small most of the time. They are also the unit for undoing, so it's sometimes bad to have huge insertions.
Actually, they're not unit. When you type "abcd", then (without exiting insert mode) move left twice and type "xx" (resulting in "abxxcd"), then undoing once will leave you at "abcd".
So most people re-bind Esc to something else. Then why is Esc the default "Esc" in vim? Shouldn't it be something else?

And for those who use caps-lock, how do you deal with getting uppercase letters depending on how many times you pressed caps-lock? Sounds impractical to me. Every "Esc" you hit will change to uppercase/lowercase. Right?

You rebind caps lock at the window manager level, not at the vim level. This disables it completely. This is actually a good idea whether or not you use it for escape. Hitting caps lock by mistake while in normal mode will make nearly every command do something different, sometimes only slightly, sometimes in destructive ways.
Many people rebind caps lock (or another easy to reach key) with escape. This way they can quickly change modes.

Also, most beginners use insert mode too much. If you find yourself going into insert mode just to change a letter or word, try learning shortcuts instead (r for replace, ciw for replace word, diw for delete word, and other neat command combinations).

That was my initial thought. I do use r to replace one letter, and ciw or ci" to change whole things. But still, after all this time, I find it simpler to just go in insert mode and do 3-4 things, than think of combinations or commands for every single thing.

Maybe I'm too lazy to memorize them, I don't know.

But using hjkl is still too weird for me, even after using vim for more than 6 months. :)

I don't usually have this problem. On a dev machine I install a more featurefull vim. I think there's a difference in how "tiny vim" most distros ship with and other vims handle navigation. Seems like tiny vim is closer to vi in enforcing hjkl in insert. I'm sure some reading this knows more than I do about this.
> But using hjkl is still too weird for me

It's very natural to use j and k. When you touch type, "j" is your right index home base (I assume you know it but that's why there's a physical marker on "j" and "f").

I mean from a layout perspective. hjkl is in one line, whereas the arrows are not in one line, with "up" one line above "down". That's a muscle memory I cannot fight yet.
One of my favorites: xp to exchange characters.

x cuts the character under the cursor and p pastes it after the cursor. Net effect is exchanging the character on the cursor with the one to the right of the cursor.

You can do the equivalent lines with:

    ddp
dd deletes a line and p pastes the deleted line below the current line. Since your cursor ends up in the line after the line you just deleted, this ends up effectively exchanging the lines.
I think what you're looking for is Ctrl+O, which allows you to exit insert mode, insert ONE command only (e.g. 4k), and immediately enter the insert mode again
you can still do ctrl-p ctrl-n to go up and down. but these are a bit limited.

it is a little painful when you have to do many minor text changes in quick succession.

and ultimately, this is what made me just go straight to emacs and i have not looked back since.

emacs may take some use to getting started, esp if you are on osx (rebind command as meta) but it gives you the best of both worlds. the choices are unlimited and you get to program your own editor you want it to be.

Repeating the previous command (.) handles most of the cases where I would do multiple small edits. Search and replace (especially with backreferences, \zs and \ze) handles most of the rest, and macros handle pathological cases. There's almost never a call for doing multiple small edits by hand.
Usually you don't. Going in and out of insert mode is the right way to do it since it allows you to use the much more powerful movement and manipulation commands available in command mode. Map escape to jk or something and move between modes with ease!
Normal mode is where you should be most of the time. Insert mode is something you go in and out of. Most people remap escape onto caps lock (OS level), use jj or jk, or use ctrl-[ (works well with OS level remapping of Ctrl onto cap-lock).
In Spacemacs (a vim emulation mode in emacs), if you type "fd" really quickly in insert mode it puts you back into command mode and then you can just use hjkl to navigate.

I assume one can set up something similar for real vim.

I had good success by starting with the basics but also learning up front what every unshifted and shifted key on the keyboard does by looking at a cheat sheet. Then review every so often, picking a few to try to remember. Just knowing what is possible is useful, and the more keys you know the more precise your edits can be. Nearly every key on the keyboard is useful in Vim.
While I would agree with this to get started, but it's always a good idea to be aware of possibilities beforehand and skim though them once so one knows that the solution exists for the problem being faced, what the solution looks like and where to look for it the quickest. So while i know I will forget the exact implementations of 90% all the cool stuff I can do with vim, but I know the possibilities when I have seen them once somewhere.

I remember feeling combinations of motions with edit commands were like the superpowers when I first saw them. I had to look them up again several times whenever I needed them to get it right. But once I got it, suddenly predicting them to edit text on vim transcended into fun territory for me personally.

Though for vim, I have found to adopt only one or two new things a week at max to improve productivity. Anything more and it used to get overwhelming, or i forgot about it anyways...

I also chose the 'full immersion' strategy to learning my way around vim. It was a lot less stressful than trying to memorize a ton of information at once, and I feel like I am retaining things I learn because I actually learned them because I needed them for one reason or another.
Exactly. I installed VsVim or ViEmu and went about my daily work. Played Vim Adventures, just a few levels. Every now and then I'd look up more. After a while, I couldn't imagine not knowing vim, and it's painful to watch other programmers edit text.