Hacker News new | comments | ask | show | jobs | submit login
Here is why vim uses the hjkl keys as arrow keys (catonmat.net)
478 points by freestyler on Mar 9, 2012 | hide | past | web | favorite | 98 comments

Doesn't this go way back though?

The reason that keyboard had those arrows keys on it was because those keys correspond to CTRL-H, J, K, L and the CTRL key back then worked by killing bit 6 (and bit 5) of the characters being typed.

The effect was that H which is ASCII 0x48 would become 0x08 which is backspace. If you look at an ASCII table (e.g. http://www.asciitable.com/) you will notice how the uppercase ASCII letters line up nicely with the control characters so that just dropping bit 6 will get you there. Same thing with the lowercase (drop bits 5 and 6) and you are on the control characters.

The CTRL-H, J, K, L therefore correspond to BS, LF, VT, FF. BS is backspace (i.e. left), LF (down), VT is vertical tab (so up) and FF is form feed (which in this case takes you up). I'm not sure why FF was used for up.

This is also why CTRL-I is tab, CTRL-D ends a communication. All of that goes back to teletype days. Also for telnet users out there you'll see that CTRL-[ lines up nicely with ESC. And when you see a ^@ being printed on the terminal you can see why it corresponds to a null byte.

One other interesting thing about ASCII: uppercasing and downcasing can be done by twiddling a single bit.

If you look at this picture of an ASR-33 Teletype you'll see that come of the control characters on the keyboard correspond to those in the ASCII set. This is because ASCII evolved from the earlier teletype character sets: http://upload.wikimedia.org/wikipedia/commons/0/0b/ASR-33_2....

This is correct, and the reason they used those keys was that it was the 'home row' on a typewriter which was used in teletypes which meant your little finger could push 'ctrl' and your right hand could drive the cursor through forms without moving off the home row.

When I saw the title I was expecting to see a picture of the rogue screen. Rogue (and later hack, and nethack) is a text displayed dungeon exploration game and was often the first exposure folks got to the convention of h,j,k,l as left right up down.

I really miss having control over there. I xkeymap it there of course but some keyboards have a physically 'push-on/push-off' caps lock key there which is annoying.

Count me in the school of thought that the control key is meant to be to the left of the A key, just like the horn button is meant to be in the center of a car steering wheel. Every computer keyboard I worked with before the advent of IBM PCs had this arrangement. Every computer I work with now I reconfigure to swap the caps lock with the control key.


It's a major frustration for me, too, as an Emacs user.

I'm still looking for a hardware dongle that does nothing but map the caps lock scan code to left ctrl. Sure, I can rebind the key (and I do) but as a contractor I move around a lot and having something that circumvents the OS entirely would come in handy.

I use a keyboard that has a similar layout to that in the images above (ctrl to left of A, tilde on home key at top right etc). It's called the Happy Hacking Keyboard and is made in Japan by a Fujitsu subsidiary: see here http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard. It's extremely expensive (~$300 or so) but has amazing key action and having gotten used to it I would never want to use anything else

Actually, on a Teletype, VT went down, not up. (Vertical tab stops were settable only in hardware on most models.) And FF, which went to the top of the next page, is obviously unrelated to going right. So the correspondence was really just suggested by BS and LF; it was apparently some terminal manufacturer's idea to put up and right on the next two keys.

As far as I know, this was Lear Siegler's invention; I don't know of any other terminals from the era that use ^K and ^L for "move up" and "move right". (Someone did mention that ^K and ^L did do that on the ADM-3A, right?)

Then the question becomes, is there a reason those control characters correspond to those letters? I have a hard time imagining a world where BS is ^Q and VT is ^V and therefore the arrow keys are spread out all over the keyboard.

Good point. One question:

> I'm not sure why FF was used for up.

Did you mean "used for right"?

And that's the source of the old joke where 'overwrite' something unfortunate with ^H^H^H^H^H.

ADM3a was a terminal, not a computer. I used to use these two in around 1976 or so. And the hjkl pattern has nothing to do with this or any other terminal. The ASCII control codes for Ctrl-H, Ctrl-J, Ctrl-K and Ctrl-L were used to make the Teletype's printing carriage move left, down, up or right. Bill Joy's innovation was "modes" so that Ctrl-H did not delete the character when it moved left, etc...

  8 H backspace
  9 I tab (right a lot)
 10 J line feed (down)
 11 K vertical tab (down a lot)
 12 L form feed (down a page)
 13 M carriage return (left a line)

Bill Joy didn't invent modes; all editors were very modeful until Bravo, in the 1970s. And in vi, you don't use ^H to move left; you use h. Tony has already corrected you on ^K and ^L, but I'll point out that there was a way to move the carriage right on a Teletype, even though it wasn't ^L: the SPACE character does that.

Most of the answers I've seen as to why hjkl have fallen into the "so your fingers stay on the home row / it's actually quite fast once you get used to it" realm. But those answers were never completely satisfactory to me. Once you get used to it, it's fine. But I feel like it would have been fine as adsw, or jkl; (so that your hand really is on the home keys) or some other 4 key combination near the home row, too.

This explanation of the origin hjkl is the first one to satisfy me. Now I can see the others, not as explanations as to why it is, but explanations as to why it stuck.

I have another good suggestion: learn pressing the left control with the side of your palm. It's usually a very minimal movement, and I find it a lot easier (and less straining) than moving the pinky to press control. The only drawback is that it doesn't really work on a laptop keyboard.

I picked that up a few years back playing World of Warcraft and some other games, to avoid having to move my left hand away from WASD as much as possible, but it has served me very well in a lot of other applications since then.

Edit: Well, that was supposed to be an answer to cheatercheater below this comment. Woops.

Yeah, I do that. One reason why I hate laptop keyboards!

I use JKLI. The two main advantages are that you don't use your pinky finger repetitively, and the layout of JKLI makes intuitive sense as to what each key does.

You don't use your pinky for hjkl either, or use shift.

Good point, not sure what I was thinking. For me the strain of moving my index finger from J to H is more than the strain of moving my middle finger from K to I, but that's probably quite subjective.

Once you get used to hjkl,it's pretty "intuitive" (in that you don't need to think about what you're doing).

Unless you use Dvorak. I know I'm in a super minority here. But shortcuts based on keyboard location rather than tied to some mnemonic are damn near impossible to get right. Doubly so when I have to switch to QWERTY to work with anyone else.

I switched cold turkey to Dvorak and Vim at practically the same time (what can I say, I like pain :) I learned Vim mnemonically and have no problem using Vim on QWERTY. At the same time, I could have just as easily learned it "locationally" and been screwed. Only chiming in because I think it's interesting that the learning curves for location-vs-mnemonic seem approximately equal, but the long-term payoff of one is much greater. Neat asymmetry.

The reason it stuck is because it was grandfathered in, not because it is the best way to do it. Same as most things, like qwerty etc. People just naturally do things the way they always did things.

I have created an ergonomic layout based on the us layout, called us_split. In it, Vim's hjkl actually are the home position. The layout works under Gnome. It's just like qwerty but a bit different. Basically the two columns from the right are put in the middle. Time to learn to use it fluently is only a couple of days if you touch type qwerty already.


I see a lot of people in the comments talk about vim's "weird" layout choices. I suggest in your OS you change caps lock to esc; and that in vim you remap ; to : and : to ; in order to stop having to press shift every time. You can then really start doing the whole home row thing; IMO it's impossible without those two crucial settings.

I prefer caps lock to Control. ^C does the same thing as escape in Vi[m]. While it is chording, the combo is pretty easy to hit.

This is a bit of a compromise, but all the other programs that are not vim will benefit from it.

Another excellent trick I read somewhere is to map 'jj' or 'jk' to esc when in insert mode (so 'inoremap jj <ESC>'). As a digraph they never occur in a natural language, so you'll never type it accidentally. And you can hit it on the home row easy-peasy.

Although personally I type in Colemak, I still use this, just with 'yy' instead. To be honest, it's a barely noticeable slow-down but it's much less stressful for my hands.

There are really a lot of ways to do this. See: http://vim.wikia.com/wiki/Avoid_the_escape_key

> As a digraph they never occur in a natural language

I wonder how Dijkstra would react to that statement.

Or all those people whose handle is JJ?

It's common to use double letters (ii, jj, kk) as inner indices when writing tiled multi-dimensional array code. Regardless of whether you think that is a good convention, you should be preserving that convention if you are editing legacy code that was written that way.

Never knew that Ctrl-C does the same thing as escape. Thanks for the info!

Another issue: if you ever use Ctrl-C in a macro, and then try to rerun it, the macro ends on Ctrl-C.

I use ^[ to end insert mode.

This only happens when you use macros like I do. I took a look at my registers and here's a random one I did four months ago (using Surround.vim to wrap an indented area in div tags).

V/^[ ]\{0,2}[^ ]/-1^MS<div class="question"^M

They're not exactly the same thing, actually, ctrl-c doesn't allow you to expand abbreviations and doesn't trigger the InsertLeave autocommand, but to be honest, I don't think it's that big of a deal. I go between ctrl-c and ctrl-] without thinking.

If you use q: or q/ to edit the command or search history, ctrl-c will close the edit buffer rather than swap out of insert mode.

Also, 3aa<Esc> appends 3 a's, but 3aa<C-d> appends only 1.

Life changes. Yet again. Thank you, thank you ..

That's a brilliant layout. Unfortunately it appears to require a "tall enter" keyboard rather than the much more common "wide enter". I'm in the market for a nice cherry blue keyboard, so I'll see if I can find one with a tall enter. Anybody have any sources?

I'm using the silent ([EDIT: I had this in reverse..] cherry mx brown; non silent is cherry mx blue) DAS keyboard[1]. The "professional" model has the letters on the keys, if you don't want a blank one. They seem to come in both US (wide enter) and UK (tall enter) versions. I personally chose the UK one as that's what I'm used to.

Here[2] is a photo of my keyboard. The coloured keys are sold as WASD/escape keys but I used them for playing Skyrim - the green one on the right is especially handy for gaming as i don't have my fingers on the home row when playing games rather than typing. The blurred circuit board is a development version of the Midifighter 3D[3] as the photo was taken before they were announced.

[1] http://www.daskeyboard.com/model-s-ultimate-silent/

[2] https://plus.google.com/u/0/photos/100355674716107842386/alb...

[3] http://www.djtechtools.com/2012/02/27/introducing-the-midi-f...

I also have a Das keyboard and it is really awesome. (Also, I think it's the best looking keyboard I have ever seen--very minimalist.) However, I think you have them backwards--the "silent" version has the brown switches and the normal version has the blue switches. I also have the ultimate silent version and it is not really silent--it's just quieter than the other version. You still get a satisfying noise when you use it.

I've been really satisfied with it, and would definitely recommend it to anybody looking for a nice mechanical keyboard.

You are of course right! I have a brown, not a blue! Not sure why I was thinking about it in reverse - oops.

Well, mine is quieter than other keyboards I've used, eg a Dell keyboard[1] as long as you don't bottom the keys out, though I do get the occasional loudish click when the keys snap back, usually from the space bar, though if I slow down when typing I can make it almost completely silent (obviously thats not normal typing though).

[1] I did have a Saitek eclipse a few years ago that was quieter.

I'm using the loud, non-labeled DAS keyboard Model S Ultimate, typing with Dvorak layout. It cost's a small fortune, but it's so worth it. I love this keyboard. http://www.daskeyboard.com/model-s-ultimate/

Mostly keep my vim key-binding mappings small, so I'm not confused when working with it with default settings. Mostly use <Ctrl>-c to get out of insert mode though, escape is so far.

It did cost a lot, but honestly, as a programmer I type so much, its totally worth investing in a good keyboard and I love my DAS keyboard.

The only thing I wish is that the keys were in a flat grid rather than staggered, but it seems very very hard to find any keyboard like that, so no biggy. I've considered getting a Kinesis for years now too, but since I got the DAS, I've had no plans of switching any time soon.

I haven't completely worked out my vim bindings since I've been doing a lot of Qt development in QtCreator lately and just used that without vi mode. I use Colemak for my layout so some of the default keys are a little less than optimal, but I also don't want to change them too much. With one or two simple key swaps it seems quite usable though.

On windows (where I don't use vim) I did map alt-gr to some common programmer symbols; mapped caps lock to control and control to backspace. On my laptop I use vanilla colemak though (including caps lock as backspace, though I'm likely going to change that some time).

I bought a das keyboard last year for home. I got the blank keyboard, but found that a lot of random hand issues went away. I ended up buying myself a second one for work, and will absolutely not go back to a standard keyboard.

You my friend should check out The Keyboard Company, keyboardco.com

They have tons of choices of blue, black, and brown Cherry switches (I think), you just have to match up their terminology with the right color.

They ship from England too, fyi.

It works for horizontal enter as well.. the only key that is missing from a horizontal-enter keyboard is the one between left shift and z, which is just backspace, so you can disregard it. It's just that with a horizontal enter key, the ; key is above the enter. With a boot-shaped enter key the ; is next to l, which makes it easier for me to hit it. I use the command line a lot.

If you're looking for a good place that does boot shaped enter keyboards then you can use Unicomp's Model M. You can rearrange the keys as you wish and they'll even print key caps with anything you want on them with the home row indicator tab, so you can put one on the h key.

Oh yes, you're right -- the only difference is the swap between semicolon and enter. Good for Ruby, bad for C.

The only problem with your layout that I see is the right shift key -- you lose a key because it's covered by two fingers. I'm definitely going to give it a try.

The right shift key is covered only by the pinky finger of your right hand. The ring finger has the forward slash. If you give it a try make sure to send your comments over via bitbucket or something :)

Oh lord! I remember these unibody terminals. I had to use the Volker-Craig VC4404 . That thing was built like a tank. You had to hammer at the keys with great force. Soon one got into the habit of hammering on the keys all the time.

And then one day I walked into the lab with shiny new VT-100 terminals with their soft keys; but started hammering on them by habit. And everybody turned around and looked at me as if I was possessed..... :-D

I smiled when I saw how you call them "unibody" terminals. We have come a long way since then. :)

When my dad bought a DEC Rainbow 100, I loved loved loved that I could move the keyboard. Such a boon!

Here's a VC4404 manual which shows the key layout/functions:


Perhaps just as enlightening is the location of the Esc key (to the left of Q): http://en.wikipedia.org/wiki/File:KB_Terminal_ADM3A.svg

I'm just learning vim and just yesterday I was wondering why they chose the escape key.

These days I mostly use Ctrl-C instead of Esc, just because it's so much easier to type.

If you make a serious commitment to vi(m) though, binding your capslock to esc is probably the better way to go.

I found ctrl there more useful. Especially for its use with other programs.

It would have been a lot more useful if the link had said how/why the ADM-3A used hjkl as cursor keys.

For those reading this that don't know, it's so your hands stay on the "home row"

[Edit:] or at least that's what I thought, wavetossed's comment below provides interesting insight.

Then wouldn't have 'jkl;' made more sense? (assuming index fingers were placed on 'f' and 'j' back then as well)

Wouldn't jkl; be even better for that? (not saying that hjkl isn't a huge step up form the arrow keys)

Probably because it didn't have dedicated arrow keys. Many older terminal keyboards didn't.

But why did the terminal choose those keys? Most games have chosen an up-down combo that are up-down from each other, rather than right-left.

The thing about ASCII Control characters doesn't quite map to left-up-down-right, and even if it were part of the answer, we could ask again: why were those characters chosen for those control-roles?

And the reason they didn't was that they evolved from typewriters that relied on a forward/backward spin to move up and down (except for the carriage return lever). We're lucky that the original terminals didn't have a scroll knob on the left and right of the machine to move up and down and a lever to move to the next line or we would have been stuck with that concept for years and the laptop may not have been invented because the knobs and lever would have required some elevation, which means the whole thing would have had to have been bigger.

I used a scroll knob to move up and down (and left and right) on my Blackberry in 2000, and it was awesome. It's dramatically better than using arrow keys. It's a real shame the original CRT terminals didn't have scroll knobs. It would have dramatically improved the usability of computers throughout the 1970s and 80s.

I worry that the tone of the article seems dismissive of the choice that went into mapping those keys to arrow directions in vi ("That's the whole story"). As if to say it was mere happenstance that vi uses hjkl, and any other outcome was equally likely.

It seems more the case that the designers of that computer chose those particular keys on the basis of a desire for efficiency that vi also followed, so there was no need to create a new convention.

The "article" is barely three full sentence and includes two images. To me it clearly states why hjkl were chosen as the keys for navigating around the editor.

How does the author's "That's the whole story!" ending make you feel like he's saying hjkl being used as the navigation keys was a coincidence?

Honestly perhaps it's partly a defensive reaction about a tool I like. If so, my bad. Maybe I can't help but feel like the "article" could be perceived as saying "see, vim chose these keys simply because that's what the keyboard said without any thought toward efficiency". The idea that they're chosen for efficiency seems so self-evident that it doesn't need a flourish to reveal. It's likely I'm thinking too hard about it. In any case, it is a neat little bit of history and the link gets my upvote.

Maybe "efficiency" is implied, since folks using the initial version of vi would be used to using those keys for navigation anyway. So, I read this more as "Bill Joy used a convention" rather than "Bill Joy chose the most efficient keys for touch typists."

I've got one of those sitting in my spare room... I'd be using it right now were it not for a busted flyback, which emits a whine capable of giving me a headache within 15 minutes. It's really a rather nice terminal, with an oddly attractive screen font and a keyboard that is well-suited to UNIX.

The coolest part is that there is no microprocessor, just a bunch of 7400 series ICs and some DIP switches to configure things.

If anyone is apprehensive about vim's hjkl keys, and you like the arrow keys better, you can use ijkl instead and get all the benefits of the muscle memory you have built up for arrow keys, while still keeping your hands on the home row. Use this mapping:

    " remap h to i and use ijkl for inverse T cursor movement
    map k g<Down>
    map i g<Up>
    map j <Left>
    noremap h i
Then the 'i' key will be replaced by 'h'. So press 'h' to insert, or for inner selections, instead of 'i'. Also note the 'g' for up/down motions, which means it won't skip the wrapped part of lines - just remove the 'g' if you don't like that.

If you're worried this breaks anything else, I've had this interfere with just one other thing: a plugin that let me select text based on indentation of the line the cursor was on, but I made a few minor changes to the vimfile for the plugin and fixed that pretty easily. The other thing is that random servers won't have these mapped, but just copy the config over if you'll be doing a lot of text editing on that server. Otherwise, you can just fall back to using hjkl awkwardly.

I'd recommend the opposite. If you're apprehensive about hjkl, force yourself to use it anyway!

  " disable arrow keys
  nnoremap <Up> <nop>
  nnoremap <Down> <nop>
  nnoremap <Left> <nop>
  nnoremap <Right> <nop>

I didn't advocate using arrow keys, but rather using ijkl. Your suggestion is not mutually exclusive with my own, you can do both. :)

Though I guess the mappings I provided would have to be noremap instead of map.

I'd recommend specifying a mode on those. That i map disables half the text-objects in visual and operator modes.

I still use those text-objects, just with h instead of i. This is required because otherwise, you'll have issues when trying to move in visual and other modes.

Another reason to use HJKL is that over a 1200bauds line, it is faster to send a single byte than the 3 bytes generated by the arrow keys (^[[D^[[A^[[B^[[C)

The ADM3A was a "dumb" terminal, constructed using a board full of 7400-series TTL ICs with no microcontroller. Probably to simplify the design, it wasn't capable of sending any multi-character escape sequences. (The ANSI escape sequences hadn't been standardized at that point yet either.)

The ADM-3A was a dumb terminal (well, it had to have had some level of screen addressibility, but was definitely not ANSI-compatible), not a computer. They were sold as kits, and about 1/3 the price of a VT-100. I remember reading somewhere that they were the first glass teletypes available to the BSD folks, and Bill Joy wrote vi purely because it allowed him to monopolize one for his own use.

Yes, in addition to the cursor movement control characters, there was a single escape sequence for moving to a specified row and column. I didn't know they were sold as kits. Did you buy a kit?

You will find similar reasons for why Emacs chose many of its common keys if you examine the [Space Cadet keyboard](http://en.wikipedia.org/wiki/Space-cadet_keyboard) for which it was designed.

All those awkward Control reaches originally laid comfortably under the thumbs.

So, since the vast majority of users didn't start out on that keyboard, the reason to keep doing it that way now is: tradition.

This feature of vi/vim along with not being able to save and quit as quickly as I can in emacs (ctrl-s ctrl-x, baby, none of that :q! shite) is the reason I use emacs. I totally respect those that use vim, but I'm not a freaking machine.

To save with [Esc],w add the following to your ~/.vimrc

  " remap <Leader> to ,
  let mapleader = ","

  " save with ,w
  nmap <Leader>w :w<CR>

I can hit ZZ faster than ctrl-x ctrl-s ;)

:x or ZZ instead of :wq wins :)

I'm actually glad it worked out that way, I hate using the regular, out-of-the-way arrow keys. They're even more annoying than the mouse.

Just never decide to learn dvorak.

This is good advice in all situations, having watched my bandwagon-jumping friends flail helplessly when using anyone else's computer. And all for a small potential increase in speed, at the cost of making your computer unusable to others and completely screwing the convenience of most keyboard shortcuts.

> at the cost of making your computer unusable to others

I have a qwerty guest keyboard next to my Kinesis Advantage. Also dvorak isn't necessarily that faster, but rather more comfortable.

Agreed, learn Colemak instead ;-)

Seriously though, I think with a tiny bit of key-swapping in the vimrc, you can use dvorak or colemak just fine.

+1. Dvorak needs to go the way of the dodo. Colemak:Dvorak::Dvorak:Qwerty.


I love colemak. While I'm still not quite as fast as I once was with qwerty, it does seem to take less effort and my fingers seem to dance over the keys as most key combos do really seem to be placed side-by-side.

I use dvorak with vim with (mostly) standard key bindings. It's not too bad. H and K are not far from their respective QWERTY positions. J and K remain next to each other.

Tip from a dvorak vim user: instead of H/L for moving left/right, use H/SPACE. SPACE has almost the same function as L but is more convenient to hit with your thumb. SPACE also allows moving to the next line when the cursor it at the end of a line, unlike L.

I'm used to H, J, K, L now. But when I wasn't aware of the history of vi, I always wondered why on earth he didn't use J, I, K, L!

I wonder if being right-handed also affected that choice...either the vim implementation or the hjkl keys as arrow keys.

Wait, I thought Rogue popularized hjkl? ;-)

Wow, thanks for sharing this. I really thought it was for efficiency. (Well, it is also, obviously).

That is the coolest looking thing with a keyboard I've ever seen. I want one.

That's a pretty sexy terminal.

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact