Vi, The Basics of Editing Text on the Command Line Without Pulling Your Hair Out.

Security Analyst’s spend a lot of time on the command line in Linux so it makes sense to have a utility for editing text available. Vi and it’s improved cousin VIM fit the bill nicely; however most folks are intimidated by all of the hot keys and modes. Text editing with Vi is really easy and quick once you learn the basics, and that is today’s topic.

The accompanying video will be more of a demo to show how easy and fast editing files with Vi can be. The post will not have any images since images of text are not really worth the effort.

Starting Vi

Starting Vi is simple, just invoke the command with the file name you want to edit. Just make sure the file you are going to edit is a text file; no word doc’s here.

vi existing-file.txt

To create a new file to edit just replace the existing files name with the new file’s name.

vi new-file-name.txt

The examples I have used have file extensions of .txt, this is irrelevant and it could be anything you like or nothing at all.

Vi’s Modes of Operation

Vi has two modes of operation, command mode and insert mode. The two modes have different functions and as you can probably tell by the names of the modes one is for inserting text and the other commands. Command mode assigns keys on the keyboard to perform different functions, like moving the cursor or deleting text. To enter command mode just hit the escape key, fyi Vi starts in command mode. Insert mode allows you to insert/edit text. To enter insert mode just hit i and you will see insert at the bottom left of the screen.

Editing the text in the file

Insert mode is how you edit the text and there are two ways you usually enter insert mode: i or a. Entering i will put you in insert mode and all text typed will be in front of the cursor. Consequently entering insert mode by typing an a will put the new text to the right of the cursor. Handy if you want to add something to the end of a line.

Deleting text is just as important as entering it. Vi allows you to delete text or whole lines easily when in command mode; just type dd to delete a whole line or if you need to delete more than one line just add the number of lines to delete in front of the dd, i.e. 3dd will delete three lines. In insert mode the delete key will delete text as well.

Moving around in the document is done by using the arrow keys and backspace key (home and end work as well).

Closing and Saving

Exiting out of insert mode by hitting the escape key does not save the file or exit Vi, it just returns you to command mode. Saving the file is done by entering the command:


Hitting the escape key followed by the colon preps the system for the command, in this case w for “write”. Exiting the program is accomplished in much the same way; just use q for “quit” instead of w for “write”. If by chance you want to exit without saving your changes just add an ! after the q:


That is all you need to get going with Vi or VIM and should cover about 99% of what you would need. I hope this was useful and will keep you from pulling your hair out.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.