Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 18 down vote accepted

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)

share|improve this answer
3  
+1 for reading the question carefully and addressing the actual issue. –  LarsH May 16 '13 at 20:49

Here is the perfect solution open the terminal and type

$ vi .vimrc

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

$ set nocompatible
$ set backspace=2
share|improve this answer
4  
The OP mentioned that they don't want to use nocompatibility –  minerz029 Nov 3 '13 at 12:47
    
there is no need for no compatability u can also do it just by addig the second line –  Adithya Chakilam Nov 3 '13 at 12:49
    
Second line only works for me if I also add the first line. –  djjeck Apr 2 at 21:03

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)

share|improve this answer
4  
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
    
Understandable. I agree that is the common reaction to non-insert-mode. –  LarsH May 17 '13 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.