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”
Using hjkl instead of arrows benefits even more, than You imagine right now ;))
Just remapped my arrow keys to nop. I will see how that goes.
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.
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!
Reminds me on my similar problems! http://spz.kilu.de/blog/?issue=117
Thanks for the link to vim-anti-patterns!
> 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 “
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.
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.
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 :-).
Sorry wordpress killed the formatting … you can follow this link — http://stevelosh.com/blog/2010/09/coming-home-to-vim/#some-background-about-me
http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement <- this might also help with using hjkl, making it more useful, when prefixed with the number of lines you want to go up/down
Ah that is great, will give it a try! Thanks!
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.
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.
If one doesn’t use hjkl for navigation, there is no point using Vim. Can as well use notepad
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.
vi != vim
For one thing, vim is a powerful abrasive cleaner. But seriously, vim has a lot of improvements over vi (if you last used vi on a 300 baud it is some time ago). I think if you learn to like the modal way of things then it is quite excellent.
Oh and emacs isn’t available on my iPad/iPhone :p
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.
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.
I believe it’s # to search backwards, * to search forwards.
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.
Thanks for those hints, definitely useful.
[…] 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. […]
[…] has been a while since the last posts from this series, so the title weekly doesn’t really apply. The previous 2 bite-sized […]