Presentation by Piyush Bhopalka
Type vim and press enter
user:~$ vim
Press i and a cursor appears. Type in anything you like. When finished press Esc
Press : and you will see a prompt at the bottom of the screen
w <filename>
- Saving the fileq
- Exiting the filewq <filename>
- Saving and Exiting the fileq!
- Exit without saving the file
:wq sample.txt
Type vim
<filename>
user:~$ vim sample.txt
Press A
to append in a file. After completion, press Esc
2w
to move 2 words forwards3e
to move 3 words forward to the end of the word0
(zero) to move to the start of the line
user:~$ vim vimfile_2.cpp
Download this file. We will work on this now.
Its easy to navigate through the file in Vim. Lets see them.
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.
Press G (Shift-g)
to move to the bottom of the file
Press gg
to move to the top of the file
Press <line_number>G (Shift-g)
to move to the specified line number.
There are several commands for deletion. Lets go through them one by one
Put the cursor on top of the character to be deleted and press x
Go to line 25 and delete A from pushA function
Go the start of the word and press dw
Go to line 39 and delete the word EXTRA
Go the start of the word and press 2dw
Go to line 56 and delete the words UNNECESSARY TEXT
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
Press u
to undo an operation.
Press Ctrl-R
to redo the operation.
First press dd
to delete a line. It actually stores the content of the deleted line.
Press p
to put (paste) the content.
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)
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
Press /
and type the word to be searched.
/push
n
to search again for the same word. N (Shift-n)
to search again for the same word in the opposite direction.
Move to one of the parenthesis and press %
. You will reach its matching parenthesis.
:s/<old_word>/<new_word>
- Find the old word and replace with the new word
:s/peek/showFirst
:%s/<old_word>/<new_word>/g
- Find the old word and replace it with the new word in the whole file
:%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
Type vimtutor
in your terminal and start practicing.
user:~$ vimtutor