Editing text with Vi/m

In Linux everything it’s considered a file, for that reason editing files become such an important task in the system admin world.

Vi or Vim it’s the text editor by default in CentOS and the same comes within almost all the Linux distributions in the market, to be quite honest I used to hate Vi the first time I used it, I felt it was very confusing and useless to learn, instead I started to use Nano (Another text editor) which it’s so much easier to use. However when you start to dig more into the Linux world start to realize the potential of this tool could have.

At the beginning, what it’s more difficult to understand it’s that Vi doesn’t work like a normal text editor, instead Vi have two principal operational modes called “Insert” & “Command”.

Command Mode

From the command mode, we can do a lot of stuff such as: move through the document using the arrows keys, save changes, exit the document, search and substitute words and much more.

If want you are looking for it’s no covered in this post, I will urge to use the man and help page where you can find all the detailed information about it, I will tell you how to reach the man and help page at the end of this post.

Let’s get started

To create a file with just type Vi and the text file + extension.

$vi version1.txt

As you start typing you will be automatically changed to the INSERT mode. We can tell we are in the Text mode because we see it at the end of the text, as the followed image:


Once you are done with your changes to go from INSERT to COMMAND you only need to type (ESC) and easy enough you are in command mode again.

From command mode is where the magic happens, there are a lot of options available from this mode, for this reason, I will try to explain the basic and the ones I have found useful.

Saving Changes

To save our documents we have to begin typing the character “:”, immediately it will show up at the end of the text as we see in the next image, for saving and quit the document the commands are the following:

:q = for quit the file

:w = for save/write

:wq = for save and quit (at the same time)

:q! = to enforce quitting without saving


After press enter, we are back at the console.

P.S. Bear in mind you need to have write permissions to the file you want to edit, Otherwise, an error will show up as below:


Reading documents using Vi / Insert mode

Once you know the basics Vi it’s not that bad, by default when you open a file with Vi you will start in command mode.There are few ways to change to insert mode  and  the commands are the following:

Type “i” for insert

Type “a” for append

Type “o” for open

The one that I use most it’s “i” because for me it makes more sense. Once you type the letter you will find the new mode at the bottom of the screen, with big INSERT as the image below, from now on you can edit text make modifications and move through the text using the arrows keys. Once you are done to save your work you have to use the ESC key to go back the command mode.

Opening a file

$vi /usr/share/doc/vim-common-7.4.160/docs/help.txt —> (Please note the path I wanted to show you also where you can find all the helpful information to manage Vi as a pro.)


Editing the file “WE CAN EDIT AS WE WANT”


Because this is a system file owned by root, we won’t be able to change it. However, even if we don’t have the root password we should be able to edit our own files without problems.

There are other cool options that I found during my learning and I would like to share with you.


If we search for a specific word inside of a document, we have to be from the command mode and the syntax is the following:

/word = to search forward

?/word  = to search backwards

n =  to jump to the next result


Please bear in mind that the searches are case sensitive

Copy and Paste

We can also copy and paste from command mode as weird as it could sound and the commands are the following:

Type “y” = for select one single line

Type “p” =  To paste

Using Yank for copy and paste a single line as below:


Copying a selection

Type “v” to go to VISUAL model as below:


Hold “y” to copy

Type “p” to paste in another place.

Example copy and paste



Another command I found useful was “:set number” which allows you to enumerate each line of your documents. This is particularly useful for debugging when you are looking for a specific error in a long code.


Vi it’s not only useful for reading or editing simple files, it’s also quite intuitive if you are programming or making a script to automate a task. Vi separates by colors the command and helps you to build you program in harmony, rather that nano that only offers you a plain text color. Also, we can substitute words really easy from command mode.

“:% S / text to be substituted / substitute text / g” = In this example “s” stands for substitute and “g” for global which applies the changes for the whole text.


If you want to look for more information about Vi here are some command can help you:


$man vi

$ls /usr/share/doc/vim-common-7.4.160/docs

$info man

And from the command mode in vi if you type “:/help” you will be redirected to the help.txt file in this path “vi /usr/share/doc/vim-common-7.4.160/docs” to access all the official documentation for this awesome text editor.

I hope you like it!

See you in the next post









Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s