Editing files
We can create files with touch
and by using cp
to
copy them. How do we edit text files and place information in them?
We shall learn how to use the UNIX vi editor. I recommend the
O'Reilly book on it if you want to become a power user (you do).
To create a file, type
vi <someFileName>at the UNIX command line. When I use it to open the file
bar
, I see this:
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "bar" 0L 0C
DO NOT TYPE YET!
The tildes on the side indicate a empty file. If you are working in
cs.ncssm.edu
, they are blue. The OL OC "bar" indicates that file
bar
has no lines and no characters. If the file bar
were nonempty, its contents would be displayed, then blue tildes would
fill any empty screen.
Before you hit any keys there is something important to know. The vi editor is a moded editor. It has two modes: command mode and insert mode. Command mode provides mobility and search/replace capabilities. Insert mode allows you to insert characters.
You always begin a vi session in command mode. There are lots of ways to get into insert mode. Here are a few.
i | insert characters before the cursor |
I | insert characters at the beginning of the line |
a | append characters after the cursor |
A | append characters at the end of the line |
o | open a new line below the cursor |
O | open a new line above the cursor |
There are other ways to enter insert mode, but this will do quite nicely for now.
There is one way to get out of insert mode. You do this by
hitting the escape (ESC) key. Let's now try this out. Go into your
file bar
and hit the i
key to enter text. Type some
text. Then hit ESC. To save your current effort type this anywhere:
:wThis will write the file; a message will appear at the bottom of the window indicating this has happened. Do not panic; that message is not a part of the file that is saved. To quit, type
:qthis will quit you out of the file. You can type
:wqto write and quit. The command
:qw
does not work for
(obvious) reasons. You have just done a simple vi session. You can
reopen the file bar
by typing vi bar
at the UNIX
command line; the contents you last saved will be redisplayed. You
should take a few minutes to try all of the ways of getting into
insert mode. Change the file and save it. Quit and display it to the
screen with cat
and more
.
Let's go back in our file now and learn some more useful commands. We will look at command mode commands now.
Some people like line numbers. To see them, you get into command mode
and type :set number
. Do this and watch them appear. Now
type :set nonu
or set nonumber
and watch them disappear. They are not a
part of the file. Here are some useful command mode mobility
features. Experiment with them in a file.
:<lineNumber> | go to indicated line number |
^ | go to the beginning of the current line |
$ | go to the end of the current line |
:G | go to the end of the file |
r (then a character) | replace character |
~ | change case upper->lower or lower->upper |
dd | delete line |
dw | delete word |
cw | delete word, then enter insert mode (change word) |
Finally we shall look at search capabilities. These all belong to command mode. Enter
/someString
in command mode and vi will seek out the first instance of that string in the file or tell you it is not found. Type an n to find the next instance. You can enter
?someString
to search for someString backwards from the cursor. Type p to find the previous instance. To do a global search and replace enter
:1,$ s/oldString/newString/g
to find every instance of oldString and convert it to newString. If you leave the g off the end, you will only change the first instance of oldString to newString on each line. (Think of g for global). You can do this on a range of line numbers by typing
:<beginLine>,<endLine> s/oldString/newString/g
One last thing bears mentioning. Do not use the scrollbar and the mouse to move around the screen. Use the arrow keys or go to a line number. If you use the scrollbar, you will see other parts of your terminal session, but if you attempt to edit, you will "snap" back into your file.
You can copy past with the mouse in a window or between windows. Select the text you want to copy. Go into insert mode and right-click to paste. This is the best way to copy and paste between files.
Experiment with these in some files. There are a lot of excellent tutorials on vi on the web; avail yourself of these to learn more. Remember the most important thing: you never stop learning vi!
Next,you will want to organize your files into directories.