In Vim, why is 'j' used for down and 'k' for up?
Asked Answered
M

7

85

I've been using Vim for many years and have never really thought about it. A friend of mine asked why that is, noting that in our culture, left would usually map to up while right would map to down, making the Vim keys backwards.

I understand that they are on the home row, meaning that you do not have to move your fingers anywhere to hit them, but that's a different point altogether.

Why were these keys given their present purposes? Is there some documentation on the decision as well?

Mentality answered 1/7, 2011 at 22:21 Comment(6)
To quote Crockford, "It seems like once something stupid gets in, it takes a long, long time to get it out." :-) developer.yahoo.com/yui/theater/video.php?v=crockonjs-2Hydroquinone
Why in your opinion does left usually map to up and right to down. I haven't really thought of it before but I feel it should be the otherway around. Possibly purely because I'm right-handed (but the majority of us are) and so, maybe something like, right feels more positive, as does up. Hmm. I'm just intrigued why your natural reaction is to see it the other way. This certainly isn't the place for this discussion though (I wonder where is, psychology.stackexchange maybe - if such a place exists).Zimmer
@tjm, I agree that "right feels down" in the sense that we read left-to-right. Once you're at the end of a line, the next character is always 'down'...Just a thought. Anyone whose native language reads right-to-left out there feel differently?Hydroquinone
When using the Latin alphabet the text starts at the top left corner of the page. I feel it would be more intuitive to have the 'j' key pointing up. This would have the left directional keys ('h' and 'j') taking us back toward the 'beginning' of the page, and the right keys taking us to the 'end'.Pole
Sorry for responding to such an old thread, but I believe the reason j feels like it should be up is twofold. 1) Your middle finger rests on j which, for the vast majority of people is your longest finger, and therefore points 'up' more than the others. 2) Most people are used to using the cursor keys, which have up and down arranged one above the other, meaning that unless you do some kind of finger acrobatics, the middle finger acts as both up and down, so when you then use your 4th finger to go up it just feels plain weird.Benedikta
I sometimes imagine k as the gas pedal, which moves me forward; and j as the brake, which pulls me back :) Though after all the years, sometimes my logic gets confused. Why the left side opening braces [ and { are associated with upwards movements (as in "the text before"), when the left side j moves me down?Tenancy
P
112

The answer is in the Wikipedia entry for vi. Bill Joy, who wrote the visual mode of ex - which ended up being Vim's precursor vi - used a Lear Siegler ADM-3A terminal on which the HJKL keys mapped to left, down, up, right - and it’s been that way ever since.

Here's the keyboard layout:

keyboard layout of the ADM-3A

A couple of other points of note on the ADM-3A layout:

  • Left of the Q: the escape key - somewhat handier than where it is on keyboards today, hence a good choice for switching between normal and insert modes.
  • Top right: the 'Home' key doubles as the tilde (~), which subsequently became shorthand for a Unix user's home directory.
Phagocytosis answered 1/7, 2011 at 22:33 Comment(7)
ah crap. you beat me by a minute.Ungrateful
well now i guess the question is why did the ADM3A use J for down.Mentality
I wish they would just remove caps lock, finally.Secretarial
@geowa4, J was probably down because Ctrl+J is line feed in ASCII. The ADM3A terminal was just barely smarter than a teletype, so a coincidence like that might have simplified something in the implementation. Note that Ctrl+H is ASCII backspace which corresponds to left...Autarchy
I've seen some people advise to remap the caps lock key as escape to get that ease of use back :)Megaton
@NeilG and replace it with EscCustos
vi was originally a wrapper around ex - ex was still a stand-alone command. As for vim, if you create a symbolic link from vim to ex (if it's not already there), you can use it to make vim emulate ex.Enrich
E
25

vimtutor provides the mnemonic that 'J' looks vaguely like a downward-pointing arrow, though that may or may not be the original reason why it was chosen.

Extraterritoriality answered 1/7, 2011 at 22:26 Comment(2)
An alternative mnemonic is that j has a descender while k has an ascender.Berate
if you read a paragraph, H goes "back" towards what you have already read, and L goes towards what comes next, so I find J should still go "back", up one line.Arnoldarnoldo
H
15

This is a total guess, but: The Ctrl+J character is the "line feed" character, which on a traditional TTY moves down one line, providing a mnemonic. K was right next to it on a Qwerty keyboard, under the second most commonly used finger on the right hand when in the home position.

Hominid answered 1/7, 2011 at 22:24 Comment(2)
a plausible intentional coincidence w.r.t. the design of the aforementioned ADM3A terminal layoutDearth
The calculation may be something like ASCII 74 (J) - 64 (Ctrl) = ASCII 10 (LF / Line Feed).Deguzman
W
5

Simply to supplement all the answers, here is the photo of the ADM-3A keyboard (the exact keyboard on which Vim has been created).

P.S: I wish they had used W, A, S, D

Enter image description here

Wollastonite answered 1/2, 2022 at 2:49 Comment(0)
P
3

It explains it if you run vimtutor in the terminal. It says:

The h key is at the left and moves left.

The l key is at the right and moves right.

The j key looks like a down arrow.

Penang answered 29/3, 2018 at 17:42 Comment(0)
A
2

From a user experience perspective, after you open a file, you always move down first and moving down is usually a more frequent operation. Since from left to right is the natural direction for most of us, it make sense to associate the task you perform first or more frequently with the key on the left. You can try to switch the 2 keys and try to tell which way is better. For me, "J" for down is more natural and comfortable.

Aurelie answered 5/4, 2017 at 7:29 Comment(2)
"make the down key come first before the up key" I'm not sure what you mean by "come first before" as I don't see a sense of direction in the keyboard, but I think we agree that hitting a key with the index finger is easier than with the middle finger. So it makes sense to use the key that's nearly always under the index finger for down, the most common direction to navigate in a file.State
@jlmg For me, if A is on the left of B, it feel nature to say that A comes first and B comes later. And I will prefer put the key I press first and more frequently on the left hand rather on the right. It's just more natural and comfortable for me.Aurelie
E
1

I always thought it was because on the Dvorak layout, j and k are also next to each other. There are not many keys that have this property on both Dvorak and A/QWERTY/Z.

Exalt answered 27/12, 2013 at 10:28 Comment(1)
The question is actually about why using J for down and K for up (why not K for down?) in such layout.Aurelie

© 2022 - 2024 — McMap. All rights reserved.