There are a lot of help pages for vi (or vim as it is linked mostly in Linux systems and can be installed also in Windows), here I will list only those commands, I am using regularly. First version of the next lines I created on the last days of my job, and later, collegues told me that this was very helpful.
You know there are some states of vi:
command mode – you see your file and you can move in the text, but not edit
insert mode – to enter this mode, you must give one of the insert keys, to leave this mode, just press <esc>
command line – to enter the command line from command mode, just press : Then you are at the basic level of vi – vi is based on the ed editor, a line editor.
Calling vi
You cannot edit with one vi more than one file, but you can call more than one file at once, and go through this list without leaving vi (and keep settings, search pattern, buffers etc. between these files). But you must save each file before switch to the next one.
- vi file1 file2 file3
- vi file*
- vi `find . -name ‚file*’`
In first example you will be able to edit these 3 files,
- switch from one file to the next file in the list – :n
- switch from one file to the previous file in the list – :N
- to go back to the previous file independed from the list- :e#
(caution – :n will not load the file, you watched before, but will enter the next file in the list!) - to go to the start of the list :rewind
- to reload the file as it was before your last save – all your changes will be lost :e!
In the second example, you can edit all files, which match the pattern ‚file*‘
In the third example, you can edit all files, which match the pattern ‚file*‘ found by find command, this means also matching in subdirectories
Hint:
vi at least is a line editor as based on ‚ed‘. Although you see your file in full screen mode, you must be aware that your commands apply to a line or some lines.
Leaving vi
: x → this I prefere (don’t type the blank, I must do this as WordPress otherwise uses a silly icon)
:q → leave the file – if you did some changes, you will be prompted whether you really will leave
:q! → leave the file in any case, you will not be prompted – if you did some changes, they will not be saved.
:wq → some collegues do this – but why to type 2 letters, when it is possible with only one
:wq! → guess, what will happen: the file will be written and then leave vi in any case. Very dangerous! In case, the w(rite) command fails, you will not be prompted and informed about this value. Total loss of your changes!
:e! → reload file – undo all changes you did after the last write
Good, if you did same changes accidental and want to undo them.
:w → write your changes to file, but does not leave vi, you can continue to edit
:w newfile → write your changes to a new file – but you are still editing the old one! If you quit (:q) you will be prompted that you changes are not saved. To continue to edit your new file, switch with :e#! to this file (this was different at HPUX and previous versions of pure vi)
Basic commands which will bring you into the insert mode
a – insert text after the cursor position
i – insert text at the cursor position
A – add text at the end of line
I – add text at the start of line (uppercase i)
o – open a new line after present line
O – open a new line before present line
Then you can type your new text. Lease insert mode by pressing <ESC>
Basic commands working in the command mode
x – delete character at cursor position
dd – delete present line
D – delete rest of line
dw – delete rest of word
<del> will only move backwards in command mode, but in insert mode, it will delete before cursor
You can prefix these commands with a number
3x – will delete 3 chars
3dw – will delete 3 words
3dd – will delete 3 lines
Moving in command mode
Although you can move around your text with the cursor keys or even sometimes with the mouse, move inside vi is basicly a task of the four letters
j – down
k – up
l – right
h – left
The cursor keys are only aliases to these keys. Normally you will move with the keys, but in some special situations, it it worth to remember above definitions, if the cursor keys do not apply.
Hint: the old vi did not allow to move in the insert mode, the new linux vim does do it with the cursor keys. How this is managed, I don’t know. There the above letters to move do not work and will be used as text, of course.
To move more fast, you can use:
w – move one word
3w – move 3 words
To move one page down – <Strg>f
To move half page down – <Strg>d
To move one page up – <Strg>b
Move to top of file – 1G (no : before!)
Move to buttom of file – 0G
Move to line 10 – 10G
On which line I am, in which file I am – :f (ok – in vim line number is displayed always in lower right corner)
Searching
There are some facts to know.
- Normally you can issue the search commands in command mode, not on command line!
- Searching is normally case sensitive
To make searching case insensitive – :set ic (short cut for :set ignorecase) – to make it sensitive again – :set noic
Normally vim continues searching within the whole page – continue search after end of file at the beginning (or reverse). To disable this – :set nows (short cut for :set nowrapscan)
search forward – /
continue search forward – n
continue search backward – N
search backword – ? then n and N work just opposite
Search pattern can contain regular expressions, more see later.
Regular Expressions
You can use in search strings some special wildcards, some with special meaning. Unfortunately they are slightly different to those of shell and sql.
. (dot) – one wild character
* (star) – some wild characters
^ – start of line
$ – end of line
Example: /^a.b*.jpg$ → seach for lines, starting with a, any character 2, character 3 must be b, then any character and count is allowed, line must end with .jpg
Buffers
Undo buffer – you can undo your last command, and also undo your undo:
u – undo last command
you can undo all commands of you session (vi: only last command)
<Strg>r – undo your previous done undo steps (vi: u toggles)
all commands which erase something (x,dd,D,dw,...) will put the deleted text into a buffer stack
p will insert this buffer after the present cursor position – in a new line in case of line deletion, after the cursor in case of character deletions
P will do this before present line
nice command: xp will exchange just the order of 2 characters
Named buffers:
The above mentioned erase commands will be put the erased text into a stack buffer, named 1-9. As this is a stack, the contents will change during your session.
But you can also put items into named buffers with simple names like a-z. The contents of these buffers are kept during your vim session.
Your have to mark which buffer you want to use with „, eg. buffer a → „a, buffer 1 → „1
To put text into a buffer: „ayy → put whole line into buffer a, „byw → put word into buffer b („cyx does not work – but why to put only one letter into a buffer)
You can prefix also delete command with a buffer name: „edd
To use the contents of your buffer: „ap or „aP (see above for the difference of p and P)
Named buffers are kept, if you switch from one file to the next one with n, but also when you leave vim completely and reenter!
If you have larger sections of lines to delete or copy into a buffer, you can do this range commands – see below!
Command repetition
Your can repeat your last command: just press . (dot)
Your can prefix your command with the number, how often the command will be done
5x – will delete 5 character
5dd – will delete 5 lines
„a10yy – will put the next 10 lines into buffer a
Marks
You can mark lines in your text to be able to return to them or to used them for Range Commands
set a mark named m: mm
goto mark named m: ‚m
goto previous position: ‚ ‚ (double apo)→ useful after a search, so you can return to the position before your search.
Caution: when using n for next search, ‚ ‚ will return only to the position of the previous search
Useful also after Range Commands to come back to the start position
Range Commands
In command line mode ( : ) you can add a range for a number of lines, the given command will be applied:
:1,2 d → delete line 1 and 2 of the file → not so useful, as you have to know the line number
:.,$ d → delete from the present position ( . ) to the end of file ( $ )
:1,. d → delete from the top (1) to the present position ( . )
:’m,. d→ delete from mark m (‚m) to the present position ( . )
Substitute command
The substitude command will replace old text with new one, separated by a delimiter, normally a / But in case, a slash is also part of your text, it can be any other character not in your text. I prefer %. If the text to be replaced apear often in your line, only the first occurents will be replace – or you have to end your command with g for global. It is useful to use regular expressions in your search string, see above for details.
Examples:
add some chars in front of each line of the given range – :1,$ s/^/# new text/
this will add in front of each line the pattern „# new text“ – the ^ means that this is done at front of line
add some chars at end of line – :1,$ s/$/# new text/
this will add at end of each line the pattern „# new text“ – the $ means that this is done at end of line
Hint: As already mentioned above, if you have to work with file path pattern, e.g. /var/avitech/log – you have to replace the marker sign / by an other character – any are allowed, but should be not part of your text. Example:
:1,$ s%/var/log/%/run/log/% → will replace in the whole file the old path with the new one.
And now some examples, which made my audience always astonishing!
Example 1: create a file which contains a list of files: ls -1 > list.sh
now you have the file list.sh containing the file name in each line
enter vi and issue – :1,$ s/\(.*\)/mv \1 new_\1/
this will change each line from eg. „file1.lis“ to „mv file1.lis new_file1.lis“
explanation:
1,$ – you know, this applies to all lines in file
s – substitute command, / is the delimiter (caution: if you want to manipulate path names, you must use another delimiter, eg. % as mentioned above)
create a block: “\(“ start pattern, “\)” end pattern to mark a block, “.” means use any single character, “*”means as much as available characters
in the replacement section, you see the new characters, “\1” is the contents of the defined block in the search part
more difficult:
Create again a file which contains a list of files: ls -1 > list.sh
issue – :1,$ s/\(.*\)\.\(.*\)/mv \1.\2 new_\1.new_\2/
this will change each line from eg. „file1.lis“ to „mv file1.lis new_file1.new_lis“
You see, your can define more than 1 block, block 1 from start of line to character „.“ (dot), block 2 the rest of line.
As “.” is normally a wild card letter, you must mask it by “\”.
if there is no “.” in the filename, nothing will be changed in this line!
Colors, Refresh, and some other Hints
Sometimes I don’t like colours in the text as they make reading difficult due to bad colours. But sometimes they are set by default.
:syntax off will switch off the color (and on to reverse it) – as they are shown as syntax highlighting
If you like, you can set different colors. See /usr/share/vim/vim*/colors for different colour definitions, with :color evening you can change to one of theses
Sometimes it happens that the screen is smashed by some other characters no coming from your edit. To restore your screen, use for refreshment: <Strg>l
How to display special characters:
set list – display <cr>, <tab>, etc. don’t interpret them
set nolist – back to normal display
Summary
In above text are not all vim commands, of course. Only those, I am using. If somebody has some more helpful commands, let me know!
Hint
There exists also vim for windows! GVIM within a window, where you can also move in the text and mark items with mouse.
Just look for GVIM in the common download portals.
A link to another page with a lot of hints:
https://alvinalexander.com/taxonomy/term/3023
More switches to call vim see “man vim”!
eg. vim -b file – binary mode