The vi editor began life as a UNIX editor. It was originally intended to edit only plain text files so it doesn't assume a GUI nor depend on mouse events. Since it was created long before the graphical interface it's not as easy to learn but it's much faster and far more efficient that other "enhanced" editors. If you fancy yourself a techie, you'd better learn vi, GUI editors are for weenies.
You'll find the vi editor in the cygwin directory under the bin directory (c:\cygwin\bin\vi.exe). Since it's a Windows exe type file you can just double-click it to start it up. As an aside, Cygwin can provide an entire Linux environment if you want. While in the c:\cygwin\bin\vi.exe double-click the bash.exe file. This will load an actual Linux type shell using the command interpreter, bash. Very nice.
You can set up a login environment for yourself including .bashrc and .bash_profile which provides an initial environment for every time you run bash. This is really the best way to use vi too. Start bash and then invoke vi. I like Cygwin because it offers all the really useful UNIX/Linux stuff running under Windows (Microsoft must hate these guys). You can even get Xwindows (I prefer Xming though) and xterm.
The advantage of xterm under Xwindows is that you can easily copy and paste between the x terminals. I used to use MKSTools but they're too expensive, Cygwin is completely free. When we combine a Linux kind of environment with the vi editor, we have the ideal editing setup, especially if you're into programming.
So the best all-purpose editor for creating and editing programs and scripts is vi . This editor has lots of very useful features for working with shell scripts or batch files and is by far the most convenient way to manage editing tasks.
Unfortunately, it is also kind of tricky to master. The best way to master vi is by simply using it. A good place to start is with the .bashrc file (.bashrc and .bash_profile both modify the environment for bash to work in. When you login, .bash_profile runs automatically if it exists in your home directory. If you start a new instance of bash it will read the .bashrc file. These two initialization files can be identical; it depends on what you want a sub-shell to do).
The vi editor also has an initialization file named, .exrc. This file brings up vi with features you set here. For example:
se wrapmargin=5The features are set by the set command or the shorthand version, se. You can "map" one character to another with the map command. In the example here the wrapmargin feature will force a line-break 5 characters from the end of the line to prevent line wrapping. Line wrapping is handy but it can make a file hard to read.
The map feature is used here to for an uppercase G to be recognized when you type a lowercase g. The letter, G, is used to "goto" a line in the file so you would type, 20G to go to the 20th line. By using map, you can use a lowercase g instead.
Another convenient feature is the showmode option which just shows you when you're in edit mode. You can also add the se number option to show the line numbers in a file. You may not want this as a default setting so you can leave it out of the .exrc file and use it in when in vi, :set number instead.
As mentioned above, this file is used to set up the environment for you after you've logged in. Like most shell commands, vi needs an argument, something to work on. This will be a file of course and, since .bashrc is the file we want to edit:
This will "open" the file and display the first screenful of text in the file. If it's a new file there won't be anything so vi will display the tilde character (~). There are several ways to enter new text depending on where the cursor is located. Normally the cursor will be positioned on the first character of the first line.
The vi editor is a "screen editor" meaning that it displays one screen full of the file at a time and also that you can move around within that screen to edit stuff. To do all this it uses commands, usually of just one letter. There are several ways to access these commands. The simplest commands are those having the simplest function.
To quit vi
To write the changes to disk
To both write and quit vi
To force an action use the "bang"
To move the cursor to the bottom of the file
To move to the top of the file
To delete a range of lines
to substitute one string with another
To search for a string of characters
/some string of characters/
Notice that these commands use another character at the beginning of the command. The colon character (:) is used to tell vi that the action applies to the entire file in most cases. The slash character is used to specify some string of characters ("words"). This hints at the most basic functions of vi, but to actually edit the file you have to use another kind of command. These commands don't use the colon (:), you just enter the command:
add (append) text to the right of the cursor
Hit the Esc key to exit
insert text to the left of the cursor
Hit the Esc key to exit
create an empty line below the current line and enter editing mode
Hit the Esc key to exit
create an empty above the current line and enter editing mode
Hit the Esc key to exit
delete everything from the cursor to the end of the current line
delete the entire line
copy the current line
paste deleted or copied text below the current line
paste deleted or copied test above the current line
skip to the beginning of the next "word"
skip to the beginning of the next "word" including any punctuation
skip to the end of the current "word"
skip to the end of the current "word" including any punctuation
These commands are used when you are actually editing a file. When in the text entry mode (a,i,o,O) you have to hit the Esc key to exit the editing mode. There's lots of other commands but these are enough to get you started.
Now we can edit the .bashrc file (letters that are bold are commands you will enter:
Hit the Esc Key here ...
At this point you've added some text to the bottom of the file. If you write this file to disk and then quit,
this file will now be permanently stored on the disk.
Now lets add something else:
Hit the Esc Key here and then write and quit:
You can edit, save and edit again as often as you want. Since this is your startup file that will execute every time you login, let's execute it now to see what it does:
Notes on working in the Linux Environment
This technique is necessary because the shell will usually execute a copy of itself to run the script. This copy will execute and then disappear returning control back to the original shell. Whatever that copy (sub-shell) does to the environment is forgotten as soon as it finishes so any settings it may have changed are also forgotten. The dot (.) command tells the original shell to not use a sub-shell but to run the script itself and replace the current environment with the one specified in the script.
You've probably noticed that we're not in Windows-land anymore. While we are running UNIX/Linux programs in Windows, the working conditions are considerably changed. By using vi with the bash shell, we can very closely emulate a Linux environment. Add to that all the Linux commands that we have to play with and the underlying Windows platform becomes irrelevant.The concept of shells and sub-shells is important to understand. Your login shell is like book you bought for some class. Everyone in that class starts off with the same book but everyone adds something. Some people like to highlight stuff, some might use post-it notes for bookmarks, some might write notes in the margins. Every book starts out as identical to every other book but gets modified by the reader.
A login shell is the original book and a sub-shell is a copy that may get modified. If one of the modified copies replaces the original it becomes the original. All subsequent sub-shells now use the new original copy. Doing it this way means that the environment settings won't be forgotten but will instead become part of the original shell. Sounds complicated but it makes sense once you've used it a few times.
So the dot command above will replace the existing environment with the settings added to the .bashrc file. The changes you've added are now permanent and all sub-shells from this point on are copies of that changed environment. Now you can see the effect of the changes. If you don't like the changes, edit the .bashrc file again, change stuff, write and quit vi and then run the dot command again.
Any changes to the shell's environment while in a sub-shell will be lost when the sub-shell finishes. To preserve changes to the environment use the export directive to force the change to exist in all subsequent sub-shells. If export is used in the .bashrc file then that environment change will persist in all the sub-shells. If you enter the .bashrc entries above and run the dot command on .bashrc, you will immediately see the difference. The prompt (the PS1 variable) will be different
It's important to understand that UNIX (and, later, Linux) has always been a multi-user and multi-tasking operating system. This means that many users can be executing many commands at the same time. To keep everything straight, each user is given a copy of the shell at login. This copy belongs to that user and that user can modify the environment using the .bashrc startup file. The shell that belongs to a user can "spawn" (start) sub-shells and each sub-shell will belong to the original login shell for that user.
This makes the multi-user, multi-tasking capability possible. Understanding this design is necessary to effectively manage your environment. It's also helpful when using the vi editor to know how to interact with the environment. In fact it's part of vi's power and utility. With a package like Cygwin, you can play with a Linux-like environment while keeping the familiar (if lame) Windows "look and feel".
Return to the Contents Page