Command | Description |
:w | writes the file (stays in vi). |
:q | quit vi. Will not work if you have made changes and not saved them. |
:q! | quit vi (an I really mean it!). Will exit evenif you have made changes and not saved them. |
:e fileX | edit fileX instead of this one. You can use any file name, including a new one. |
:e # | edit the previous file instead of this one. |
:e! % | re-edit this file again, throwing away any changes made and not already saved. |
:e! | re-edit this file again, throwing away any changes made and not already saved. |
:r fileX | read in the contents of fileX below the current line. |
:g/old/s//new/g | globally change old to new. We'll look at this one in more detail below. |
:g/old/s//new/g
So, g stands for "global". The first part, specifies what lines to change:
:g/old/s//new/gand means to globally search for the specified string, in this case old. The string could also be a regular expression, which lets you match all strings that follow a specific pattern. We will cover regular expressions in great detail later in the course. For now, just type in the string to change.
The next part specifies what to do on each line where the regular expression was found. In this example, we're doing a substitute.
:g/regex/s//new/gThe standard way to use s is
:s/old/new/
Notice that in our global change, the string to change (old) is empty. This indicates that we want to change whatever string was matched in the :g/old/ part.
At the end, the last g means to make the change globally across the entire line. Without it, only the first occurrence of old on each line would be changed.
:g/old/s/dog/cat/g
:7,18 /s/dog/cat/gWhen specifying an address, . (dot) means the current line. You can also offset the current line with plus. A $ means the last line.
So this:
:.,. + 3 /s/dog/cat/g
means to make the substitution from the current line to the current line + 3.
This:
:.,$ /s/dog/cat/g
means to make the substitution from the current line to last line of the file.
:g/old/d
:1,5 d
Command | Description |
CTRL-G | display the current line number. |
Command | Description |
20x | |
df4 | |
3cw | |
cfG | |
4dw | |
dF- |