Vi is a text editor that is included by default in every linux, unix, and *bsd installation. Every person who’s serious about using this should learn the basics of it. Very powerful, but a bit hard to learn, it’s something that everybody who manages a linux, unix, *bsd box should learn to use at least on a basic level.
There will be times (I promise you) where you will need to use it and not have the facilities to download and install the editor you normally use.
To launch vi and create a new file, simply go to your location where you want to create the file and simply type:
if you want to modify a file, do the same, just cd to the location where the file is and just type vi filename.you.want.to.modify
Getting around.. Moving the cursor.
Arrow Keys… NOT! vi does not use arrow keys, but to move the cursor around, you would use the following four keys: H,J,K,L (respectively, left, down, up, right).
Now like every other editor, you would move the cursor to where you want it to be (make your edits) and do one of the following:
type: i to insert from the following location. When done inserting text, just hit the ‘esc’ (escape key)
type: o to open a new line below where the cursor is so you can enter a new line.
type: x to delete the current character.
type: /search term to find instances of your search term. You can just hit ‘/’ to find the next instance.
type: dd to delete the current line.
These are the basics of moving around and doing stuff in vi. Now for some useful tidbits that believe me you’re gonna be glad you know.
Type: ‘esc’ then ‘:q!’ to quit without saving because you messed up.
Type ‘esc’ then ‘:wq!’ to write your changes and quit, because you’re done making changes.
Type’esc’ then ‘: w <filename>’ to write your changes to a different file.
The one super-power I give vi. Search and Replace Globally.
Let’s say you want to replace every instance of the word, “tomato” with the word “carrots” in the file. Something you’ll eventually want or need to do.
Type ‘esc’ then ‘:%s/tomato/carrots/g’
The slash g means global. I’m pretty sure you get the idea. There are so many other things vi can do, but it would literally be a small novella. I exclusively use vi for all my editing tasks. Why? It’s always there and it’s consistent and reliable.
Hope this helps!