vi editor

vi editor

The Unix vi Editor

For me, the vi editor is like a second language. I use a simple subset of the commands for everything.

I am always logged in with root permission, and have done so my whole IT life without issue, so no, folks who say you cannot are wrong. If you know Unix (which I grew up on) you learn how to use it, and are aware of when you commit commands that do not reverse out. So, we learn to do things like “ls *” to see what the output is before we change to “rm *”!

After we have done sudo su to go into root, I type:
set -o vi

Here are the basics that will do anything, say a file called fred.txt:

vi fred.txt
Then….

Escape key to go into the editor.
hjkl for movement – this is normal keyboard movement outside of the ESC sequence. ESC takes you into actual editing. After edits, hjkl moves you around the text.
i for insert (esc to exit the insert of course).
o to insert below a line.
O to insert above a line.
ESC SHIFT : w SHIFT ! to (force) write the file (e.g. save your edits without exiting, and use on non-writable files when root user.
ESC SHIFT : SHIFT Q (e.g. Capital Q) to quit without saving (w will have saved though).
ESC SHIFT Z Z (two capital Z’s) to save and exit without a force write.
To replace, append, delete:
Use h,j,k, or l to move to the required character, r (to replace) and type in the new character, all of which do not need the ESC character – but you can type ESC to your hearts content anyway.
Without ESC, SHITFT A to append to the end of a line – start typing after SHIFT A, and use ESC to exit the editing you are appending.
SHIFT G to move to the last line in the file. Then use something like SHIFT o to add a line below.
Again, SHIFT Z Z (hold the SHIFT key to give you two capital Z’s) to exit and save. If you want to quit instead, use the quite option above.
Use your mouse to highlight for copy and paste. If you are in Debian, instead of Linux2023, you will find cut and paste really awkward, and work out what sequence you use to activate the copy and paste.
To delete a line, position the cursor and type dd (two d’s). You can repeat for multiple lines in sequence.
To delete a character(s), position the cursor and type x (multiple times for more characters).
You cannot undo anything, so major edits should use :w! to save prior to any mistakes, and if confused, simply use :Q! to quit without saving your possible mistakes.
There are many vi commands, so it is possible to mistype and do things like accidentally inserting content. You can find out other options on the Internet, but the above covers everything, as I have found out since the mid-1980’s on.
Sometimes you may have the same file in editing mode in another terminal, where you try to edit it twice using another terminal. You may see various messages to work through.