I have read it in many articles (e.g.) that it's a bad practice to use arrow keys in normal mode. Could somebody explain why is it considered a wrong practice, if at all?
8 Answers
Actually this is not a bad practice.
A lot of people (including Vim's doc as @B Layer's answer shows) argue that you shall not use arrow keys because it makes your hands move from the home row (the second line of your keyboard where you have hjkl) and that make you less efficient.
This is (IMO at least) a bad result from vim "purists" circle jerk: new users follow this blindly, some even disable the arrow keys and then they complain that they are not as efficient as before.
The only real rule to use vim is to use it efficiently: if you feel more comfortable using Left and Right than h and l be it, use the arrow keys. Vim even has set mouse=a
option if you want to use a mouse!
Once you get used to vim and get better with the different available motions (f, t, ,, ;, w, b, e, ge, and basically everything in :h quickref
) you might realize that you don't use arrow keys that much. And if you still use them, it's not a problem, use what you are most efficient with.
Note That the argument of don't leave the home row for more efficiency is not that good in my opinion: If you take a look at Why does vim use hjkl you'll see that these keys were chosen because there weren't any arrow keys when Vi was written. One could argue that maybe if the keyboards were different at this time no one would consider not using the arrow keys a good practice.
-
14I often found myself bending my wrist very badly to reach the arrow keys because I was so used to them. I ended up disabling the arrow keys to get myself used to
hjkl
just to relief some pain from my wrist.– ShahbazMar 26, 2018 at 13:57 -
10I'd like to point out that this only makes sens on QWERTY keyboards. For some layouts hjkl can be a real pain.– coteyrMar 27, 2018 at 2:14
-
1You can make use of both options: whether you pick hjkl or arrows, you can bind the other set of up/down keys to move by on-screen lines and not file lines. It helps when editing files with long lines. Mar 28, 2018 at 9:21
-
5However much I appreciate your to-my-eye correct answer, could I please prevail upon you as a fellow moderator and vi aficionado into trying your hardest to find some less “seedy” alternative phrasing to circle jerk that does not evoke the vividly disturbing image of a trouser-less ring of boys or men all masturbating together in each other’s company‽ Pretty please? :)– tchristMar 29, 2018 at 2:27
-
2Ack! @tchrist that gratuitous word painting of the offending phrase has made your comment the thing I'd personally like to see cleaned up. LOL.– B LayerMar 29, 2018 at 13:49
I don't know if this is really relevant any more, but I'm an old-timer so here's a bit of history.
In the old days, VT100 terminals had arrow keys, but pressing one transmitted an escape sequence like <ESC>[A
for up, <ESC>[B
for down, etc.
Also, being a serial terminal communicating at 9600 baud, it was possible for the user to press the keys too fast to transmit the whole sequence which would cause one key press to interrupt the sequence of the previous key press and confuse vi.
This would cause vi to inexplicably go into insert mode and insert [B[B[B[B[B[B into your file if you held down the down arrow. I say inexplicably because there's nothing about this sequence that should enter insert mode and the fact that each sequence starts with an ESC character should have exited insert mode. But still, it would insert garbage. Not always - just when you were in a hurry.
One quickly got tired of this and decided to just stick to the hjkl
keys.
-
8For whatever reason, TCP terminal programs still use escape-based arrow keys, and still do not have any 100%-reliable way of distinguishing between pressing the escape key versus sending the first byte of an arrow-key escape sequence.– supercatMar 26, 2018 at 23:20
-
1
-
1@jamessan: If I read that right, you're suggesting that the reliable way of distinguishing escape from arrow keys is to not use escape-based arrow keys, which ties in with my "for whatever reason" comment about programs still using escape-based arrow keys.– supercatMar 27, 2018 at 14:22
-
Hmm, I have an old IBM terminal that can emulate a VT100, might have to try this out Mar 28, 2018 at 14:37
-
4I can certainly confirm that this was true, at least way-back-when, as during the 80s we had to put up signs in the undergrad terminal room not to use the arrow keys in vi for this very reason. They kept messing up their files when the tty driver dropped interrupts.– tchristMar 29, 2018 at 2:35
While I agree with @statox that no one should be chastised, shunned, excommunicated, or tarred-and-feathered if they choose to use the arrow keys...use your software however you see fit...I wouldn't exude as much of a laissez-faire attitude if I were giving guidance to an open-minded new user. I'd pretty strongly recommend that they start with following best practices. They are "best practices" for a reason...they've been employed to good effect by lots of people who came before you and me.
Heck, even Vim's own help nudges you in that direction, though not without hyperbole ("greatly slow down")...
You can also move the cursor by using the arrow keys. If you do, however, you greatly slow down your editing because to press the arrow keys, you must move your hand from the text keys to the arrow keys. Considering that you might be doing it hundreds of times an hour, this can take a significant amount of time.
There's also the fact that whatever choices you make at the start may become habits that are difficult to break if you want to change in the future. So all else being equal why not adopt a recommended habit now rather than take the chance that using arrow keys is inefficient or carpal-killing?
All in all, consider giving an honest effort to doing things without arrow keys. After a reasonable amount of time if you just can't live without up/down/left/right...don't.
...is what I would say to a new user who was interested in my opinion. Don't take it personally if you don't agree (I'm talking to you, downvoter).
-
9Even if you accept that it is indeed better than arrow keys (I am not even convinced it is) it is somewhere is at the bottom of the list of all the possible "best practices". I would argue that there are many many more important things than using hjkl. Mar 26, 2018 at 14:25
-
5@Carpetsmoker I don't think it's necessary to rank best practices. Unless you have a strong desire to use arrow keys why not do as Vim recommends? Particularly if you are new to Vim...we all know how hard it is to change the way we do something once it's ingrained.– B LayerMar 26, 2018 at 20:12
-
1"you greatly slow down your editing" - that seems like quite the exaggeration to me... nevertheless I agree it's worth giving it a try. It's just honestly not important enough to war over.– ShadowMar 26, 2018 at 22:46
-
2@BLayer no not here - this is the most civil discussion on the topic I think I've seen. But wars certainly have been fought...– ShadowMar 26, 2018 at 23:00
-
1
don't think it's necessary to rank best practices
Well, it seems to me that we should focus on the things that give us the best value in life, given that resources are finite and that life is short. I happen to know the hjkl keys be heart due to a misspent childhood playing rogue, larn, angband, etc. on a laptop without numpad, but some people spent their childhood better, and will have to "unlearn" arrow keys regardless. I think that learning motions, ranges, etc. are a much more useful way to spend time. Mar 27, 2018 at 2:07
Other answers have said that the efficiency loss from moving your hands from the home row isn't that big a deal, and I mostly agree. However, what is a big deal is when you are learning vim, always using arrow keys to avoid switching back into normal mode.
In other words, using a single ← where you would have used a single h isn't a huge loss in productivity, but using ←←←←←←←←←← where you could have used EscI is a huge loss.
That's why people tell you to avoid the arrow keys when you're learning vim, because it forces you to learn normal mode instead of never leaving insert mode. Normal mode is where all your efficiency gains are.
There are two main reasons why people say you shouldn't use the arrow keys. Some claim that hjkl is preferred because it keeps your hands on the home row, which isn't false, but also doesn't matter. Others, including myself, would say you shouldn't use the arrow keys if you're moving more than a few characters, but you shouldn't use hjkl, either.
The important thing isn't which keys you're using, but rather how many. If you want to move to the beginning of the word, you don't hit hhh
; you hit b
. End of the word is e
, not llll
. Beginning of the line: 0
, end of the line: $
. And anything further, /search
for the text that exists where you want to go. (or whichever of the advanced movement commands you prefer).
But any time you find yourself typing the same key more than twice in a row, there's a better way to do it.
A more precise statement might be "using the arrow keys is bad practice while trying to learn vim better". People new to vim are used to using the arrow keys to move around and so don't learn hjkl
, thus not getting the chance to see if this is preferable.
Personally, after gaining some general familiarity with vim, I disabled the arrow keys for a while to get the hang of hjkl
and reenabled them later to simplify my .vimrc
. The conventional wisdom holds in my case; once I knew how to use both hjkl
and the arrow keys, I found myself almost exclusively using the former.
-
6I disagree. Like this blog says, that "all or nothing" approach for beginners helps cause the "user unfriendly" reputation of vim. New users start, get bogged down and overwhelmed and (not surprisingly) conclude vim is an unholy mess and switch back to an editor where they can be decently productive. -- If you're learning to swim, you don't throw yourself off the boat into the open ocean. It's perfectly acceptable to dog-paddle around the kiddy pool until you get comfortable with the water.– R.M.Mar 27, 2018 at 16:05
-
2... not to say that disabling arrow keys won't work for people. But it's a "I like vim and want to learn how to use it better" step, not a "I'm new to vim and want to learn how to use it" step.– R.M.Mar 27, 2018 at 16:07
The usual answer involves efficiency. You've likely been trained over the years to employ touch typing, which employs keeping your hands on asdf and jkl;. A vast majority of the key bindings in Vi are designed around this fact, including the hjkl movement commands.
Personally I actually use both. I use hjkl when I'm actively editing large sections of text, and the arrow keys when scrolling through log files (and |, g, and / when I'm doing spot edits throughout a file). This just happens to be what works well for me, and in a case like this, that's what really matters.
Long time ago, many terminals did not have arrow keys, or did not have all four of them. Software systems that needed navigation normally provided some sort of navigation approach without relying on arrow keys, and vi's hjkl is a canonical example. (Another classical example is WordStar's ^E-^S-^X-^D.) As keyboard navigation's utility became increasingly obvious, more and more new terminals began to come with arrow keys, and software systems often mapped these to the old ways of navigation. However, this provided a portability concern of sorts: if children trained at schools were to come to rely on such a fancy new-fangled feature as arrow keys, the thinking was, they might not be able to be productive on the — once common — keyboards without arrow keys.
So, teachers of courses into basic computing began to cautiously warn students to learn to use the lowest common denominator so as to maximise their employability. And this practice has long outlived any reasons that it may once have had.
In modern implementations of vi, the main reason to prefer hjkl is that it's slightly faster to use keys already under your hand than moving your hand to wherever your arrow keys are. The downside of avoiding the context switch of your hand, of course, is that you may need to perform a context switch of vi. Nowadays, when humans are very rarely treasured by typing speed, whether you do one or the other is pretty much a matter of personal preference, and there's nothing inherently bad about using arrow keys anymore, if there ever was.
hjkl
in Dvorak. It's definitely less sensible than on qwerty, but I don't know that I'd call it "difficult to use"; they're just keys, and it's not like it's hard to reach them or anything once you're used to it.hjkl
should be the best option for moving in a document. The truth is, some keyboards had the arrow keys onhjkl
in the time when vi was created, so vi used the same keys in normal mode to match the keyboard layout. Here is an article with a photo of the keyboard: catonmat.net/blog/why-vim-uses-hjkl-as-arrow-keyshjkl
. Good ones. But they get inflated to the status of dogma, at least in part, because people have to feel like they're better than others.hjkl
are not the best ever in dvorak. But as a dvorak user I can find them a bit handy since they are paired together: as injk
on one side of the board; andhl
on the other side.