Vim tutorial from vimtutor
Table of content
-
Lesson 4
Chapter 1 (toc)
Lesson 1: basic operation
-
Moving cursor with
up
,down
,left
,right
-
Starting vim with
vim file-name.ext
-
Exiting vim with:
-
ESC
:q!
to discard all changes -
ESC
:wq
to save all changes
- Deleting any character with
ESC
x
Lesson 2: the delete operator
-
Deleting from the cursor to the next word type:
dw
-
Deleting from the cursor to the end of the word type:
de
-
Deleting from the cursor to the end of a line type:
d$
-
Deleting a whole line:
dd
-
Repeating a motion:
operator [number] motion
. Where
-
operator: For what to do. Ex:
d
for delete -
number: Optional count to repeat a motion
-
motion: moves over the text to operate on. Ex:
w
(word),e
(end of word),$
(end of line), etc
-
Moving to the start on the line:
0
-
Undoing actions:
-
u
for previous actions. -
capital u
U
for all the changes on a line. -
CTRL-R
for undo the undos (redo)
Lesson 3: the change operator
-
Putting the previous deleted text to the cursor with
p
. -
Replacing the character at the cursor with x with
rx
(x is the typed character). -
Changing the word (removing then inserting) to the end of the word with
ce
-
Changing the word to the end of the line with
cc
,c$
-
Changing can also work with the format
operator [number] motion
Lesson 4.1: cursor location and file status
-
Press
CTRL-G
to show the location of the file editing. -
Press
G
(capital g) to go to the bottom of the file. -
Type
gg
to go to the top of the file. -
Type number of the line and
G
to go to that line. For ex:5G
Lesson 4.2: The search command
Type / followed by the pharse in order to search for it
-
Press
enter
to search. -
Type
n
in order to search again forward,N
for backward direction. -
When wrapscan is turned on, if the search reaches the end of the file it will continue at the start.
-
CTRL-O
to go back where you came from,CTRL-I
goes forward. -
Matching parentheses search by move the cursor to (, ), {, }, [, ] then type %.
Lesson 4.3: The substitute command
-
Type
:s/old/new/g
to substitute ‘new’ for ‘old’. -
s/thee/the
will only changes the first occurence. -
Adding the
g
flag means to substitute globally in the line, change all occurrences of “thee” in the line. -
To change every occurrence of a character string between two lines,
- type
:#,#s/old/new/g
where #,# are the line numbers of the range of lines
where the substitution is to be done.
-
Type
:%s/old/new/g
to change every occurrence in the whole file. -
Type
:%s/old/new/gc
to find every occurrence in the whole file,
with a prompt whether to substitute or not.
Lesson 4: Summary
-
CTRL-G
displays your location in the file and the file status.
-
G
moves to the end of the file. -
number
G
moves to that line number. -
gg
moves to the first line.
-
- Typing
/
followed by a phrase searches FORWARD for the phrase.
- Typing
-
Typing
?
followed by a phrase searches BACKWARD for the phrase. -
After a search type
n
to find the next occurrence in the same direction
or N
to search in the opposite direction.
CTRL-O
takes you back to older positions,CTRL-I
to newer positions.
-
Typing
%
while the cursor is on a (,),[,],{, or } goes to its match. -
- To substitute new for the first old in a line type
:s/old/new
- To substitute new for the first old in a line type
-
To substitute new for all 'old’s on a line type
:s/old/new/g
-
To substitute phrases between two line #'s type
:#,#s/old/new/g
-
To substitute all occurrences in the file type
:%s/old/new/g
-
To ask for confirmation each time add ‘c’
:%s/old/new/gc
Lesson 5:
:!command
executes an external command.
-
Some useful examples are:
-
:!ls
- shows a directory listing. -
:!rm
FILENAME - removes file FILENAME.
-
:w FILENAME
writes the current Vim file to disk with name FILENAME. -
v motion :w FILENAME
saves the Visually selected lines in file FILENAME. -
:r FILENAME
retrieves disk file FILENAME and puts it below the cursor position. -
:r !ls
reads the output of the ls command and puts it below the cursor position.
Lesson 6:
-
- Type
o
to open a line BELOW the cursor and start Insert mode.
- Type
- Type
O
to open a line ABOVE the cursor.
-
- Type
a
to insert text AFTER the cursor.
- Type
- Type
A
to insert text after the end of the line.
-
The
e
command moves to the end of a word. -
The
y
operator yanks (copies) text,p
puts (pastes) it. -
Typing a capital
R
enters Replace mode untilESC
is pressed. -
Typing
:set xxx
sets the option “xxx”. Some options are:
‘ic’ ‘ignorecase’ ignore upper/lower case when searching
‘is’ ‘incsearch’ show partial matches for a search phrase
‘hls’ ‘hlsearch’ highlight all matching phrases
You can either use the long or the short option name.
- Prepend “no” to switch an option off:
:set noic
Lesson 7:
-
Type
:help
or pressF1
orHELP
to open a help window. -
Type
:help cmd
to find help on cmd . -
Type
CTRL-W
CTRL-W
to jump to another window. -
Type
:q
to close the help window. -
Create a vimrc startup script to keep your preferred settings.
-
When typing a : command, press
CTRL-D
to see possible completions.
Press TAB
to use one completion.