vi editor commands for Unix, Linux and Mac

8 Oct 2014

About editor might have been written as about other issues, especially about the vi editor already more. He is one of the older representatives and remains one of the most important tools in Unix, Linux, and perhaps also in the Mac environment. Of course, today there are many editors who have become each an important part of the development and administration process, particularly noteworthy are sublime text and textpad. But one of the fastest representatives at all, not only on servers, is the editor vi.

vi is considered controversial and also acts as a deterrent to some, because it works a totally different ideology follows. In the operation of all other programs and editors one is usually equal in editable mode, not in vi, you have to give him only a instruction feedbacks that you would now like to add something. Other functions are only some getting used to, but the learning curve is very steep and with a few keystrokes you can reach over with your mouse and keyboard from other programs.

This short command summary of the vi editor (and his clones VIM, calvin, elvis, lemmy, Nvi, WinVi, ...) is not intended as a compendium to document all the commands and the honored readers alike to take the courage that was indeed documented with countless here commands the reference of the writer, but also at the same time the desire to learn this editor was taken.

How to start with vi

First we open the editor on the console with this command and create the non existing file "test.txt".

vi editor, open and create a new file

Then we want to insert something by first press the key "i", now we are in the insert mode, now we write the words "Hello World".

vi editor, switch to insert mode and type Hello World

Now we leave the insert mode, by press the "ESC" button, save the results and close the vi editor by insert the keys ":", "w" and "q".

vi editor, escape from insert mode, save and close the result

Now you are ready to perform more experiments and experiences.

subtitlecommanddescription
helpvi -hstart help page of vi editor (man vim)
modinormal modevi knows three different modi, the normal mode (usally to open a file and navigate in the lines)
insert modeswitch from normal mode to insert mode or to use command line mode by press one of this characters "i", "I", "a", "A", "o", "O", "c", "C", "s" or "S", to go back to normal mode use the ESC button
command modeusally used to save a file with insert :w or leave a file with :p or do both with :wq, to view file in coloured mode set :syntax on
startvi myfile.txtopen vi editor with file myfile.txt OR create a new file when file does not exits
vi -R myfile.txtopen vi editor with file myfile.txt in read-only modus
vim myfile.txtopen vim editor (vi clone)
vi +99 myfile.txtopen vi editor with file myfile.txt on line 99
vi -r myfile.txtrecovery of a corrupt file myfile.txt (after a crash of vi)
vi +/PADERN myfile.txtopens file myfile on first position with PADERN
basic:qquit editor
:q!quit editor force
istart insert mode (more on subtitle insert)
ESCleave the current insert mode
:wwrite to file
:w!write to file force
:w myfile.txtwrite to file myfile.txt
:wqwrite to file and quite VI
:syntax onstart coloured mode
navigationhgo left
jgo down
kgo top
lro right
:99jump to line 99
99takes you 99 lines down from your current position
0go to first character of line
$go to last character of line
/PADERNsearch "PADERN" forward in file
?PADERNsearch "PADERN" backward in file
nsame search as before forward
Nsame search as before backward
5Ggo to the 5 line in file
Ggo to the end of the file
redo, undo.redo last command
uundo last change
Uundo last change in whole line
deletedwdelete word
dddelete line
dggdelete everthing from your current line to the top of your line
Ddelete until end of line
:g/^$/ddelete all empty lines
:g/with this/ddelete all lines with this
:g/^\s*$/ddelete all lines that are empty or contains white space
:v/^\s*"/ddelete all lines that are not empty or contains white spare
:v/this\|that\othersl/ddelete all lines that not this, that or others
copy and paste"ayycopy current line in named buffer a (vi could work with several buffers a,b,c)
"adddelete current line in named buffer a
"appaste from buffer a in
"ayGcopy all lines to the end of the file in buffer a
replace:g/old text/s//new text/gcombined with ex command :g, replace "old text" with "new text" in all lines
:s/replace this//"replace this" with nothing in all lines
:%s/r/sreplace r with s
:%s///greplace all ^M, therefore you must enter and then
enhanced insertistart insert mode before current position
Istart insert at the beginning of the current line
aappend after current character
Aappend after current line
ostart insert on a new line below
Ostart insert on a new line before
ssubstitute one character and start insert mode
Ssubstitute line and start insert mode
several filesvi file.1 file.2 file.3open vi with file.1, file.2 and file.3, file.1 is first visible
:nopen next file.2, and so on (maybe it is necessary to save the file that you edit before)
:rewrewind, at the end of all file.*, show file.1 again
:e#open file before (opposite from :n)
:e nextfile.txtopens nextfile.txt, maybe it is necessary to save the file that you edit before or you open it with :e! nextfile.txt
:r myfile.txtinsert file myfile.txt on current position
:! ls -ltrenable you to call the shell command ls -ltr from vi


If, despite all the care one error have crept in, so I ask this to be made ​​known to me.

No comments:

Post a Comment

newer post older post home