Vim Editor

An inbuilt powerful text Editor

Presentation by Piyush Bhopalka

Opening Vim

Type vim and press enter



user:~$ vim
					

Typing into Vim

Press i and a cursor appears. Type in anything you like. When finished press Esc

Saving and Closing a file

Press : and you will see a prompt at the bottom of the screen

  • w <filename> - Saving the file
  • q - Exiting the file
  • wq <filename> - Saving and Exiting the file
  • q! - Exit without saving the file


:wq sample.txt
					

Opening Existing file

Type vim <filename>



user:~$ vim sample.txt
					

Appending a file

Press A to append in a file. After completion, press Esc

Using count for motion

  • Press 2w to move 2 words forwards
  • Press 3e to move 3 words forward to the end of the word
  • Press 0 (zero) to move to the start of the line


user:~$ vim vimfile_2.cpp
					

Download this file. We will work on this now.

Cursor Location and File Status

Its easy to navigate through the file in Vim. Lets see them.


Down arrow

Knowing your present location

Press Ctrl-G to know the present location of your cursor and your file. Now do remember your cursor number before you execute the next command.

Move to the bottom of the file

Press G (Shift-g) to move to the bottom of the file

Move to the top of the file

Press gg to move to the top of the file

Moving to a specific line number

Press <line_number>G (Shift-g) to move to the specified line number.


Up arrow

Commands for Deletion

There are several commands for deletion. Lets go through them one by one


Down arrow

Deleting a character

Put the cursor on top of the character to be deleted and press x

Go to line 25 and delete A from pushA function

Deleting a word

Go the start of the word and press dw

Go to line 39 and delete the word EXTRA

Deleting 2 word

Go the start of the word and press 2dw

Go to line 56 and delete the words UNNECESSARY TEXT

Deleting a line

Go to the start of the line and press d$

dd also does the same. Try 2dd

Go to line 80 and delete the line

Up arrow

Undo Command

Press u to undo an operation.

Press Ctrl-R to redo the operation.

Put Command

First press dd to delete a line. It actually stores the content of the deleted line.

Press p to put (paste) the content.

Copy and paste Command

Press v to enter Visual Mode. Move the cursor to select lines to be copied.

Press y to yank (copy) the content.

Go to the specific location where content is to be pasted. Press p to put (paste)

Replace Command

Move the cursor on the character to be replaced. Press r and type the character you wish to type.

Press R (Shift - r) to replace more than one characters

Search Command

Press / and type the word to be searched.


	/push
						

Down arrow

Searching the next word

  • Press n to search again for the same word.
  • Press N (Shift-n) to search again for the same word in the opposite direction.

Matching the parenthesis

Move to one of the parenthesis and press %. You will reach its matching parenthesis.

Substitute Command

:s/<old_word>/<new_word> - Find the old word and replace with the new word


	:s/peek/showFirst
						

Down arrow

Substituting in the whole file

:%s/<old_word>/<new_word>/g - Find the old word and replace it with the new word in the whole file

Substituting with a prompt

:%s/<old_word>/<new_word>/gc - Find the old word and ask for prompt to replace it with the new word


:%s/show/print/gc
						

Practicing vim in your own pace

Type vimtutor in your terminal and start practicing.


user:~$ vimtutor
					
let's go on