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:wqto save all changes
- Deleting any character with
ESCx
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:
dfor 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:
-
ufor previous actions. -
capital u
Ufor all the changes on a line. -
CTRL-Rfor 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-Gto show the location of the file editing. -
Press
G(capital g) to go to the bottom of the file. -
Type
ggto go to the top of the file. -
Type number of the line and
Gto 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
enterto search. -
Type
nin order to search again forward,Nfor backward direction. -
When wrapscan is turned on, if the search reaches the end of the file it will continue at the start.
-
CTRL-Oto go back where you came from,CTRL-Igoes forward. -
Matching parentheses search by move the cursor to (, ), {, }, [, ] then type %.
Lesson 4.3: The substitute command
-
Type
:s/old/new/gto substitute ‘new’ for ‘old’. -
s/thee/thewill only changes the first occurence. -
Adding the
gflag 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/gwhere #,# are the line numbers of the range of lines
where the substitution is to be done.
-
Type
:%s/old/new/gto change every occurrence in the whole file. -
Type
:%s/old/new/gcto find every occurrence in the whole file,
with a prompt whether to substitute or not.
Lesson 4: Summary
-
CTRL-Gdisplays your location in the file and the file status.
-
Gmoves to the end of the file. -
number
Gmoves to that line number. -
ggmoves 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
nto find the next occurrence in the same direction
or N to search in the opposite direction.
CTRL-Otakes you back to older positions,CTRL-Ito 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:
:!commandexecutes an external command.
-
Some useful examples are:
-
:!ls- shows a directory listing. -
:!rmFILENAME - removes file FILENAME.
-
:w FILENAMEwrites the current Vim file to disk with name FILENAME. -
v motion :w FILENAMEsaves the Visually selected lines in file FILENAME. -
:r FILENAMEretrieves disk file FILENAME and puts it below the cursor position. -
:r !lsreads the output of the ls command and puts it below the cursor position.
Lesson 6:
-
- Type
oto open a line BELOW the cursor and start Insert mode.
- Type
- Type
Oto open a line ABOVE the cursor.
-
- Type
ato insert text AFTER the cursor.
- Type
- Type
Ato insert text after the end of the line.
-
The
ecommand moves to the end of a word. -
The
yoperator yanks (copies) text,pputs (pastes) it. -
Typing a capital
Renters Replace mode untilESCis pressed. -
Typing
:set xxxsets 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
:helpor pressF1orHELPto open a help window. -
Type
:help cmdto find help on cmd . -
Type
CTRL-WCTRL-Wto jump to another window. -
Type
:qto close the help window. -
Create a vimrc startup script to keep your preferred settings.
-
When typing a : command, press
CTRL-Dto see possible completions.
Press TAB to use one completion.