11

I am working on a ChromeOS computer with both Vi and Vim. When I open up the editor to an existing document, I type A to begin editing. When I try to move around with my arrow keys, the following happens:

vim and vi arrow keys

I have tried both Vim and Vi, and both of the have this problem. How do I move around in my document while editing it?

muru
  • 24,838
  • 8
  • 82
  • 143
Elliot A.
  • 113
  • 1
  • 8

3 Answers3

8

I do need to point out the elephant in the room: You shouldn't be navigating in insert mode.

Insert mode is for inserting text. While many implementations of vi allow you to navigate (using the arrow keys) while in insert mode, the recommended, normal method of navigation is to navigate in normal mode. Normal mode should be the mode you are normally in, hence the name.

The topic of modal editing vs. arrow keys is well described (at a beginner level) under "Misconception #1" in this excellent article.

As an additional point of background, the terminals upon which vi was originally designed didn't even have arrow keys. On some old keyboards the HJKL keys are additionally labelled with arrows; I don't actually know if this came before or after vi but I suspect the former.

I highly recommend working your way through the vimtutor (on the command line) and reading through the above linked article to start getting a handle on the power of modal editing. I suspect that the problem of non-functioning arrow keys will drop into total insignificance once you get a feel for the powerful motion commands available in normal mode. I know I personally never use arrow keys while editing text in Vim, and I feel handicapped when using other editors where I must use the arrow keys to navigate.

This site itself will also be of great assistance; for instance, here are a couple posts you may find helpful:

As to the nitty-gritty of Chrome OS and how it interprets arrow key presses in your terminal, I suspect that debugging it will go beyond the scope of this site. If you are truly determined to use arrow keys in vi, perhaps you will have better luck with the keyboard-to-terminal debugging issue amongst the knowledgeable people over at superuser.com. Best of luck to you whatever you decide, and I hope you may come to appreciate the power of Vim even with non-arrow-key only usage patterns. :)


P.S.: The most basic motion commands there are, are h, j, k, l. To these you can add <Space>, <Backspace>, <Return> and -, and of course the great favorites w, b, e and their whitespace-separated-word counterparts W, B, E. If you add in sentence motions ( and ), paragraph motions { and }, searching the word under the cursor forward or backward with * and # respectively, and same-line character search with f, t, F, T, ; and ,, and if you actually learn all of those well, or even most of those (which won't take you very long, actually)...I can promise you with total confidence that you will never go back to using arrow keys to navigate text within Vim.

This isn't even a complete list of motion commands; far from it. To see a more complete list along with documentation for each, start Vim, type :help motion.txt, and use j and k as your "down" and "up" keys to scroll through the document.

Wildcard
  • 4,409
  • 26
  • 49
  • 2
    "You shouldn't be navigating in insert mode" - > Because pressing the down arrow twice is somehow not as fast as pressing <Esc>jji. Never mind that <Esc> is also outside the home row, just like the down arrow. – Martin Tournoij Jan 29 '16 at 02:19
  • 1
    @Carpetsmoker, if a down arrow keystroke could be properly replayed as part of a dot command, I would probably do that sometimes. Since it can't...I don't bother with arrow keys; <C-o>2j is still faster. – Wildcard Jan 29 '16 at 02:40
  • 1
    Also, I haven't touched the <Esc> key a single time since learning that <C-[> does the same thing. – Wildcard Jan 31 '16 at 07:49
  • 1
    I've mapped my caps lock key to esc, it's useful in everything not just vim. – minseong Nov 09 '17 at 10:06
0

An alternative to @wildcard's answer is to just edit or create ~/.vimrc and put the following line in there :

set nocompatible
statox
  • 49,782
  • 19
  • 148
  • 225
Orabîg
  • 109
  • 3
  • 1
    If you have a vimrc at all, nocompatible will be automatically set, and setting it yourself has side-effects. For that reason, current advice is to create a vimrc but not put :set nocompatible in it. – D. Ben Knoble Feb 15 '22 at 14:58
  • I'm sorry, but you're wrong @D.BenKnoble. I just tested what you said, and creating an empty .vimrc does not fix the issue at all (it would have been very weird). But adding the line in it does. – Orabîg Feb 16 '22 at 11:54
  • This may depend on your version. But see :help compatible which definitely states that when a vimrc is present this option is automatically set. – D. Ben Knoble Feb 16 '22 at 12:30
  • If it is confirmed that it is not always the case, where's the point of this comment ? – Orabîg Feb 16 '22 at 15:14
  • 1
    From :h 'compatible' When a vimrc or gvimrc file is found while Vim is starting this option is switched off this is always the case. The issues here are that 1) setting nocompatible is not a universal way to fix OP's issue 2) setting 'nocompatible` in your vimrc is useless as the doc says. – statox Feb 25 '22 at 09:30
  • Well, if you're both right , then my eyes are lying to me :) (I have vim 8.0.707) No matter what the docs say, if I create (touch) ~/.vimrc, then the issue is still there, but if I do echo "set nocompatible" > ~/.vimrc then it's gone... – Orabîg Feb 26 '22 at 22:40
0

First thing I do is vi ~/.vimrc echo "set nocompatible" > ~/.vimrc

PS. It is incredible how people ignore the simple fact that vi is totally unusable since decades. It was great in 1980s but not in 2000s (and now is already 2022).

Denis
  • 9
  • 1
  • Welcome to Vim :-). I have the impression that your answer is very similar to this one. I would recommend to be careful with opinion in answer they tend to offense and create usefulness debate ;-) – Vivian De Smedt Oct 05 '22 at 07:47