The purpose of this lesson is to introduce you to the many choices available in text editing and introduce you to the vi editor.
Since the earliest days of computers, people have encoded information in some way in order for a computer to understand it. People used paper tape with holes, punch cards, and casette tapes. Today, we use a keyboard. We type and store information on the computer. For many tasks in Unix and other work, plain text is all we need. So we need just a plain text editor. Not a word processor, not a page editing system, but just a plain text editor.
There are hundreds and hundreds of software programs that give you the ability to create and edit plain text files.
You just need to pick the one right for you and become excellent at using it.
The vi skills I learned more than 17 years ago have taken me through employment as a software engineer, three graduate degrees, and my present work in Web publishing. During all this time, I have known graduate students and co-workers who rejected vi and instead use a trendy, editor-of-the-moment. I have just quietly kept on using vi. I've never had to learn another editor, and I do not intend learn another. I even use Lemmy so that I can edit files in Windows using a vi editor clone.
The Unix command vi starts up the visual editor. The vi editor is one of the most common plain text editors available on nearly every Unix system. While many claim that vi is not easy to use at first, vi nonetheless is readily available and widely supported.
I will provide instruction here in vi. You will not have vi commands on the test, so you don't have to memorize vi. If you don't use vi for your work, you are responsible for choosing your own plain text editor and becoming adept at using it.
How vi Works
You start a vi editor session to edit a file called filename.txt like this:
$ vi filename.txt
When you issue this command, the appearance of your screen will change. Rather than seeing the shell prompt and the previous lines of the Unix commands and output, the entire Unix window or screen will appear to be the contents of the file filename.txt. If filename.txt was blank, or hadn't existed before you invoked the vi command, the screen will appear mostly blank.
This is the visual representation of a file you are editing. You use the keyboard as your interface to enter text or enter commands to manipulate that text.
The key thing to remember is that vi has two modes: command, and insert.
When you are in command mode, everything you type on the keyboard gets interpreted by vi as a command.
When you are in edit mode, most everything you type on the keyboard gets interpreted as characters and will appear in your file--letters, numbers, punctuation, line returns, etc.
When you are in insert mode, you can switch to command mode by pressing the Esc key on your keyboard (this is often at the left and upper portion of your keyboard).
When you are in command mode, there are many keys you can use to get into edit mode, each one gives you a slightly different way of starting to type your text. For example, if you are in command mode, you can simply type i on your keyboard to enter edit mode (i stands for insert). Then, the characters you type go into the file.
There are hundreds of vi commands; and it would take a fifteen week course to cover all the ways that you can use the vi editor to edit files, run commands, and do an astounding amount of things.
For a new user, this can be baffling and unnecessary. Instead, use this abbreviated set.
Alternatives to vi
Unix Text Editors
$ pico filename.txt
$ emacs filename.txt
A Cut and Paste Method
If you know that you will never have to work in a Unix-only environment, you might be able to get along without knowing vi. For example, you might always work on a PC, with a Unix window available to run your Unix commands. If so, you might use a simple Windows-based plain text editor to create and edit files. For example, if you want to edit a file called apple.txt.
- Edit the contents of apple.txt using a Windows-based plain text editor; save apple.txt on your PCs hard disk;
- Open apple.txt and copy its entire contents into the Windows clipboard by using the Select All/Copy option of the program
- If you are changing the contents of apple.txt
as it is on the Unix system to the version of apple.txt
on your PC, remove the old version in your Unix
account like this:
$ rm apple.txt
- In your Unix window, bring up a vi session
to contain the new version of apple.txt like this:
$ vi apple.txt
- You are in command mode initially in the vi editor, so just press i to get into insert mode, and then use your windows Paste option to put the contents into apple.txt.
- Hit the Esc key to get back into command mode, then ZZ to save the file and exit vi.
- You now have a copy of apple.txt in your Unix account that is the same as is on your Windows PC.
A File Transfer Method
You could edit all your files using a PC-based text editor, then transfer files to your Unix account using a software package on your PC that supports file transfer protocol (FTP). You can find freeware and shareware ftp programs on hotfiles.com.
After installing ftp software on your PC, you would:
- Use your PC's text editor to edit your file
- Use your PC's ftp application to transfer your file to your Unix account.
Exercise: Find your favorite text editor
Experiment with a couple of the text editors available. If none of the Unix-based ones work well for you, figure out what does and how you can accomplish your assignments. Make sure the editor you choose edits and creates plain text and NOT word processor files, rich text files, Microsoft word or other files, or any other type of file other than plain text.
The bottom line is that you need to figure out what works best for you, then get good at it so that you can efficiently do your work.