Tuesday 21 April 2020

Top 10 example of using Vim or VI editor in UNIX and Linux

Vim or VI is a popular text editor used in UNIX and Linux systems. It is a powerful tool that allows users to edit and manipulate text files efficiently. In this article, we will discuss 10 examples of using Vim or VI editor in UNIX and Linux.

1. Opening a file

To open a file in Vim or VI editor, we can use the following command:

vim filename.txt

This command will open the file named filename.txt in Vim or VI editor.

2. Moving the cursor

In Vim or VI editor, we can move the cursor using the arrow keys or the h, j, k, and l keys. The h key moves the cursor left, the j key moves the cursor down, the k key moves the cursor up, and the l key moves the cursor right.

3. Inserting text

To insert text in Vim or VI editor, we need to enter the insert mode. We can enter the insert mode by pressing the i key. Once we are in insert mode, we can start typing the text we want to insert.

4. Saving a file

To save a file in Vim or VI editor, we can use the following command:

:w

This command will save the changes made to the file.

5. Quitting Vim or VI editor

To quit Vim or VI editor, we can use the following command:

:q

This command will quit Vim or VI editor if there are no unsaved changes. If there are unsaved changes, we need to use the following command:

:q!

This command will quit Vim or VI editor and discard any unsaved changes.

6. Copying and pasting

To copy text in Vim or VI editor, we need to enter the visual mode. We can enter the visual mode by pressing the v key. Once we are in visual mode, we can highlight the text we want to copy using the arrow keys or the h, j, k, and l keys. Once we have highlighted the text, we can copy it using the following command:

y

To paste the copied text, we need to move the cursor to the location where we want to paste the text and use the following command:

p


7. Deleting text

To delete text in Vim or VI editor, we need to enter the visual mode. We can enter the visual mode by pressing the v key. Once we are in visual mode, we can highlight the text we want to delete using the arrow keys or the h, j, k, and l keys. Once we have highlighted the text, we can delete it using the following command:

d


8. Searching for text

To search for text in Vim or VI editor, we can use the following command:

/keyword

This command will search for the keyword in the file. To move to the next occurrence of the keyword, we can use the following command:

n


9. Replacing text

To replace text in Vim or VI editor, we need to enter the command mode. We can enter the command mode by pressing the : key. Once we are in command mode, we can use the following command to replace the first occurrence of the keyword with the replacement text:

:s/keyword/replacement

To replace all occurrences of the keyword with the replacement text, we can use the following command:

:%s/keyword/replacement/g


10. Using multiple windows

Vim or VI editor allows users to work with multiple windows. To split the screen into two windows, we can use the following command:

:split

This command will split the screen into two windows, with the current file in the top window and a new empty window in the bottom.

To switch between windows, we can use the following command:

Ctrl-w Ctrl-w

This command will switch between the two windows.

We can also resize the windows by using the following commands:

Ctrl-w + Increase the height of the current window Ctrl-w - Decrease the height of the current window Ctrl-w > Increase the width of the current window Ctrl-w < Decrease the width of the current window


In this article, we have discussed 10 examples of using Vim or VI editor in UNIX and Linux. These examples cover some of the basic functionalities of Vim or VI editor, such as opening a file, moving the cursor, inserting text, saving a file, and quitting Vim or VI editor. We have also discussed some advanced functionalities such as copying and pasting, deleting text, searching for text, replacing text, and using multiple windows. Vim or VI editor is a powerful tool that can greatly enhance the productivity of UNIX and Linux users.

Labels: , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home