Working with Editors

Now on to the wonderful world of editors. I'm talking here about computerized line editors, as opposed to those who want to know whether your book will be delivered in time for the original deadline.

There are times when you need to work with an editor, whether to edit a configuration file, write a script, or do some quick edits on the company Web site. In the Linux world, there are many alternatives, and your Ubuntu system comes with a few. Let me introduce you to a friendly new member of the family.

Meet gedit

Your GNOME desktop comes with a very nice and powerful editor called gedit (command name gedit). With colorful language-sensitive syntax highlighting (sh, C, C++, Perl, HTML, and others), tabs, and a spell checker, gedit is easy and fun to work with. To see gedit in action, look at Figure 21-2. You can start gedit on its own or specify a filename to start with. Notice that I started my session using gksudo because I was editing a system script.

gksudo gedit /etc/profile 

Figure 21-2. gedit is an easy-to-use editor that supports tabs so you can work on multiple files simultaneously.


In the preceding example, I use gksudo instead of sudo. This is a graphical frontend to sudo. It's also the best (and correct) way to run a graphical command that requires administrative privileges via the <Alt+F2> quick-start.

As you can see by looking at Figure 21-2, gedit supports tabs, thereby making it simple to work on multiple files at the same time. Just click the tab to select the file you want to edit. Basic editing functions like cut, paste, find, and replace, are available directly below the menu bar. To edit a file without starting from a command line, click the Open icon, which opens up the standard GNOME file selector.

For years, I've been a fan of vim, the next editor I will discuss, but I appreciate the beauty of a simple, friendly editor like gedit. It's easy-to-use with a learning curve close to zero. Still, there may be times when you don't have access to gedit and times when a slow, remote connection makes a graphical editor too painful to consider. It's at those times that you'll be thankful for a simple shell window and vim to get your work done.

I Am vi, the Great and Powerful

You can almost hear a fearsome voice echoing eerily around the walls of your office or home. If there is one editor that strikes fear in the hearts of newbies everywhere, it is certainly vi (or vim), the visual editor. vim stands for vi improved and is the version of vi that you find in your Linux system. Anyhow, pay no attention to that fearsome voice behind the program. vi is not so frightening after you get to know it. To start vi, enter the command name on its own or start it by specifying a filename.

vi /tmp/test_file 

To start entering information, press <i> to go into insert mode. Type several lines and then press <Esc> when you are done editing. Moving around with vi is easy. Depending on the terminal emulator you are using, you can typically use your cursor keys. In the absence of cursor key control, the up, down, and sideways motions are all implemented with single keystrokes. To move left, press the letter <h>. To move right, press <l>. The letter k is up, and j is down.

A little further on, I've included a quick cheat sheet.

When you work with vi, the <Esc> key is your friend. If you don't know where you are or what mode you are in (insert, replace, or append), press the <Esc> key. You go back to normal vi command mode. Your second best friend is u or U, which stands for undo. The uppercase undo undoes every change to the current line and the lowercase undo undoes the last change only.

:q, :w, :wq, and ZZ

Finished editing? When it comes time to save your work, press <Esc> (to get out of whatever mode you are in) and type ZZ. Another way to exit is to type :wq (write and quit). At any time during an editing session, you can type :w to save your current work. Finally, if you really don't want to save anything you have done, type :q! The exclamation point essentially says that you won't take no for an answer. Had you modified the file and simply typed :q, vi would warn you that you were trying to exit from a modified file without having saved your changes.



These commands let you move around:

Line (or cursor) up


Line (or cursor) down


Single character (or cursor) right


Single character (or cursor) left


Move one word right


Move one word left


Move cursor to the first character in a line


Move cursor to the very beginning of a line


Move cursor to the end of a line


Jump to the end of a file


Jump to the beginning of a file


(You can also type a number followed by gg and jump to that line.)

These commands let you start inserting text in various ways:

Start inserting text before the current character


Start inserting text at the beginning of the line


Start inserting text after the current character


Start inserting text after the last character in the line


Open a blank line below the current position


Open a blank line above the current position


These commands let you delete or change characters, lines, and so on:

Delete a single character


Delete three characters


Delete a whole line


Delete 20 lines


Delete a word


Change an entire word (press <Esc> to finish)


Change five words (press <Esc> to finish)


Replace a single character


Start replacing text (until you press <Esc>)


Quick Tip

Need help while in vi? Make sure you aren't in insert or replace mode, and then type :help.

I urge you not to let vi frighten you. Get to know it. The likelihood that you will ever log on to a modern Linux (or UNIX) system that doesn't have some form of vi installed is virtually nonexistent. That said, if you need more information than I've given you here, consider the vi tutor. This little tool is distributed as part of the vim documentation. It is essentially a text file that tells you what to do and how to do it as you read it. To start the tutor, type the following command:


When the tutorial starts, you should see a picture like the one in Figure 21-3. The entire tutorial takes between 25 and 30 minutes for most people to complete.

Figure 21-3. Learning vi or vim from the vimtutor.

Recovering a vi Session

From time to time, you may find yourself trying to edit a file, but someone else, maybe you, is already editing it. That session may be open or something may have happened to terminate it accidentally. As a result, you get a nice, long-winded message along the lines of "swap file found" and a whole lot of information on what you can do about it. Here's a shortened version of that message:

E325: ATTENTION Found a swap file by the name "textfiles/.listing1.swp"           owned by: mgagne   dated: Sun Dec 28 14:43:20 2003          file name: ~mgagne/textfiles/listing1 

To locate these files, you can use the famous find command and look for anything with a .swp extension. Another and better way is to have vi report on these for you. You can check for the existence of swap files by using the -r option, which provides you with a little more information than the simple find.

Swap files found:    In current directory: 1.    .linux_companies.swp           owned by: mgagne    dated: Mon Sep 15 14:38:55 2003          file name: ~mgagne/textfiles/linux_companies           modified: YES          user name: mgagne    host name: francois         process ID: 2266 2.    .sas.swp           owned by: mgagne    dated: Mon Sep  8 09:49:02 2003          file name: ~mgagne/textfiles/sas           modified: no          user name: mgagne    host name: ultraman         process ID: 6340    In directory ~/tmp:        -- none --    In directory /var/tmp:        -- none --    In directory /tmp:        -- none -- 

Power vi: Start-Up Options

The next time you need a reason to use vi over one of the other editors, consider some of these tricks for getting to what you want as quickly as possible.

vi +100 ftl_program.c

This takes you right to line 100 in the file called (in this case) ftl_ program.c. This can be a great timesaver when you are compiling programs and something goes wrong, as in the following example:

 gcc  -O2 -Wall  -c -o ftl_program.o ftl_program.c ftl_program.c:100: parse error before `<' make: *** [ftl_program.o] Error 1 

Another useful start flag is the same one you use to search for text inside a file: the forward slash (/). In the next example, I want to get back to the same place I was working on in my file. To mark my place, I had written my name on a blank line. This vi starter gets me right to where I was working:

vi +/Marcel ftl_program.c

Note the plus sign before the slash.

Other Editor Options

gedit and vi (or vim) represent only two of many, many different editors available to the Linux user. Your Ubuntu Linux system comes with a few others. One of my old favorites is another nongraphical editor that makes a nice alternative for people who, having gotten up close and personal with vim, would prefer something else. It's called Nano (command name nano), an enhanced editor based on Pico (in case you are curious). Nano, by the way, stands for Nano's ANOther editor.

Yeah, I know.

Moving to Ubuntu Linux
Moving to Ubuntu Linux
ISBN: 032142722X
EAN: 2147483647
Year: 2004
Pages: 201

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: