Linux Basics : Part08 : Text Editing Tools VI & VIM

Well there are many text editors for Linux, some are pretty straight forward and some really need a full fledged-tutorial.

Today we will see the “vi” and “vim” text editor.

vi” was first introduced and serves as the default text editing tool in most Linux distribution.

“vim” is just an upgraded version of “vi” and takes similar syntax’s more or less. So let us see what those are.

The “vim” text editor 3 main modes.

  1. Command Mode. –> Default mode when you start or open vim.
  2. Insert Mode.
  3. Replace Mode.

Let us see what each mode does.

  1. Command Mode:
    Whenever you start “vim” or open a file using “vim” by default you enter into command mode. In this mode you can move across the file up and down and read it, but you cannot edit the file.

  2. Insert Mode:
    There are several ways by which you can enter into insert mode, but the mode used one is by pressing the letter “i“. Once you press “i” you enter into insert mode where you can move across the file, edit it, delete lines, etc. Other ways to get into edit mode are:
    -> A : Enter edit mode and the cursor moves to the end of the line.
    -> o : Enter edit mode and automatically move to a new line below.
    -> O : Enter edit mode and automatically move to a new line at the beginning of the file.

  3. Replace Mode:
    As the name suggest, it is used to replace the words as you type the new ones. You can enter into replace mode by pressing “insert” key.

Once you are done editing the file, you can exit the file by pressing “:wq

where
w = write
q = quit

If you want to write and stay in the file then just press: “:w

If you want to write and quit the file forcibly: “:wq!

If you want to quit the file without writing: “:q

Search and Replace:

Search:
Once you are in the file and in “command” mode, you can press “/” followed by the word to search for it.
Once you type the word and press “enter” it will get highlighted. From here you can press “n” to see the next occurrence of the word in the file.

Replace:
To search for a word and replace it with something you can use the syntax

":s/existingword/newword/"

This will only replace the first occurrence of the existing word.

To replace all the occurrence of the existing word in the current line you can use the syntax

":s/existingword/newword/g"

To search and replace an existing word in the entire file, use “%” to indicate it.

":%s/existingword/newword/g"

If you do not mention anything in the word the occurrences of the search word will be deleted

":%s/existingword//g"

If you want to confirm each substitution you can add the “c” flag.

":%s/existingword/newword/gc"

To search for a line starting with a specific word use the symbol “^

 ":%s/^existingword/newword/gc"

This will search for lines starting the existing word and replace it with the new word.

To make the searches case insensitive you can use the “i” flag

 ":%s/existingword/newword/gci"

There are many other way of searching and replacing words like searching and replacing within certain line numbers and so on, however the ones mentioned here are the basics and the most used ones.
s” in the above syntax stands for “substitute”.

The above syntax will replace only the mentioned syntax and replace it with whatever you have mentioned. So you have to enter the exact word which you want to replace.

This is OK if you remember the word and the word is not a part of other words like if you want to replace “men” with “women” you may use

 ":%s/men/women/g"

Now if the file contains words like “mention” or moment” these words will become “womention” or mowoment” which you definitely do not want.

In these case you might want to use syntax which will search for the exact word and only replace that.
For that you can use:

":%s/\<men>/women/"

Numbering:

This is pretty simple, if you are going through a huge file you might want to jump to a certain line number for that we can use the line numbering feature of vi in command mode.
":set nu"
Once you press enter after this all lines are numbered and then you can jump to a certain line using:
":1004" -> where 1004 is the line number.

Cut, Copy and Paste:

To copy a whole line you need to press “yy“.
If you want to copy multiple lines suppose 5 lines then press “5yy

To paste the line somewhere in the file press “p

To delete a line press “dd
Again if you want to delete multiple lines suppose 5 lines, press “5dd

These are again done in command mode.

Redo and Undo:

To undo most recent change in the file press “u“. To do multiple undo you can press “u” multiple times.

To undo all changes to the current line press “U

To redo last undo press

"ctrl+r"

Lastly to open a file using “vim

#vim file-a

That’s it for today, see you in the next post of blog.avoidingtech.com




Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!