Module 3.0 – How To Use vi / vim In Linux

This tutorial will focus on the use of vi / vim in Linux with their commands and notations using examples.

UNDERSTANDING THE SUBJECT MATTER

What Is Text Editor In Linux?

Writing is one of the key operations in an operating system. Writing also comes with editing, undoing, redoing searching and replacing words, lines, columns, tables, etc, which is what I am doing right now, immensely writing and editing. Hence why there must be a tool for all these activities. That tool is called a text editor.

There are a couple of text editor tools or rather text editors in Linux and they all have their differences. The common thing among them, of course, is that they can do basic text editing. Some text editors are more robust than others, some can be used to edit programs, as such, programmers prefer those kinds of editors to others.

At the same time, using a text editor is all about preferences. One will want to use what they are comfortable with and sometimes familiar with.

Some of the text editors In Linux are vi, vim, nano, emac, gedit, pcode, vscode, atom, brackets, neovim, light table, etc.

This tutorial will focus on vi and vim in Linux

What Is vi In Linux?

vi, as mentioned above, is a text editor, a tool used in editing files.

What Is Vim In Linux?

vim is also a text editor, but an improved version of vi. Hence the acronym vim (vi improved). More so, apart from editing a plain text, vim in Linux is specially used for editing programs, as such, it’s a tool for programmers and some programmers have testified and preferred vim to any other text editor in Linux.

What is vim Editor Used For?

vim editor is basically used for creating, writing, reading and editing files. Furthermore, advanced use of vim in Linux is by programmers, used in editing programs.

Differences Between vi and vim

vim is an amplified version of vi. Vim over vi has enhanced feature of syntax highlighting and multi level undo. In vim, a file opened, edited and exited can be reopened and a lot of the changes done can be undone again. There is multi level persistent undo feature.

In addition, vim can edit almost any kind of file, it has only a few limit compared to vi. vi cannot manage nul characters and has limited line length.

Furthermore, vim in Linux supports not just multiple screen splitting, It also allows you do the same or different work on the multiple screen that’s splitted and lastly, vim can browse the file system if configured.

How To Install vim In Linux

Depending on the Linux distribution and version, vim tool may not be shipped with the operating system. Therefore, you will have to install the vim text editor if you want to make use of it.

To Install vim, you use the command,

  • <yum install vim> (For Red Hat Enterprise Linux or CentOS)
  • apt get vim (For Debian or Ubuntu Linux)

How Do I Open vi Editor In Linux?

To open a vi or vim editor, you use the command “vi” for a vi editor and the command “vim” for vim editor.

To open a file with the vi editor, you use the command,

vi <file-name>

More so, to open a file with vim in Linux, you use the command,

vim <file-name>

vim in Linux

Useful vi editor Commands In Linux

Some of the action you can perform when using vi /vim in Linux are listed below.

Searching For & Replacing A Word

To search for a word or words, you use the key, “/” or “?

“/” will search for a word from the beginning of a file downward while “?” will search for a word from the end of the file upward

If there are many related words in a file, the key “n” is used to move to the next available word while the key “N” (uppercase) is used to move to the previous searched word

see examples in “ACTION TIME” section.

Saving, Quitting & Exiting A File

To save a file in vi / vim in Linux, you use the key “w“, to quit a file, use the key “q“, and to exit a file, use the key “!

Another way to do this, that is, save, quit and exit all at the same time is to use the key “x

See examples in “ACTION TIME” section

Quitting And Exiting A file Without Saving

Of course, you can open or edit a file without saving it, probably when you realize you do not need to touch the file. To do that, you will not use the w character which signifies write. you will only use the “q!” character (that is, quit and exit without saving)

Undo and Redo An Action

To undo an action, use the command, “u

To redo an action, use the command “ctrl+r


vi / vim Modes

There are three modes in using vim. They are the command mode, insert mode and visual mode.

vi / vim Command Mode In Linux

In the command mode, you do not need to use any of the insert keys before you can edit a file. The command mode is just like using short cuts to edit files. There are many characters that can be used in the command mode. Some of them are

  • gg : It moves the cursor to the beginning of the file
  • G : It moves the cursor to the end of the file.
  • 0 (zero): it moves the cursor to the beginning of a line.
  • $: moves the cursor to the end of the line.
  • w: moves the cursor forward one word
  • b: moves the cursor backward one word
  • yy : means copy a line
  • dd : means cut a line
  • p: means paste
  • u: means undo
  • ctrl+r : means paste

In summary, using vi or vim in command mode simply means you open a file with the command, vi <file-name> or vim <file-name> and use the basic short cut keys mentioned above to edit a file.

see examples in the section “ACTION TIME“.

vi / vim Insert Mode In Linux

The insert mode allows you to edit a file by writing (inserting texts). You can also do a copy and paste in the insert mode. If you are connected to a server via ssh, you can copy a file from some other places and paste but if you are connected via the server’s console, you may not be able to do that. To get to the insert mode, you can use either of the keys below.

  • The “i” key : represents insert
  • The “a” key : represents append and
  • The “o” key : represents open a new line

see examples in the “ACTION TIME” section

vi / vim Visual Mode In Linux

To get to use the visual mode, you enter the key “v“. In the visual mode, large copy within the file and paste can be done via the console.

see example in “ACTION TIME” section.


ACTION TIME

How To Use vi / vim In Insert Mode (Examples)

1. create the file, “amazing_love” in your home directory. The file should contain the following words.

“ I’m forgiven, because You were forsaken
I’m accepted, You were condemned
I’m alive and well, Your Spirit is within me
Because You died and rose again
Amazing love, how can it be
That You my King would die for me
Amazing love, I know it’s true
It’s my joy to honor You ”

Steps

  • create the file,
[root@rhel8 ~]# vim amazing_love

you will have a page like this.

  • Hit the “enter” key, it will take you to a page like this one below.

  • enter the key, “i” to initialize the insert mode, you can confirm you are already in the insert mode at the bottom of the page, it will be written “INSERT” this way below,

  • From this point, you can start to write. If you are connected via ssh, you can copy and paste a file but if you are connected via the console, copy and paste will not work in the insert mode.

To Save, Quit And Exit In vi /vim,

  • Enter the “Esc” key to leave the insert mode. you will notice that the “INSERT” will not show anymore meaning that you escaped out of the insert mode.

  • enter wq! keys to write, save and exit, followed by the “enter” key

w, represents write

q , represents quit

i, represents exit as mentioned in the “UNDERSTANDING THE SUBJECT MATTER” section above.

  • Now you have the file, amazing_love created


How To Use vi / vim In Command Mode (Examples)

Let’s do some activities below

1. move the cursor to the end of the file, amazing_love.

As usual, open the file first by using the command <vim amazing_love> or <vi amazing_love>

To move the cursor to the end of the file, enter the key, “G”

2. move the cursor to the beginning of the file.

To do that, enter the key, “gg”

3. move the cursor to the end of the line.

To do that, enter the “$” key

4. Move the cursor to the beginning of the line

To do that, enter the number, “0”


How To Use vi / vim In Visual Mode (Examples)

Let’s do some activities below

1. copy the first line in the file, amazing_love and paste it at the end of the file.

STEPS

  • To do this, open the file with vi or vim, make sure that the cursor is on the first line, enter the key, “v” select the line by using the arrow keys, the right arrow key of course,

  • enter the key, “yy” to copy the line, then go to the end of the line and enter the key “p” to paste

Summary

you can now see that using the vi or vim text editor will require you to either use only one mode or combine the two or three modes together for file editing.

Now that you understand the modes, let’s continue with “ACTION TIME”


How To Search For & Replace a Word Using vi / vim (Examples)

Let’s do some activities

1. from the amazing_love file, search for the word “you”

steps

  • open the file amazing_love with the vi or vim text editor tool using the command,

<vim amazing_love> or <vi amazing_love>

  • place the cursor at the line where you want to start the search from, in this case, the beginning of the file and enter the key “/you” as shown in the image below. If you are using the vi editor, you will have to hit the Enter key

  • enter the key, “n” to go to the next word and the “N” key to go to the previous word

NOTE: As mentioned in the “UNDERSTANDING THE SUBJECT MATTER” section, you can also use the “?” key to search for a file from the end of the file upward.

2. replace the word You with He in the amazing_love file.

To replace a word, use the formula below. 

< :%s/oldword/newword/g > – this will replace the old word with new word

STEPS

Of course, open the file with the vi or vim command

  • enter “:%s/You/He/g” and hit the Enter key.


How To Delete Using vi / vim (Examples)

The  character “d” combined with other characters deletes an operation

For example,

  • dw” deletes a word
  • dgg” deletes the beginning of the file
  • x deletes a character.

1. Delete 5 words in the first line of the amazing_love file.

STEP

  • open the file using vi or vim and enter the character “5dw

2. delete the first 3 lines in the amazing_love file

STEP

  • open the file using vi or vim and enter the character “3gg


How to Undo or Redo using vi / vim (Examples)

1. Undo the action in the previous example

STEPS

  • enter the character “u

2. Redo the action in the previous example.

STEPS

  • enter the character “ctrl+r

Cheers!!!

How To Configure Vim To Suite Writing Ansible Playbook

In the user’s home directory that will be writing the playbooks, create .vimrc file and add the following below, save the file.

[kenny@hqdev1]$ vim .vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et nu cuc
autocmd FileType yam; colo desert

Class Activity 3.0

Create a file called “My national anthem”, the content of the file should contain the national anthem of your country. Save the file.

Edit the file and delete the first and last line of the contents in file content

Your feedback is welcomed. If you love others, you will share with others

7 Comments

  1. Hi, there.
    Please, teach me how to:
    1. Find and delete all strings begining with a commomn substring (prefix: e.g. abc)
    and ends with different substrings (suffix: e.g. 1, 2, 123, x12, y99, y987,
    .txt …).
    Ex.: find and delete abc1, abc939, acbxyz, abc44, …
    2. Find and substitute the strings, which begins/ends with a commomn substring
    (prefix: e.g. abc (becomes xyz)) and ends with another substring
    (suffix: e.g. 1, 2, 123, x12, y99, y987, .txt …) by a common substring (e.g.
    .py, .txt, txt, date2020Nov27, …)
    Ex.: abc1 becomes abc.txt, abc123 becomes abc_today, abc123 becomes xyz123,
    …).
    Thank you in advance. IOS

  2. Hi, there.
    Please, teach me how to:
    1. Find and delete all strings begining with a commomn substring (prefix: e.g. abc)
    and ends with different substrings (suffix: e.g. 1, 2, 123, x12, y99, y987,
    .txt …).
    Ex.: find and delete abc1, abc939, acbxyz, abc44, …
    2. Find and substitute the strings, which begins/ends with a commomn substring
    (prefix: e.g. abc (becomes xyz)) and ends with another substring
    (suffix: e.g. 1, 2, 123, x12, y99, y987, .txt …) by a common substring (e.g.
    .py, .txt, txt, date2020Nov27, …)
    Ex.: abc1 becomes abc.txt, abc123 becomes abc_today, abc123 becomes xyz123,
    …).
    Thank you in advance. IOS

  3. Privet Victor!
    Please send some hints about the subject (above and now…) below:
    “Hi, there.
    Please, teach me how to:
    1. Find and delete all strings begining with a commomn substring (prefix: e.g. abc)
    and ends with different substrings (suffix: e.g. 1, 2, 123, x12, y99, y987,
    .txt …).
    Ex.: find and delete abc1, abc939, acbxyz, abc44, …
    2. Find and substitute the strings, which begins/ends with a commomn substring
    (prefix: e.g. abc (becomes xyz)) and ends with another substring
    (suffix: e.g. 1, 2, 123, x12, y99, y987, .txt …) by a common substring (e.g.
    .py, .txt, txt, date2020Nov27, …)
    Ex.: abc1 becomes abc.txt, abc123 becomes abc_today, abc123 becomes xyz123,
    …).”
    Thank you in advance. IOS

Leave a Reply

Your email address will not be published.


*