Weekly Vim Focus

I’m a long time user of vim, but sometimes I feel I have retained some bad habits from when I first started using it, or that I am stagnating in my skills. Sure, I learn a new trick every now and then, but most of them I tend to forget again. So I am going to pick a few commands, and make an effort to stick to them consciously for a week. Hopefully. they will make it into my permanent repertoire that way. Depending on how well this goes, I might make a series out of it.

Here’s what I am going for this week:

  • use c for change instead of deleting and then going to insert mode
  • use ctrl-O to jump to the previous location (I just found out this also works across buffers)
  • use ctrl-R to paste registers in insert mode
  • use hjkl instead of arrow keys for navigation. (this is a big one, tried it before, and failed. But it makes a lot of sense to keep the fingers on home row as much as possible. Might go as far as remapping the arrow keys to force myself to do this.)

Seems like a short enough list to be tractable. More to come next week if I find this approach to be helpful.


24 thoughts on “Weekly Vim Focus”

      1. Most of the times you shouldn’t even use hjkl, but more semantic commands, i.e. f,t,F,T,H,L,M. For examaple if you want to add a parameter to the function call, you should press “f)” to jump to the closing bracket, and the press “i” and add what you want. It’s much more efficient then going there with “l”, or even with word-jump “w”. Try it and after a few days you won’t be able to imagine walking the text with just hjkl. Furthermore, “;” key repeats the last “f”, so if you press “f,” (to jump to the nearest comma), you can press “;” to jump to the next commas.

      2. Thanks for the comment, I completely agree! I have been using some of those already, but for trivial navigations I still use arrow keys, which is what I want to change. Might focus on some of your suggestions next week!

  1. > use ctrl-O to jump to the previous location (I just found out this also works across buffers)

    I take it you also know of “

  2. You can disable the arrow keys, so that will force you to use hjkl for navigation. You can add this to your vimrc to get this done.

    nnoremap j gj
    nnoremap k gk

    hope this will force you to learn :D.

    1. ah heh i guess it removed everything in angled brackets. That is more or less what I have done now, except for the gj and gk mappings, which are for screen lines if i recall correctly. I will leave that off as it gives me an additional incentive to keep lines under 80 chars in length.

    2. I’d say that 90% of the vim experience comes from navigating code using the home row keys (and of course word objects). It can be even more effective (and useful as a by-product) if you remap them to other things (like changing the buffer; nnoremap :bprev , etc.). That way, you will curse every time you press the arrow keys :-).

  3. What I do for arrow keys is leave them in insert mode, but remap them to useful things in normal mode. I have L/R set to switch tabs, but I haven’t decided what to do for U/D yet. Also one thing that helps reinforce hjkl is the use of multiple windows, especially for h/l because those are infrequently used in normal mode. I actually have Ctrl-h,j,k,l mapping directly to window movement to save the Ctrl-W stroke first.

    1. I used to have Up and Down mapped to bubbleing lines around in normal or virtual mode. I had to change it after I realized the mouse scroll is up and down in the terminal.

  4. Are you guys f*ing nuts – I last used vi on a 300 baud modem dialling into the university mini in the 80’s. We didn’t use vi because it was the best thing ever – we used vi because it was all we had and it had the neat feature that it did windowed updates – that is if you’re on a slow line and had 24 lines on your display it would only update the middle 10 lines or so. This was because refreshing the whole screen took too long on the 300 baud modem.

    We used hjkl because that was all the tty had. vt52’s were too expensive so we used cheap terminals and the arrow keys weren’t mapped properly in the termcap.

    Now I use visual studio or Xcode, I still use vi when I log into a unix box and need to make a quick change. You guys are wasting your time learning vi – it’s nuts. IF you want to use something powerful try emacs at least – we couldn’t use emacs because it used up all the memory on the mini, so we used vi. Not because vi was excellent, it was all we had.

    The sys admin had the sun workstation, there was only one, he didn’t use vi, we used vi because that was all we had, not because it was excellent.

    When I could afford a PC at home, I bought turbo pascal it had a windowed development environment, I didn’t use vi. I did write a curses package on the PC, so I could use vi, I was f*king nuts. I didn’t use vi because it was excellent, it was all we had.

    1. Actually, Dave, you are both right and wrong.

      Agreed wholeheartedly that there are huge roots to these things in screen efficiency, and predate visual editors, but much of vim is designed to keep you on the “home row” of the keyboard, or as close to it as possible. If you look at the commands most people use: s, F, f, hjkl, etc – they are designed to keep you in the sweet spot on QWERTY keyboards and to avoid hand motions away from the home row as much as possible.

      This isn’t to say there aren’t huge benefits to using (visual) IDEs – on the other hand some programmers think their productivity is better in vim due to the low key count and lack of requirement to use a mouse or arrow keys to get to where they want to go next – the majority of vi(m) keys are close to the home row.

  5. Everyone may know this, but if not, this will make your vim week: use # to search forwards for the next occurrence of the word your cursor is currently on. Use * to search backwards.

  6. Before it became second nature to use hjkl instead of arrow keys (admit it: the reason that you don’t use it now is because it’s confusing!), I used some tricks to help me remember which letter does what:

    h and l – they are the left and right sides respectively and will take you left and right
    j – “jump” down a line or notice that the j drops below the normal line of text, indicating it will take you down
    k – sort of forms an upward-facing arrow

    It’s hard to get used to but worth it, especially when you can start including it in compound commands in normal mode.

  7. […] Week 1 of my vim productivity boosting experiment went well. During the week most of the commands I aimed for ended up being used often. I got a lot of helpful comments, both here and on Hacker News. I ended up unmapping my arrow keys, and I do feel like this made a difference, I am now comfortable with those keys. As a bonus, it feels a lot more natural using hjkl in compound movement commands than it did with the arrow keys. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s