Text Editor With “Vim” Command

1. Overview


Since in Linux everything is a file, mostly you edit files when working with Linux. There are several text editors out there in Linux, choosing the right one to be your favorite is depend. Personally, from my point of view, I think people prefer text editor “vim” the most due to the rich of features.

In this tutorial will walk you through some basic features of “vim” text editor that people mostly use them in their daily work with Linux.

2. Prerequisites


In this article, it is presumed that:

a. You have already installed a Linux Operating Systems (OS), such as Debian or CentOS. Please refer this link for Debian Linux installation. Installing Debian OS With Logical Volume Manager (LVM)
b. You have done some initial setting after OS installation. You can refer this link. Debian 8.x Initial Server Configuration

3. Command “vim” Text Editor


Basically, we need to understand the concept of modes in “vim” command. There are several modes available in “vim” command but for now we will talk about some modes only such as command mode, insert mode, visual mode, and exit mode.

3.1 Command Mode


When you execute command “vim filename”, it will lead you into the command mode. With this mode, you can use command options to work the text content such the following.

Search Text: to search for text in vim press “/” command and it will lead you to bottom of the file. Then just type the word or words that you want to search following the “/” command. To search a word/words forward in the content of the text, press “n” or press “N” to search a word/words backward. In the following picture, we will edit a file name “testfile.txt” and search for words “Tech Space KH”.

#vim testfile.txt

Search and Replace: to search through the entire content for a word/words and replace it with new word/words, use the command “:%s/word/newword/g”. In below picture we search for words “Tech Space KH” and replace it with the words “TechSpaceKH”.

Deleting Text: to delete the current line in the text content, press “dd” command.

Undo and Redo: to undo/redo your last action, press “u” command.

Go to Line Number: to go directly to specific line number, press “:” following with the line number. As below picture, we will go directly to line number 3 in the text.

Copy and Past: to copy the current line, press “yy” command. To past the copied line, press “p” command. In the following picture, we copy the last line and past it below the copied line.

3.2 Insert Mode


We can go into insert mode easily by just pressing i command. After going into insert mode, we can navigate and manipulate the text. In the insert mode, we edit the text content, add more text, or delete text. To exit the insert mode press “esc” key, then it will go back into command mode.

3.3 Visual Mode


To go into visual mode, press “ctrl + v”. In visual mode we can select text using arrow keys and apply commands to the selected text that’s currently highlighted. Let see how it works as the following. First, we will press “ctrl + v” to select the lines and then press “shift + i”. After that press “shift + #” and finally press “esc” key to command out all the selected lines.

3.4 Exit Mode


To go into exit mode simply press “:” command. To write change to the file edited, press “w” command following “:” command . Press “q!” to quite editing the file without saving. To save and exit, press “wq” or “x!” following “:” command.

9. Conclusion


I guest now you should be able to use command “vim” to edit a file in Linux. Let try to practice it by own pace to familiar yourself in editing the text file with command “vim”. If you have any questions or suggestions you can always leave your comments below. I will try all of my best to review and reply them.