W.W.E
Linux

WORKING WITH VI EDITOR PART 2

1 922

Video Tutorial at end of page

Welcome to part 2 of tutorial working with vi editor. Nowadays you will see improved version of vi  called VIM editor.VIM stands for VI Improved.

Its more user friendly. requires less resource, available on all linux flavours and across the board its implementation is very similar.

In our last tutorial we did very basic tasks like opening  existing file , creating new file, reading file (quitting without saving content) and writing to file (saving content).

You need to be careful to use commands as VI editor is case sensitive

There are so many  VI editor commands but in this tutorial we will discuss about ones which are used mostly.

When you Open file in Vi Editor and you want to position cursor either at first line or at last line you will use below two commands (you must be in command mode to execute commands)

command: gg

description:to go to start of file.

command: shift+g

description: to go to end of file

Now we will learn how to move within a file withouting changing contents of file. To accomplish this you must be in command mode. Below commands will help you to move one character at time

command: k or up arrow

description:Moves the cursor up one line.

command: j or return or down arrow key

description:Moves the cursor down one line.

command: h or backspace or left arrow

description:Moves the cursor to the left one character position

command: l or space or right arrow key

description:Moves the cursor to the right one character position.

command: dd

description:delete the current line. if you want to delete n(1,2…) number of lines for example you want to delete 5lines from current position. press 5dd to delete 5 lines

Tip: if you want to see line numbers use below command

command : :set number

description: display line number next to each line .

Above command is useful if you are doing are copying/deleting multiple lines
command:yy

description: yank/copy the line in buffer. You can paste line using p option. similarly like deleting you can use number before yy to copy n number of lines  e.g. 15yy will copy 15 lines in buffer, which can be put into file anywhere using p.
command: ZZ or :wq!

description:save changes and exit the file.

If you want to search string in file and you want to start search from start of file type /string and then press enter

It will search all words and will highlight all matches. you can press n to move to next match

If you want to search for string staring from end of file use ?string  and then press enter

It will search all words and will highlight all matches. you can press n to move to next match

The syntax for replacing one string with another string in the current line is

:s/currentstring/newstring/

Here “currentstring” represents the old string and “newstring” represents the new string. For example, to replace each occurrence of the word “tutorial” in a line with “guide,” type

:s/tutorial/guide

above command  will replace first occurence if there are more occurrences of tutorial press n to go to next occurance

To replace string in entire file below syntax is used

Syntax: %:s /currentstring/newstring/

example: %:s/tutorial/guide

above command will replace tutorial with guide in entire file by using

to replace string in whole file you can also use flag g with above command

Syntax: %:s /currentstring/newstring/g

example: %:s/tutorial/guide/

%s – specifies all lines. Specifying the range as ‘%’ means do substitution in the entire file.

g – specifies all occurrences in the line. With the ‘g’ flag , you can make the whole line to be substituted. If this ‘g’ flag is not used then only first occurrence in the line only will be substituted.

This is end of part 2 tutorial.We have learnt most commonly used commands in this tutorial. I hope you have enjoyed tutorial and please leave comment if you need more information. I would also request you to subscribe to newsletter

You can now watch video tutorial

SUBSCRIBE OUR NEWSLETTER
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a reply

You must be logged in to post a comment.