147

"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?

13
  • 9
    The historical responses to this question are interesting. The religious diatribes less so. May 24, 2013 at 0:50
  • 5
    Good question. This always bugs me about vi[m], as a Dvorak keyboard user it really sucks!
    – wim
    May 24, 2013 at 3:50
  • 35
    I remember when first using vi, many years ago, a senior colleague advising me "Make sure you learn to use the h, j, k, and l keys because one day you'll end up on machine at a client site where the arrow keys don't work, and you'll look like a complete d***head if you can't even work vi."
    – Ergwun
    May 24, 2013 at 4:59
  • 1
    In insert mode you still can move the cursor using Ctrl-O then a normal mode command, and it will go back to insert mode. Also check CTRL-G CTRL-J and CTRL-G CTRL-K which will move the cursor to the next and previous line, at the column you started inserting.
    – Benoit
    May 24, 2013 at 6:41
  • 1
    I've developed a load of bad vi[m] habits over the years. I think I need to use it over a very slow connection, to force me to use better habits.
    – aidan
    May 24, 2013 at 7:54

14 Answers 14

192

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.

13
  • 5
    @ott-- 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. May 24, 2013 at 1:28
  • 7
    "put your cursor anywhere in the string" - and again, whereas in insert mode you'd use the arrows to get there, in normal mode you could do /want and hit enter to land on the word 'want', then do ci". May 24, 2013 at 10:30
  • 8
    Hey this is neat! What a fast an powerful way to move the cursor around. The only way to improve it would be some way to magically point at where you wanted the cursor, and have it just jump there. Maybe we can invent something. I think we should call it a "mouse". May 24, 2013 at 17:50
  • 20
    @MatthewScouten No one is claiming that vim is simpler than a mouse. With a bit of practice though, it's much, much faster. Using a mouse is like pointing and grunting to get your point across. Vim lets you speak in full sentences.
    – Dean
    May 24, 2013 at 18:01
  • 6
    Breaking habits is a fine answers - but forcibly preventing someone from a normal activity is fascist. Worse, the usability of (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. Sep 8, 2014 at 1:52
104

Key Placement

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.

Keyboard

Since vim is derived from vi, it uses the same hjkl keys.

New Habits

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.

Resources and Insert Mode

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.

10
  • 2
    Not to mention that the hjkl keys are much more efficient than moving your hands to the arrow keys. May 23, 2013 at 19:02
  • 4
    This doesn't answer the question, only explains why hjkl is chosen.
    – Matsemann
    May 23, 2013 at 20:50
  • 3
    This is true and interesting history, but the OP asks how you move the cursor in insert mode. hjkl don't work in insert mode, so how is this an answer to the question?
    – LarsH
    May 23, 2013 at 20:50
  • 3
    @Kruug, the first part of the question is why someone said never to use arrow keys in vim. The fact that you can use 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.
    – LarsH
    May 23, 2013 at 21:16
  • 1
    Now I understand why Vim uses escape to exit insert mode^^ Cool picture!
    – Étienne
    May 23, 2013 at 21:31
67

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.

6
  • At first, I thought your were going to say that 'hjkl' are useless, but good catch, word/paragraph/sentence motions are much more useful that the mantra regarding single character motion.
    – Xavier T.
    May 24, 2013 at 8:52
  • 1
    Thanks. I had a similar discussion on HN the other day which derailed because of poor wording. Focusing one's efforts on hjkl is useless but hjkl are useful.
    – romainl
    May 24, 2013 at 9:43
  • 6
    Same here: in the end I liked your answer, but I think the tone came across as unnecessarily ranting/trolling/flaming.
    – UncleZeiv
    May 24, 2013 at 17:43
  • 3
    Don't call them useless, the argument about speed is a good one, and the way HJKL work with other VIM commands in a way that the arrow keys don't is an important one as well. Aug 4, 2014 at 19:27
  • @T.C. Any examples to make the whole debate more interesting?
    – romainl
    Aug 4, 2014 at 19:56
19

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.

15

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.

12
  • 5
    Wow. Seriously? I don't want to believe that, as it would imply that the people who designed Vim would think that their users spend more time typing than thinking.
    – Dogbert
    May 23, 2013 at 17:27
  • 14
    Why does moving you hand imply thinking?
    – EBGreen
    May 23, 2013 at 17:41
  • 8
    well, when I had to do it in school, you had to hit esc before you could use the hjkl navigation, so you had already removed your left hand from the home row to get your pinky up to Esc. I recall that to go back one space and insert a character you had to enter 'Esc : h i' and then type the character you wanted to insert. thats not so helpful. May 23, 2013 at 18:58
  • 1
    @Renan - you're assuming 'typing' means 'inserting text'. I have my fingers on the home row when I'm reading and thinking, too, because the keyboard is how I navigate in Vim: searching to move, opening other files to compare code, hopping around to correct spelling errors, etc. May 24, 2013 at 10:33
  • 2
    @Renan - 1) you underestimate the savings and 2) little things add up. I've seen touch-typing Vim power users edit at 10x the speed of other developers. At sufficient speed, editing becomes automatic and does not interrupt one's thoughts or flow. It is a pleasure to work this way, so it does float my boat, but I'd wager it's a bigger productivity boost than you think. Try watching coders like Gary Bernhardt as an example. May 24, 2013 at 12:53
10

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.

3
  • I think ESC [ A is what you're referring to?
    – LarsH
    May 23, 2013 at 21:03
  • 2
    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.
    – RBerteig
    May 23, 2013 at 22:12
  • @RBerteig: Good to know.
    – LarsH
    May 25, 2013 at 5:46
6

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.)

1
  • 3
    Remember, if 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".
    – Kaz
    May 24, 2013 at 3:45
5
  • the Arrow keys may not work well in certain environments. hjkl are normal characters and well understood by all kinds of terminals.
  • You don't move the cursor in Insert mode (except for Backspace and correcting a short typo), the idea of vi is to enter Insert mode only to enter something. Movement is done in Normal mode
  • If you can, you should avoid using hjkl for moving whereever applicable, and instead only use them for directions. There are numerous options to move in vi, see: http://vim.wikia.com/wiki/Moving_around
3
  • Actually you can move the cursor in insert mode, at least to the left, using backspace. But I agree you can't move it freely.
    – LarsH
    May 23, 2013 at 20:57
  • I tried to express that you do not WANT to move the cursor in Insert mode at all.
    – Squeezy
    May 23, 2013 at 21:49
  • Sorry, I misunderstood what you wrote as saying you can't move the cursor in insert mode. Actually I do want to move the cursor in insert mode: if I'm entering some new text and I hit the wrong key, I want to use backspace to correct it. But beyond that limited use, I agree.
    – LarsH
    May 24, 2013 at 2:46
4

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."

4

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.

1

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.

0

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.

0

just write the following command before writing in vim :-

  1. esc
  2. :set nocp

one problem with this is that you may have to write it each time.

1
  • well this would be useful when you want to use arrow key in insert mode
    – 0decimal0
    May 25, 2013 at 14:31
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .