Linux vi editor tutorial - Create and edit file in command line terminal

The vi editor has a long history in UNIX. It is a default text editor of the UNIX and UNIX based operating system including Linux. Some people say vi is difficult to use. Here is a good news for you, the improved version of Vi editor which is called Vim editor is a lot easier and user friendly. In this tutorial, I'll just mention Vi but it's the same as vim. I'll mention some vim improvements over original vi editor at the end of this tutorial.

Create new file with vi editor

In the Linux permission tutorial you have been introduced briefly to vi editor. You probably still remember that vi has 2 modes, command mode and insert mode. To start vi from the command line, type:

vi filename

or

vim filename

The vi command will create a new file if the filename you mention in the command above does not exist in the current directory. You’ll have a screen with a column of tildes (~) on the left screen like the example below:.

[email protected]:~$ mkdir vitutorial
[email protected]:~$ cd vitutorial/
[email protected]:~/vitutorial$ vi vitest

vi create a new file picture here image

To insert text to the new file, press i. Now you can start typing what you want. You probably notice that delete key and backspace key didn't work. If you want to delete text, you must enter vi command mode. Press Esc to go the vi command mode. To delete a character, move cursor to the character and press x to delete that character. To delete a line, move cursor to the beginning of the line and press dd. To continue insert the text again, press i or this time try press a to append text. The different between i and a command in vi is, a will move the cursor one character after the current cursor. At this step in the learning process, just remember the vi command mode and vi insert mode. The vi command mode is everything here, insert text, delete text and save file. Oh we haven't save our file yet. To save and exit file, enter this key: :wq. Of course it's in the command mode.

vi insert text image

You can find all basic vi commands in the next topic.

Edit file with vi editor

The Linux vi editor is a great tool to edit files in the Linux command line terminal. It has been a popular text editor to edit server configuration files and programs for so long. Almost all Linux distributions include vi editor in default installation. Now, let's learn how to edit file with vi editor. The syntax used to edit file using vi editor is the same as create a new file. To edit a file in Linux system, you can change directory or just provide the path to the directory where the file resides. See the vi format below:

vi /path/to/filename

During the create new file with vi editor practice, we didn't use much commands. You learn how to insert text, delete text and save file. However, when editing a file or program in Linux, you are going need more than that. Of course, the basic file editing is just insert text, delete text and save the changes, but there are many useful vi commands that can make your job a lot easier. This is the table of basic vi commands that you can use to assist you editing a file in Linux command line terminal:

Command Description
i Insert text
a Append
u Undo
x Delete 1 character
dd Delete one line
yy Yank (copy 1 line)
p Paste
ZZ Exit and save
:q Quit without saving
:wq Write and quit
:q! Force quit without saving
:wq! Force write and quit
/ Searches forward
? Searches backward
h Move cursor back 1 space
j Move cursor down 1 line
k Move cursor up 1 line
l Move cursor forward 1 space

Those are basic vi commands that every Linux user should remember. In the original vi editor, the arrow keys are not for moving cursor. You probably noticed that during create a new file with vi editor practice before. If you don't know how to move the cursor, then how do you plan to edit a file? Here is the tips on how I remember the keys to move cursor in vi editor. I just remember two keys, the key point, j for jump and the k for climb. Use j to jump down one line and k to climb up one line. Then I would know that before j is h which is used to move back one step and the l key in front is to move forward. I don't know what you think about that, but that's how I remember how to move cursor in vi text editor. Let's start practice editing a file with vi text editor now by openning the file that we created just now.

[email protected]:~/vitutorial$ vi vitest

This is the my vitest file. It doesn't matter if your file content doesn't similar with mine. We are going to try all those command above so you can see how it works and get familiar with it. You already use i to insert text before. This time, use a to append text to the file. Press a and type something.

Tdfgdfhis is a vi test file. We just learn how to insert text to new blank file.
We open the file with: vi filename
We save a file with: :wq
in command mode.

When you open the file with vi editor, the cursor will be at the first character on the first line. When you press a to append text, the cursor will move to the next character, that means the second space. As you can see from the example above, I type something that I don't understand myself. So, I want to cancel it. What command that we have in vi editor which can cancel what we did? We can delete character like in the create new file example above. But there is a useful command in vi editor that we can use, which is the undo command. To undo, press Esc key to go to the vi command mode and press u. This will undo what I just type a moment ago.

This is a vi test file. We just learn how to insert text to new blank file.
We open the file with: vi filename
We save a file with: :wq
in command mode.

We already know how to delete a character in vi editor, let's try delete one line. The vi command to delete one line is the dd command. When you press dd, the line which your cursor is at the moment will be deleted. To delete the line with dd, make sure you are in the vi command mode. See the example below:

We open the file with: vi filename
We save a file with: :wq
in command mode.

As you can see, the first line is gone when I press dd. Press u to undo, and the line is back.

The next command is the vi command to yank. Yank means copying one line. Press yy at the first line will copy that line. when we press p, vi will paste that line below the line where the cursor is right now. Let's see what it means:

This is a vi test file. We just learn how to insert text to new blank file.
This is a vi test file. We just learn how to insert text to new blank file.
We open the file with: vi filename
We save a file with: :wq
in command mode.

The cursor is at the first line when I press yy. So vi paste the line to the line below it when I press p.

You already learn how to save and exit file in previous the example. If we don't want to save the file, we can enter the key :q which means just quit. But if we press :q now, vi won't quit. That is because we already make changes to the file. Vi warns us that the file has been modified, and we do not save it yet. We can see the message at the bottom of vi editor which says vitest modified, not saved. If we want to save, we can issue the :wq command or the ZZ (double upper case Z) command. If we don't want to save and just quit, we must force vi to quit. The command to force quit is :q!.

One of the most useful utility that vi has is the / command, which can be used to search text in the file. We can use vi search utility to edit a certain word for the whole file. We can also use the vi search utility to fine a certain word in a large file without having to scroll line by line looking for the word. To use the vi editor search utility, type /word and enter. Vi will move the cursor to the first word in the file. To go to the next word, press n. You can press n again and vi editor will continue looking for the next word in the whole file. The vi / command searches forward, that means from top to bottom, and the ? command searches backward, opposite the /.

Finally, we can move the cursor around in the file with j, k, h, l keys. Move down line by line to the end of the file by pressing j. To move up line by line, press k. Press j to move backward on the line. Press l to move forward on the line.

What do you think of vi editor now? Is it difficult to use? I'm sure your answer is yes if you are a gui user before. Well, don't give up now. All you need is some practice. You'll never regret because vi editor is very useful and supported by most of unix-based operating system. There are many other things that you can do with vi. If you want to know what vi is capable of, type vimtutor and scan over to see what it can do.

So, what are improvements vim over vi editor? Like I said earlier, it's more user friendly. For example, with vim editor, you can use backspace as well as delete key function. You can also use arrow keys. That improvement alone means so much for a beginner. Other vi editor basic functions are still the same. And I recommend vim for all Linux users. To use vim instead of vi, you just replace vi with vim. For example, if we want to edit a file, type vim file. That's all.

Here is a description about vim from the manual page:

DESCRIPTION
Vim is a text editor that is upwards compatible to Vi. It can be used
to edit all kinds of plain text. It is especially useful for editing
programs.

There are a lot of enhancements above Vi: multi level undo, multi win-
dows and buffers, syntax highlighting, command line editing, filename
completion, on-line help, visual selection, etc.. See ":help
vi_diff.txt" for a summary of the differences between Vim and Vi.

While running Vim a lot of help can be obtained from the on-line help
system, with the ":help" command. See the ON-LINE HELP section below.

Most often Vim is started to edit a single file with the command

vim file

More generally Vim is started with:

vim [options] [filelist]

If the filelist is missing, the editor will start with an empty buffer.
Otherwise exactly one out of the following four may be used to choose
one or more files to be edited.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.