Vi Text Editor Commands

As a brief introduction to vi, go through the following: First, type
vi x
at the Unix prompt. Assuming you did not already have a file named x, this command will create one. (If you have tried this example before, x will already exist, and vi will work on it. If you wish to start the example from scratch, simply remove x first.)
The file will of course initially be empty. To put something in it, type the letter i (it stands for “insert-text mode”), and type the following (including hitting the Enter key at the end of each of the three lines):
The quick
fox will return.
Then hit the Escape key, to end insert-text-mode.

Going Further: Other Frequently-Used Commands

You now know how to use vi to insert text, move the cursor to text, and delete text. Technically, the bare-bones set of commands introduced above is sufficient for any use of vi. However, if you limit yourself to these few commands, you will be doing a large amount of unnecessary, tiresome typing.
So, you should also learn at least some of these other frequently-used vi commands:
     h              move cursor one character to left
j move cursor one line down
k move cursor one line up
l move cursor one character to right
w move cursor one word to right
b move cursor one word to left
0 move cursor to beginning of line
$ move cursor to end of line
nG move cursor to line n
control-f scroll forward one screen
control-b scroll backward one screen

i insert to left of current cursor position (end with ESC)
a append to right of current cursor position (end with ESC)
dw delete current word (end with ESC)
cw change current word (end with ESC)
r change current character
~ change case (upper-, lower-) of current character

dd delete current line
D delete portion of current line to right of the cursor
x delete current character
ma mark currrent position
d`a delete everything from the marked position to here
`a go back to the marked position
p dump out at current place your last deletion (``paste'')

u undo the last command
. repeat the last command

J combine (``join'') next line with this one

:w write file to disk, stay in vi
:q! quit VI, do not write file to disk,
ZZ write file to disk, quit vi

:r filename read in a copy of the specified file to the current

/string search forward for string (end with Enter)
?string search backward for string (end with Enter)
n repeat the last search (``next search'')

:s/s1/s2 replace (``substitute'') (the first) s1 in this line by s2
:lr/s/s1/s2/g replace all instances of s1 in the line range lr by s2
(lr is of form `a,b', where a and b are either explicit
line numbers, or . (current line) or $ (last line)
:map k s map the key k to a string of vi commands s (see below)
:abb s1 s2 expand the string s1 in append/insert mode to a string
s2 (see below)
% go to the "mate," if one exists, of this parenthesis
or brace or bracket (very useful for programmers!)
All of the `:’ commands end with your hitting the Enter key. (By the way, these are called “ex” commands, after the name of the simpler editor from which vi is descended.)
The a command, which puts text to the right of the cursor, does put you in insert-text mode, just like the i command does.
By the way, if you need to insert a control character while in append/insert mode, hit control-v first. For example, to insert control-g into the file being edited, type control-v then control-g.
One of vi‘s advantages is easy cursor movement. Since the keys h,j,k,l are adjacent and easily accessible with the fingers of your right hand, you can quickly reach them to move the cursor, instead of fumbling around for the arrow keys as with many other editors (though they can be used in vi too). You will find that this use of h,j,k,l become second nature to you very quickly, very much increasing your speed, efficiency and enjoyment of text editing.
Many of the commands can be prefixed by a number. For example, 3dd means to delete (consecutive) three lines, starting with the current one. As an another example, 4cw will delete the next four words.
The p command can be used for “cut-and-paste” and copy operations. For example, to move three lines from place A to place B:
1. Move the cursor to A.
2. Type 3dd.
3. Move the cursor to B.
4. Type p.
The same steps can be used to copy text, except that p must be used twice, the first time being immediately after Step 2 (to put back the text just deleted).
Note that you can do operations like cut-and-paste, cursor movement, and so on, much more easily using a mouse. This requires a GUI version of vi, which we will discuss later in this document.

Clean install of OSX 10.8 Mountain Lion USB

Though most users are best served by the easy upgrade process to OS X Mountain Lion through the Mac App Store, some people want to perform a clean install and start with a blank slate. A clean install means the drive is completely erased and Mac OS X 10.8 is installed fresh, nothing else is on the drive, no apps are installed, and no files are included.
The process described below will format the selected Mac disk and erase everything on it, followed by performing a completely clean and fresh installation of OS X Mountain Lion.
We’d highly recommend backing up your Mac before performing a clean install, even if you have no intention on using it afterwards.
  1. If you don’t have it yet, get Mountain Lion from the Mac App Store but do not install it yet (orredownload it if you did install it)
  2. Create a bootable install drive for OS X Mountain Lion, make one manually with a USB drive oruse the LionDiskMaker tool to automate the process with a USB or DVD
  3. With the boot installer drive connected to the Mac, reboot and hold down the Option key
  4. Choose the “Mac OS X Installer” startup volume from the boot menu
  5. Select “Disk Utility” and choose the hard drive you wish to format, click the “Erase” tab, and then pull down the “Format” menu and select “Mac OS Extended (Journaled)” as the type, name the drive if you wish
  6. Click the “Erase” button and let the drive format – this is the point of no return
  7. When finished, quit out of Disk Utility and now select the “Install Mac OS X” option from the menu
  8. Choose your freshly formatted hard drive and install Mountain Lion
When the Mac reboots you will have a clean installation of Mac OS X 10.8 to work with.
At this point you can either import files and apps from the backup you made, manually copy over backed up files, or just start anew.
OS X 10.8 Mountain Lion will be offered exclusively through the App Store, familiar territory for Apple since OS X Lion was provided the same way. Thankfully it’s still possible to create a bootableOS X 10.8 Mountain Lion installer from any USB drive, be it a flash key or external hard drive.
By creating a bootable install drive, you can perform clean OS X 10.8 installs, install it on separate partitions, and install OS X Mountain Lion on Macs that don’t have internet connections. We’ll walk through the process here, but don’t forget to check OS X 10.8 system requirements for the destination Mac before beginning.
If you already have the Mac OS X Mountain Lion install DMG extracted, skip this first set of steps and jump directly to making the bootable drive below.

Download OS X Mountain Lion and Extract the DMG File

  1. Download OS X 10.8 Mountain Lion from the App Store
  2. Go to the /Applications/ directory and right-click on “Install Mac OS X Mountain” selecting “Show Package Contents”
  3. Open the “Contents” directory and then open “SharedSupport”, looking for a file named “InstallESD.dmg”
  4. Double-click on InstallESD.dmg to mount it on the desktop

Make the OS X Mountain Lion Install Drive

  1. Launch Disk utility and connect a USB drive to the Mac
  2. Select the USB drive from the left menu and click on “Erase” tab, choose “Mac OS X Extended (Journaled)” as the format, then click on the “Erase” button in the corner
  1. Now select the formatted USB drive from the left side and click on the “Restore” tab
  2. Drag the previously mounted “Mac OS X Install ESD” image into the “Source” section
  3. Drag the formatted partition to the “Destination” section, then click on “Restore”
  1. Confirm that the USB drive will lose it’s data and enter the Admin password when asked
Disk Utility will now create a bootable OS X Mountain Lion installer drive out of USB drive using the disk image, this can take a little while depending on how fast the drive and Mac are but 20-30 minutes isn’t unusual.
When finished, reboot the Mac and hold down the Option key to bring up the boot menu:
Select the orange “Mac OS X” option and you will boot into the OS X Mountain Lion installer, from here installation is the same as normal. Click on Continue, select the destination drive, and install.