145

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

Greenonline
  • 2,235
  • 11
  • 24
  • 30
Mike Lee
  • 649
  • 2
  • 6
  • 8
  • 9
    The historical responses to this question are interesting. The religious diatribes less so. – Isaac Rabinovitch May 24 '13 at 00:50
  • 5
    Good question. This always bugs me about vi[m], as a Dvorak keyboard user it really sucks! – wim May 24 '13 at 03: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 '13 at 04: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 '13 at 06: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 '13 at 07:54
  • `"Never use arrow keys in Vim!"` If you do, you're on the DarkSide :-) – Luc M May 24 '13 at 16:33
  • 1
    related: http://ux.stackexchange.com/questions/29564/unintuitive-order-of-vi-directional-keys – Handsome Nerd May 25 '13 at 17:05
  • As a Dvorak user I can't not use the arrow keys. – Doug T. May 26 '13 at 15:58
  • 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 May 27 '13 at 22:43
  • I used `hjkl` when I was playing NetHack on a notebook and I didn't have a normal numpad. – Malcolm May 28 '13 at 05:05
  • @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 May 30 '13 at 19:59
  • `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 Oct 05 '16 at 11:10
  • 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. –  Oct 24 '19 at 19:17

14 Answers14

191

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
  • 2,720
  • 1
  • 23
  • 24
  • 1
    Usually 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-- May 23 '13 at 21:16
  • 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 May 23 '13 at 21:18
  • 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. – Matthew Finlay May 24 '13 at 01: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"`. – Nathan Long May 24 '13 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". – Matthew Scouten May 24 '13 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 '13 at 18:01
  • 1
    `F(c%` is probably the way to go for changing a parenthesized expression. As a bonus it works in vi. – Samuel Edwin Ward May 24 '13 at 19:17
  • @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 May 24 '13 at 21:03
  • 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. – New Alexandria Sep 08 '14 at 01:52
  • @NewAlexandria The arrow mappings were not *ideologically* inspired: http://xahlee.info/kbd/keyboard_hardware_and_key_choices.html – Ergwun Sep 15 '17 at 00:23
  • @MatthewScouten actually, vim supports mouse if the terminal supports it. It's actually enabled by default in Git Bash and some other distributions. – SOFe Sep 29 '19 at 06:57
  • the ironic part is; there is no way to scroll to vim's command history without the arrow keys. at least none that i know of – Fuseteam Nov 18 '21 at 19:19
  • 6 mins after i say that i discover ctrl+n and ctrl+p gg didn't see this in the documentation – Fuseteam Nov 18 '21 at 19:27
102

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
  • 704
  • 2
  • 5
  • 15
  • Great, migrated. Now I can get a little rep on this site as well haha – Christian Stewart May 23 '13 at 17:36
  • 2
    Not to mention that the hjkl keys are much more efficient than moving your hands to the arrow keys. – sixtyfootersdude May 23 '13 at 19:02
  • 3
    This doesn't answer the question, only explains why *hjkl* is chosen. – Matsemann May 23 '13 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 '13 at 20:50
  • @LarsH It answers the first half of the question. – Kruug May 23 '13 at 21:04
  • 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 May 23 '13 at 21:08
  • 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 '13 at 21:16
  • @LarsH I have added additional information :) – Christian Stewart May 23 '13 at 21:19
  • 1
    Now I understand why Vim uses escape to exit insert mode^^ Cool picture! – Étienne May 23 '13 at 21:31
  • carriage return, line feed, separate keys. love it. – lynks May 24 '13 at 10:37
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.

romainl
  • 22,554
  • 2
  • 49
  • 59
  • 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 '13 at 08: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 '13 at 09: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 '13 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. – Aviator45003 Aug 04 '14 at 19:27
  • @T.C. Any examples to make the whole debate more interesting? – romainl Aug 04 '14 at 19:56
  • this is really well thought out and insightful. i have been wondering why its so sacrilegious to use the arrow keys whey they work just fine, especially on my laptop keyboard where the jump from hjkl is really not that far from my arrow keys. tbh, i could intuitively use hjkl arrow keys if .vimrc was configured to use j as up and k as down, and im assuming most systems you will work on will allow to to configure your .vimrc file which you will be able to use temporarily while you work on the system and then remove and replace with the old (if there was one) .vimrc that was there. – trevorkavanaugh Jun 07 '22 at 10:48
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.

LarsH
  • 1,014
  • 1
  • 8
  • 19
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
  • 249
  • 1
  • 7
  • 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 '13 at 17:27
  • 13
    Why does moving you hand imply thinking? – EBGreen May 23 '13 at 17:41
  • [Neo](http://neo-layout.org) solves that problem nicely by integrating cursor keys into the character area – knittl May 23 '13 at 18:52
  • 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. – Frank Thomas May 23 '13 at 18:58
  • @FrankThomas That's assuming you've already left command mode. – pdoherty926 May 23 '13 at 19:06
  • @FrankThomas You do not have to hit ESC before using hjkl. Type "vi " 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 May 24 '13 at 00:37
  • may have been because we had to ssh in. – Frank Thomas May 24 '13 at 01:40
  • 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 May 24 '13 at 10:33
  • @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. – Dogbert May 24 '13 at 12:37
  • 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 May 24 '13 at 12:53
  • @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 May 25 '13 at 06:02
  • What's the "character pad"? The area of the keyboard where the letters are? – LarsH May 30 '13 at 19:54
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--
  • 2,201
  • 1
  • 15
  • 15
  • I think `ESC [ A` is what you're referring to? – LarsH May 23 '13 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 '13 at 22:12
  • @RBerteig: Good to know. – LarsH May 25 '13 at 05: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.)

Kaz
  • 2,631
  • 1
  • 18
  • 23
  • 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 '13 at 03: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
Squeezy
  • 9,232
  • 1
  • 16
  • 18
  • 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 '13 at 20:57
  • I tried to express that you do not WANT to move the cursor in _Insert mode_ at all. – Squeezy May 23 '13 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 '13 at 02: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."

kmort
  • 1,711
  • 3
  • 17
  • 26
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.

Conrad.Dean
  • 868
  • 7
  • 15
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.

David E.
  • 51
  • 2
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
  • 161
  • 2
  • 7
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
  • 103
  • 3
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