"Never use arrow keys in Vim!"
I heard this from a random speaker, but didn't have the chance to ask him why. Also, if you don't use the arrow keys, how can you move the cursor in the Insert mode?
"Never use arrow keys in Vim!"
I heard this from a random speaker, but didn't have the chance to ask him why. Also, if you don't use the arrow keys, how can you move the cursor in the Insert mode?
Using the arrow keys is considered a bad habit, because if you're using the arrow keys you're probably missing out on many of vim's lovely features.
When people first start to use vim, they tend to stay in insert mode since this is more like conventional text editing. To be effective in vim however, you should only be in insert mode when you're actually entering text. If you want to move the cursor around, you should be in normal mode. You should pretty much be in normal mode by default.
In normal mode there are a million shortcuts for moving around. You can use hjkl
to move around one space at a time, or you can move by words, paragraphs and so on. If you're in normal mode, there's no point in using the arrow keys instead of hjkl
since they're further away.
There's the attitude that if you're using the arrow keys, you're using vim "wrong". The truth is that vim has a really really steep learning curve, so while you're learning do whatever keeps you sane. When I first started using vim I did everything the "wrong" way and I didn't have any problems breaking the habits once I learned more of vim's commands.
As an example, when I first started, say I wanted to change the text in quotes:
String mystring = "I want to change this";
I would go into insert mode, move to the end of the string using the arrow keys, press backspace until the string was gone, and enter the new text.
A much better way is to put your cursor anywhere in the string (normal mode), then press ci". This will change
in
"
. It will delete everything between the quotes and put you into insert mode so that I can enter the new text.
ci<
and ci)
work as well, and you don't have to move your hand from the keyboard to the mouse. If you want to navigate to the start of the brackets and then change the content f(ci(
works.
Commented
May 24, 2013 at 1:28
/want
and hit enter to land on the word 'want', then do ci"
.
Commented
May 24, 2013 at 10:30
hjkl
): mapping up
and down
to 2 keys that are vertically-indistinguishable from each other, let alone from the left and right keys..... it's a tragic demonstration of ideologically-blinded arrogance.
Commented
Sep 8, 2014 at 1:52
In short, Vim
uses the hjkl
keys as the navigation interface is because it's a remnant of the old "ADM-3A" terminal, that had these keys marked with arrows.
Since
vim
is derived fromvi
, it uses the same hjkl keys.
The letter based replacement for the arrow keys allows you to navigate the text without taking your hands away from standard typing configuration. This is arguably more efficient and faster than moving your hand to hit the arrow keys. As outlined in reference #2 (see resources) this is not the end of the advantages to this: VIM has a lot of other shortcuts that you can access without moving your hands.
There are a few very detailed and accurate answers to the 2 parts of your question: in terms of arrow key replacement, I have answered in a quick and summary manner, however for your insert question, please see the resources listed below.
hjkl
don't work in insert mode, so how is this an answer to the question?
hjkl
in vim does not tell why someone should never use arrow keys, just as the fact that I can ride my bicycle to work doesn't explain why someone would tell me never to jog.
That stupid mantra is often repeated by touch-typists for whom the home row is like a second spine or by non-touch-typist who want to look cool. For non touch-typists, the home row is simply not that special and slightly moving the right hand to reach the arrows is not a problem at all.
What is important to consider is what the people who repeat that mantra propose to use instead.
Most of the time, they insist on using hjkl
.
If we leave out the fact that these keys are only used because the keyboard used by vi's author didn't have physical arrow keys and thus that any argument against the arrow keys is just rationalization, hjkl
are only marginally better than the arrows anyway. Yes, the only benefit of using hjkl
over the arrows is that you skip that small movement of the right hand to and from the arrows. Whether that benefit is worth the trouble is your call.
But that movement of the right hand is not why using only the arrows is inefficient. It is inefficient because they restrain you to move character-by-character and line-by-line while most of the movement needed for editing text have a much larger range.
Hammering the right arrow 13 times is inefficient and boring. Is it that better to hammer the l
key 13 times? No. It's equally stupid.
Keeping the right arrow pressed until you reach your target is less boring but it's error-prone and almost as inefficient as 13 hits because of the necessary and non-deterministic slow down at the end. Is it that better to keep the l
key pressed? No. Of course not.
Thankfully most GUI toolkits provide combos like Ctrl+Right
that allow us to move word-by-word or jump to EOL, to next paragraph or whatever. These shortcuts help us navigate a lot faster and more intelligently than with single arrows. Do we have the same with hjkl
? No, we don't. We can use a count, if we want, but who wants to count characters for every motion? Doing 14k
with the help of relativenumber
is great but what about horizontal motions? For large movements, hjkl
are actually worse than modifiers+arrows. And guess what? Large movements are what we need the most.
But we are lucky bastards, our favorite editor has dozens of motions that run circles around both the arrows and hjkl
: bBeEwW/?*#{}()
and so on. These commands are immeasurably more empowering than hjkl
and for the most part a lot more logical and intuitive if you are used to Ctrl+Right
and friends.
Switching from <Right><Right><Right><Right><Right><Right><Right><Right><Right>
to lllllllll
or even 9l
is ridiculously pointless.
Switching from <Right><Right><Right><Right><Right><Right><Right><Right><Right>
to <C-Right>
is vastly better. From there, switching to w
is both easy and quick. No need to <nop>
your arrows!
If these commands are more useful, more powerful and more intuitive than hjkl
, why in all hell do so many bloggers and commenters insist on replacing the arrows with hjkl
?
I. Have. No. Idea.
In conclusion, hjkl
have their place in our toolbelt because we always have to move by 2 characters or lines but they are more often than not the tree that hides the forest. Be wary of people preaching against the arrows: what they have in store for you may not be better at all.
hjkl
is useless but hjkl
are useful.
Others have described why other ways of moving the cursor around are preferred, but little has been said to answer the question, why is it considered a bad idea to use arrow keys?
I think the main reason (and @Squeezy somewhat alluded to this) is that back in the days of terminals like the VT100, arrow keys produced an escape sequence that was interpreted by the running program. (Since arrow keys aren't represented in ASCII, the typing of arrow keys has to be communicated in some "special" way.) The escape sequence was typically something like
ESC [ A
depending on properties of your terminal, your connection, and your cursor mode. If your shell or your program weren't configured properly for the terminal, they wouldn't understand the the escape sequence as an arrow key.
When I was in college, logging in to Ultrix via VT100 terminals, if I pressed an arrow while in vi in insert mode, the ESC
would be interpreted as "get out of insert mode", and then the following [ A
were interpreted as normal-mode commands. Obviously not what the user wants or expects!
Nowadays, the handling of terminals and arrow keys seems to be more robust, especially in GUI environments. But the same problems do still occur when using vim over SSH or Telnet. (Update: This answer is almost 11 years old. I haven't tested whether this is still true with recent versions of SSH and telnet.)
Using the arrow keys is frowned upon because it forces you to move your fingers from the homerow when using the QWERTY layout and from the character pad (?) when using Dvorak or whichever other layout.
The arrow keys exhibit a problem - it was already known in vi - on slow connections, like on 1200 baud modem. The arrow translates to an ESC sequence like ESC a
. Now when the time betwen ESC and a gets too long, you end up hearing a BEL (or seeing a flash) followed by append mode. This will not appear with the hjkl
keys.
ESC [ A
is a DEC VT-100 terminal (the ANSI terminal was strongly derivative of the DEC VT-100) arrow. IIRC, ESC A
was the ADM-3A arrow, or possibly the DEC VT-52 arrow. The point remains the same, however, the ESC
character sent as part of the arrow key was easily confused with the ESC
typed manually.
Regarding the second question:
[I]f you don't use the arrow keys, how can you move the cursor in the Insert mode?
To one who is master, and who therefore disdains the arrow keys, the concept of "mode" likewise does not exist.
The master adds new text using not an insert mode, but rather complete insert or replacement commands which begin with an opcode such as i
, o
or cw
that is followed by payload text, and terminated by ESC
. Commands have a syntax, and so the machine traverses through states in accepting the syntax of commands, but in the master's model of the editor, these do not give rise to a mode. What the apprentices naively call "mode", is to the master an unfinished command state, a most abhorrent situation which the master avoids.
Whenever the master realizes, whilst in the middle of an insert or replace command, that text must be inserted or deleted other than at the next character position indicated by the cursor, the master terminates the insert command, and unleashes one of the dozens of efficient movement commands (resorting to the lowly hjkl
if
a more powerful way is not obviously applicable).
Also, since the master rarely becomes distracted in such a way as to leave a command unfinished, the master rarely, if ever, causes the editor to beep due to the entry of a superfluous ESC
issued "just in case".
(However, when the network is lagged or the machine is bogged down, the master pounds on the ESC
key furiously and repeatedly, just like everyone else.)
vi
's insert command entry is a mode, then to be fair, we have to identify all such modes in other editors and call them modal. For instance, when we hit Ctrl-X in Emacs, we are in a "mode" in which it is possible, for instance, to hit Ctrl-S to save the document, which will not work outside of that "mode".
Yes, for many of the reasons mentioned above, vi
was originally written to use hjkl
, but "never" is a pretty big statement.
I tend to modify vim
(and other tools I use) to behave like I want. This works great on my machine, but as an embedded developer, I don't always get to pick what is on the machines I support. I come across older machines that only have hjkl
navigation in vi
on occasion. I end up getting the control codes inserted into my file for a second or two until I re-break my habit and switch over to hjkl
.
The only valid reason I can come up with to "never use arrow keys in Vim!" is so you are able to use machines that cannot handle the arrow keys. That feels like a pretty thin reason to say "never."
if you don't use the arrow keys, how can you move the cursor in the Insert mode?
The primary reason why I discourage people from using the arrowkeys is explicitly because it allows you to move around while in insert mode.
One of the weirdest problems with learning vim for newcomers is that vim's undo command operates by undoing the last action, not the last keystroke. Many of my friends end up staying in insert mode the entire time because there is never a reason to leave it. When they ask me "How do I undo?" I reply with "hit the letter u from command mode" they get frustrated when vim clears the entire file away.
"Vim is a pretty worthless text editor if it can't undo things right."
It's difficult to use a tool before you've learned how.
Vim becomes useful only after you've learned how to use it. The power of vim is in how you can tell it "do that thing again" or "undo that thing" or "do that thing only when I hit this macro hotkey" through subconscious muscle memory. If "that thing" becomes "type the entire file from start to finish", I agree, vim isn't that useful at all.
It's for that reason you should only enter text when you're in insert mode
. Avoiding arrow keys is just one way we've found to help you become a better vim user, faster.
Here's my 2¢.
Do whatever you feel comfortable with. If you use different editors it might be easier to use the arrow keys for consistency.
On the other hand, it is absolutely brilliant the vi (and vim) don't need any special keys,except perhaps . You could probably hack an old IBM selectric to function as a terminal.
The other thing to watch out for is that the arrow keys may not work as expected. I have used terminals where they didn't work. I'm not sure exactly how they are implemented in Vim but I'm pretty sure it is not uniform. My spotty memory even seems to recall one implementation where they were implemented as vim macros.
Running vim while shelled into my Android device which is running an ssh daemon in kbox led me to break out of my comfort zone some. Since the Anroid Terminal Emulator on the device uses volume buttons to replace the special key combos (ctrl + whatever), some keys I would normally use on the keyboard don't work in this situation.
For example, the Insert key does not work in this environment so I'm using I
to switch to insert mode. The Esc key, which I usually use to escape insert mode, does not work either so I am using ctrl + [
to escape instead. Home and End keys do not work as I am accustomed to. Instead I'm relying on the 0
and $
shortcuts I never bothered with before to reach the beginning or end of a line.
Anyway, the arrow keys still function as usual in my situation, but it's nice to know that the minimalist keyboard shortcuts are there.
just write the following command before writing in vim :-
one problem with this is that you may have to write it each time.
The name of the game with enhanced text editors seems to be "moving around in a hurry" and reducing wasted/repetitive motion. There is some time lost when you move from the letter keys to the arrow keys (depending on keyboard dimensions) and than back to the home row. But if you can get to the ESC key with your left hand pinky or ring finger switching modes becomes one fluid motion and over the course of an editing session there will be a speed gain.
And this is the key to the second part of your question, get out of insert mode by hitting ESC with your left and move with hjkl with your right. Eventually you'll be able to do this without skipping a beat.
That being said I never got anywhere with Vim until I tried some of the things mentioned here, most important being the "Get efficient: shortcut mappings" section