Why arrow keys are not recommended in Vim

127

28

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

Michael Lee

Posted 2013-05-23T17:24:49.080

Reputation: 649

how can you move the cursor in the Insert mode? Well you don't. Insert mode is only for adding text. Moving around is much easier in the Normal mode with h j k l. – Durga Swaroop – 2016-10-05T11:10:30.120

Because it gets you off the home key, and that can make you mess up other commands and it's really annoying. After a couple of years I think that the most important is to always be on the home row key so you don't do unwanted commands which can then be tedious to undo. – Jorge Fernández – 2019-10-24T19:17:06.643

6The historical responses to this question are interesting. The religious diatribes less so. – Isaac Rabinovitch – 2013-05-24T00:50:31.693

5Good question. This always bugs me about vi[m], as a Dvorak keyboard user it really sucks! – wim – 2013-05-24T03:50:13.590

32I 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 – 2013-05-24T04:59:11.163

1In 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 – 2013-05-24T06:41:59.370

1I'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 – 2013-05-24T07:54:35.787

"Never use arrow keys in Vim!" If you do, you're on the DarkSide :-) – Luc M – 2013-05-24T16:33:03.030

As a Dvorak user I can't not use the arrow keys. – Doug T. – 2013-05-26T15:58:31.583

1@DougT. I'm a Dvorak weirdo too, but I actually like where the hjkl keys are. Up and down are next to each other, and left is the right of right (imagine how tricky it would be if this weren't the case). – Dean – 2013-05-27T22:43:42.290

I used hjkl when I was playing NetHack on a notebook and I didn't have a normal numpad. – Malcolm – 2013-05-28T05:05:06.223

@DougT: I'm a Dvorak user too. The hjkl keys are awkward for cursor movement at first, but one can get used to them. FWIW, I learned vi movement keys a few years before I took up Dvorak layout. – LarsH – 2013-05-30T19:59:07.943

Answers

179

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.

Dean

Posted 2013-05-23T17:24:49.080

Reputation: 2 452

4Breaking 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. – New Alexandria – 2014-09-08T01:52:35.087

@NewAlexandria The arrow mappings were not ideologically inspired: http://xahlee.info/kbd/keyboard_hardware_and_key_choices.html

– Ergwun – 2017-09-15T00:23:18.607

@MatthewScouten actually, vim supports mouse if the terminal supports it. It's actually enabled by default in Git Bash and some other distributions. – SOFe – 2019-09-29T06:57:35.037

1Usually I mark the characters with the mouse (vim goes to VISUAL mode), then I type c and enter the new text. This is faster when the text is enclosed by <> or () e.g.. – ott-- – 2013-05-23T21:16:35.293

1@user1992284: I think my answer actually answers the question. But maybe we'll never know the answer until we can find the particular person who said "never". – LarsH – 2013-05-23T21:18:52.303

4@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. – Matthew Finlay – 2013-05-24T01:28:21.813

6"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". – Nathan Long – 2013-05-24T10:30:23.987

8Hey 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". – Matthew Scouten – 2013-05-24T17:50:46.380

18@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 – 2013-05-24T18:01:48.177

1F(c% is probably the way to go for changing a parenthesized expression. As a bonus it works in vi. – Samuel Edwin Ward – 2013-05-24T19:17:24.170

@Dean And in fact, sentences (or at least phrases) are how vim commands work. dw = "delete word", d2w = "delete 2 words", and so on. Eventually you reach the point where you're typing commands as you think of them, because you don't have to think about "okay, deleting a word is dw", it's just "delete word" and your hands hit dw. Arrow keys don't fit into this narrative because they're so far from the keys that actually do stuff. It's part of why many vim users remap CapsLock to ack like Esc; it's closer and far more useful. – Izkata – 2013-05-24T21:03:23.560

97

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.

Christian Stewart

Posted 2013-05-23T17:24:49.080

Reputation: 667

Great, migrated. Now I can get a little rep on this site as well haha – Christian Stewart – 2013-05-23T17:36:34.367

2Not to mention that the hjkl keys are much more efficient than moving your hands to the arrow keys. – sixtyfootersdude – 2013-05-23T19:02:57.540

2This doesn't answer the question, only explains why hjkl is chosen. – Matsemann – 2013-05-23T20:50:02.643

3This 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 – 2013-05-23T20:50:35.393

@LarsH It answers the first half of the question. – Kruug – 2013-05-23T21:04:42.553

I have added additional detail. it would not be a good idea to duplicate so much text when such a nice answer was provided already. I have instead added a resources section with some links to some very extensive answers on SO. – Christian Stewart – 2013-05-23T21:08:09.720

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 – 2013-05-23T21:16:32.453

@LarsH I have added additional information :) – Christian Stewart – 2013-05-23T21:19:06.297

1Now I understand why Vim uses escape to exit insert mode^^ Cool picture! – Étienne – 2013-05-23T21:31:27.460

carriage return, line feed, separate keys. love it. – lynks – 2013-05-24T10:37:13.173

57

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.

romainl

Posted 2013-05-23T17:24:49.080

Reputation: 19 227

1Don'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. – Aviator45003 – 2014-08-04T19:27:40.477

@T.C. Any examples to make the whole debate more interesting? – romainl – 2014-08-04T19:56:01.403

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. – 2013-05-24T08:52:30.260

1Thanks. 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 – 2013-05-24T09:43:40.940

5Same here: in the end I liked your answer, but I think the tone came across as unnecessarily ranting/trolling/flaming. – UncleZeiv – 2013-05-24T17:43:40.853

18

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.

LarsH

Posted 2013-05-23T17:24:49.080

Reputation: 972

14

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.

pdoherty926

Posted 2013-05-23T17:24:49.080

Reputation: 239

4Wow. 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. – Renan – 2013-05-23T17:27:47.200

13Why does moving you hand imply thinking? – EBGreen – 2013-05-23T17:41:25.950

Neo solves that problem nicely by integrating cursor keys into the character area – knittl – 2013-05-23T18:52:26.643

8well, 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. – Frank Thomas – 2013-05-23T18:58:50.423

@FrankThomas That's assuming you've already left command mode. – pdoherty926 – 2013-05-23T19:06:31.527

@FrankThomas You do not have to hit ESC before using hjkl. Type "vi <filename>" and there you go: you can use hjkl. If you have an insert/replacement command in progress, you must of course use ESC to finish it, othewrise commands like hjkl will be interpreted as a continuation of that command's payload. – Kaz – 2013-05-24T00:37:58.080

may have been because we had to ssh in. – Frank Thomas – 2013-05-24T01:40:13.710

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. – Nathan Long – 2013-05-24T10:33:54.847

@NathanLong and you'd save a couple seconds a day by not moving with the arrow keys. By the end of your lifetime you could say you were a few hours more productive than the average user. Whatever floats your boat, I guess. I still think that moving with hjkl is more error prone since it's easier to hit any of the neighbouring keys. – Renan – 2013-05-24T12:37:58.837

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. – Nathan Long – 2013-05-24T12:53:26.560

@FrankThomas Your confusion seems to come from using Insert mode as the usual mode of operation; that's not using vi(m) effectively at all. Normal mode (the mode you're in after hitting ESC) is the usual mode of operation, and Insert mode you should only be in when actually typing new text. – Izkata – 2013-05-25T06:02:16.197

What's the "character pad"? The area of the keyboard where the letters are? – LarsH – 2013-05-30T19:54:46.723

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.

ott--

Posted 2013-05-23T17:24:49.080

Reputation: 1 911

I think ESC [ A is what you're referring to? – LarsH – 2013-05-23T21:03:31.533

2ESC [ 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 – 2013-05-23T22:12:00.770

@RBerteig: Good to know. – LarsH – 2013-05-25T05:46:03.310

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

Squeezy

Posted 2013-05-23T17:24:49.080

Reputation: 5 930

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 – 2013-05-23T20:57:41.153

I tried to express that you do not WANT to move the cursor in Insert mode at all. – Squeezy – 2013-05-23T21:49:37.383

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 – 2013-05-24T02:46:36.273

5

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

Kaz

Posted 2013-05-23T17:24:49.080

Reputation: 2 277

2Remember, 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 – 2013-05-24T03:45:30.853

3

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

kmort

Posted 2013-05-23T17:24:49.080

Reputation: 1 609

3

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.

Conrad.Dean

Posted 2013-05-23T17:24:49.080

Reputation: 738

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.

iyrin

Posted 2013-05-23T17:24:49.080

Reputation: 111

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.

0decimal0

Posted 2013-05-23T17:24:49.080

Reputation: 103

well this would be useful when you want to use arrow key in insert mode – 0decimal0 – 2013-05-25T14:31:15.233

0

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.

David E.

Posted 2013-05-23T17:24:49.080

Reputation: 51

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

Vijay Edwin

Posted 2013-05-23T17:24:49.080

Reputation: 1