98

I am new to vi, actually I have started learning vi from today and I have got stuck at the behavior of the backspace key.

Actually when I fired up vi on my Ubuntu 12.04 for the first time my backspace key was working normally but after that it has started behaving strangely. Whenever I press the backspace in the insert mode it just moves one place to the left instead of erasing the character.

How can I get back the default backspace functionality? Please note that I don't want to install vim or set nocompatibilty.

Sparhawk
  • 6,929
Saprativa Bhattacharjee
  • 1,337
  • 3
  • 11
  • 10

4 Answers4

90

Here is the simplest solution.
Open a terminal, go to home directory and type

vi .vimrc

a new file open now add these lines to the file and exit by saving

$ set nocompatible
$ set backspace=2
Zanna
  • 70,465
Adithya Chakilam
  • 1,232
  • 1
  • 10
  • 13
50

That's correct behavior for vi, and it does erase the character, it just doesn't show it by replacing it with a blank like in vim. It will be apparent when you overwrite the erased character with another character, or switch back to command mode.

E.g. starting with command mode on an empty line, the following will result in the line containing fo:

ifooBackspaceEsc

Before you hit the Esc key, the line will read foo, but the last o has been tagged as an "erase-column". See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/vi.html#tag_20_152_13_88 (mainly point 4 under that heading, and also browse a little further down to read about <Control>-H)

geirha
  • 46,101
  • I spent at least couple of hours trying to figure this out (at times cursing my Windows terminal app too), finally it worked. Thank you! – Pratik Patel May 30 '20 at 06:24
34

You need to change to the "insert" mode by moving the cursor with i. There are other ways to delete characters directly. You can download a powerpoint that I use when teaching a class on VI here.

It sounds like you are in the "Command" mode. To move your cursor:
H = Left, J = Up, K = Down, L = Right

Once you have your cursor positioned you can delete text as follows:
x Deletes the character under the cursor
X Deletes the character before the cursor
dw Deletes from the cursor to the next word
dd Deletes the line the cursor is on.

To enter text, you can use one of the text entry modes.
a Adds text to the right of the cursor
A Adds text to the end of the current line
i Adds text to the left of the cursor
I Adds test to the beginin of the current line
o Opens a new line below the current line and places you in text entry mode
O Opens a new line Above the current line and places you in text entry mode

To exit Text entry mode, and return to the Command mode, use Esc.

To Undo changes: (A student favorite)
u Undo the last command entered
U Undo all changes to the ** current line**

To Save/Quit: :w Writes (Saves) the file and remains open
:wq Writes (Saves) the file and exits VI
:q Quits (Exits) if you've made no changes
:q! Quits (Exits) without saving changes
ZZ Writes (Saves) the file and exits VI (same as :wq)

Argusvision
  • 1,711
  • 12
    The OP mentioned that the mysterious behavior occurs in insert mode. So this is not the answer to the question asked. – LarsH May 16 '13 at 20:50
  • Actually the OP did not state in insert mode. The question was edited. – Argusvision May 16 '13 at 21:04
  • 1
    Ok. You're correct. It looks like I made an assumption. It's been my experience that most people new to VI run into issues with switching modes, and navigating when in the command mode. My students often complain and blame VI for a having erratic behaviour when it's simply user error, or not having a grasp of the command set. – Argusvision May 17 '13 at 12:28
  • 1
    Understandable. I agree that is the common reaction to non-insert-mode. – LarsH May 17 '13 at 13:43
  • Argus, "in insert mode" was added to the question title in an edit, but it was in the body of the question from the beginning. – LarsH Sep 09 '15 at 19:35
  • I always get annoyed by not being able to use an i from the last position when I want to append something else to the line. I know a exists, but I always seem to do i, then remember, hit esc and then a – mpag Jan 11 '17 at 21:24
  • @Argusvision - "blame VI" - you say that as if you don't think there's an issue with vi. Why does running apt-get update cause the behavior to change then? That tells me the old behavior was recognized as an error - the only question is why modern distros come with a buggy version of vi. – ArtOfWarfare Jan 21 '18 at 13:45
  • any suggestion on how can I delete a new line char before the cursor? The X command doesn't deletes a new line char before the cursor. – Deepam Gupta Apr 24 '21 at 17:11
24

Install the full vim package to get the backspace and arrow key functionality

sudo apt-get update
sudo apt-get install vim
Zanna
  • 70,465