Getting started with the default Linux editor

Sep 21, 2006 13:27 GMT  ·  By

Vi is a default text editor and it's available in almost all Linux distributions by default. Vi is a Visual editor that was designed to be used in the CLI (Command Line Interface). You might encounter situations where you'll need to edit or create some file from the command line, and that's when you'll need to use the vi editor because others might not be available.

Vi is relatively hard to use and it can't really be learned without studying some basic commands. In this guide, I intend to teach you the basics of vi and my advice to you is to try remembering the commands, because in a situation when you'll need Vi, there is a big probability you will not have access to the Internet.

To start the vi editor, all you have to do is type vi in a command line, followed by the name of the file. If the file specified exists, vi will open it and will allow you to edit it. In the bottom left corner, you'll be able to see the name of the file you opened and the number of lines and characters. If it doesn't exist, vi will create a new file with the name you've specified. In this case, in the bottom left corner you'll see the name you entered and that vi tells you that you have created a new file.

Now you are in the vi editor. You'll not be able to enter your text at this point and I'll tell you why. Vi has two modes: command and insert. The command mode allows you to manipulate text and perform various operations in vi and the insert mode is for typing characters in your file. To get into insert mode, you'll have to type either i or a. To get out of the insert mode, just hit the Esc key.

The most important thing after you modified your file is probably to save it. To do that, you'll have to go in command mode with the Esc key and then enter the command :w to save your file. If you want to save your changes in a different filename, use :w newfilename.

Now, I guess, you'll probably want to exit vi. To do this, you'll need to enter the :q command. If you haven't saved anything, vi will warn you about that and will not exit. If you want to exit without saving your changes, you'll have to use the :q! command.

Some of the commands can be combined, so if you want to quit and save the file in the same time, you can just enter the :wq command and you're good to go.

Other basic vi commands:

I'll show you only some of the other basic vi commands because even though vi is a very powerful text editor, there isn't really a reason for using it daily. If you want power, you have the Open Office Word Processor in the graphical interface. I just don't want to fill anyone's memory with useless stuff :).

u - undo the last change to the file. If you type u again, you'll re-do the change. r - replace one character under the cursor. x - delete character under the cursor. k - move the cursor up one line. j - move the cursor down one line. h - move the cursor to the left one character position. l - move the cursor to the right one character position. dd - delete the current line. p - paste after the cursor position.