Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

 Purchase  Copyright © 2002 Paul Sheer. Click here for copying permissions.  Home 

next up previous contents
Next: 7. Shell Scripting Up: rute Previous: 5. Regular Expressions   Contents

Subsections

6. Editing Text Files

To edit a text file means to interactively modify its content. The creation and modification of an ordinary text file is known as text editing. A word processor is a kind of editor, but more basic than that is the UNIX or DOS text editor.

6.1 vi

The important editor to learn how to use is vi. After that you can read why, and a little more about other, more user-friendly editors.

Type simply,

 
vi <filename>

to edit any file, or the compatible, but more advanced

 
vim <filename>

To exit vi, press Esc, then the key sequence :q! and then press Enter.

vi has a short tutorial which should get you going in 20 minutes. If you get bored in the middle, you can skip it and learn vi as you need to edit things. To read the tutorial, enter:

 
vimtutor

which edits the file

    /usr/doc/vim-common-5.7/tutor,
    /usr/share/vim/vim56/tutor/tutor, or
    /usr/share/doc/vim-common-5.7/tutor/tutor,
depending on your distribution. [By this you should be getting an idea of the kinds of differences there are between different LINUX distributions.]You will then see the following at the top of your screen:

 
 
 
 
5 
 
 
 
 
10 
===============================================================================
=    W e l c o m e   t o   t h e   V I M   T u t o r    -    Version 1.4      =
===============================================================================
 
     Vim is a very powerful editor that has many commands, too many to
     explain in a tutor such as this.  This tutor is designed to describe
     enough of the commands that you will be able to easily use Vim as
     an all-purpose editor.
 
     The approximate time required to complete the tutor is 25-30 minutes,

You are supposed to edit the tutor file itself as practice, following through 6 lessons. Copy it first to your home directory.

Table 6.1 is a quick reference for vi. It contains only a few of the many hundreds of available commands but is enough to do all basic editing operations. Take note of the following:

  • vi has several modes of operation. If you press I, you enter insert-mode. You then enter text as you would in a normal DOS text editor, but you cannot arbitrarily move the cursor and delete characters while in insert mode. Pressing Esc will get you out of insert mode, where you are not able to insert characters, but can now do things like arbitrary deletions and moves.
  • Pressing Shift-Colon (i.e.,  :  ) gets you into command-line mode, where you can do operations like importing files, saving of the current file, searches, and text processing. Typically, you type   :   then some text, and then hit Enter.
  • The word register is used below. A register is a hidden clipboard.
  • A useful tip is to enter :set ruler before doing anything. This shows, in the bottom right corner of the screen, what line and column you are on.









Table: Common vi commands
Key combination Function
h     or     Left Arrow Cursor left
l     or     Right Arrow Cursor right.
k     or     Up Arrow Cursor up.
j     or     Down Arrow Cursor down.
b Cursor left one word.
w Cursor right one word.
{ Cursor up one paragraph.
} Cursor down one paragraph.
^ Cursor to line start.
$ Cursor to line end.
gg Cursor to first line.
G Cursor to last line.
Esc Get out of current mode.
i Start insert mode.
o Insert a blank line below the current line and then start insert mode.
O Insert a blank line above the current line and then start insert mode.
a Append (start insert mode after the current character).
R Replace (start insert mode with overwrite).
:wq Save (write) and quit.
:q Quit.
:q! Quit forced (without checking whether a save is required).
x Delete (delete under cursor and copy to register).
X Backspace (delete left of cursor and copy to register).
dd Delete line (and copy to register).
:j! Join line (remove newline at end of current line).
Ctrl-J Same.
u Undo.
Ctrl-R Redo.
de Delete to word end (and copy to register).
db Delete to word start (and copy to register).
d$ Delete to line end (and copy to register).
d^ Delete to line beginning (and copy to register).
dd Delete current line (and copy to register).
2dd Delete two lines (and copy to register).
5dd Delete five lines (and copy to register).
p Paste clipboard (insert register).
Ctrl-G Show cursor position.
5G Cursor to line five.
16G Cursor to line sixteen.
G Cursor to last line.
/search-string Search forwards for search-string.
?search-string Search backwards for search-string.
:-1,$s/search-string /replace-string /gc Search and replace with confirmation starting at current line.
:,$s/search-string /replace-string /gc Search and replace with confirmation starting at line below cursor.
:,$s/\<search-string \>/replace-string /gc Search and replace whole words.
:8,22s/search-string /replace-string /g Search and replace in lines 8 through 22 without confirmation.
:%s/search-string /replace-string /g Search and replace whole file without confirmation.
:w filename Save to file filename.
:5,20w filename Save lines 5 through 20 to file filename (use Ctrl-G to get line numbers if needed).
:5,$w! filename Force save lines 5 through to last line to file filename.
:r filename Insert file filename.
v Visual mode (start highlighting).
y Copy highlighted text to register.
d Delete highlighted text (and copy to register).
p Paste clipboard (insert register).
Press v, then move cursor Search and replace within
down a few lines, then, highlighted text.
:s/search-string /replace-string /g  
:help Reference manual (open new window with help screen inside--probably the most important command here!).
:new Open new blank window.
:split filename Open new window with filename.
:q Close current window.
:qa Close all windows.
Ctrl-W j Move cursor to window below.
Ctrl-W k Move cursor to window above.
Ctrl-W - Make window smaller.
Ctrl-W + Make window larger.

6.2 Syntax Highlighting

Something all UNIX users are used to (and have come to expect) is syntax highlighting. This basically means that a bash (explained later) script will look like:

  \epsfbox{with-syntax.ps}    instead of    \epsfbox{without-syntax.ps}

Syntax highlighting is meant to preempt programming errors by colorizing correct keywords. You can set syntax highlighting in vim by using :syntax on (but not in vi). Enable syntax highlighting whenever possible--all good text editors support it.

6.3 Editors

Although UNIX has had full graphics capability for a long time now, most administration of low-level services still takes place inside text configuration files. Word processing is also best accomplished with typesetting systems that require creation of ordinary text files. [This is in spite of all the hype regarding the WYSIWYG (what you see is what you get) word processor. This document itself was typeset with LATEX and the Cooledit text editor.]

Historically, the standard text editor used to be ed. ed allows the user to see only one line of text of a file at a time (primitive by today's standards). Today, ed is mostly used in its streaming version, sed. ed has long since been superseded by vi.

The editor is the place you will probably spend most of your time. Whether you are doing word processing, creating web pages, programming, or administrating. It is your primary interactive application.

6.3.1 Cooledit

(Read this if you ``just-want-to-open-a-file-and-start-typing-like-under-Windows.'')

cooledit The best editor for day-to-day work is Cooledit, [As Cooledit's author, I am probably biased in this view.] available from the Cooledit web page <http://cooledit.sourceforge.net/>. Cooledit is a graphical (runs under X) editor. It is also a full-featured Integrated Development Environment (IDE) for whatever you may be doing. Those considering buying an IDE for development need look no further than installing Cooledit for free.

People coming from a Windows background will find Cooledit the easiest and most powerful editor to use. It requires no tutelage; just enter cooledit under X and start typing. Its counterpart in text mode is mcedit, which comes with the GNU Midnight Commander package mc. The text-mode version is inferior to other text mode editors like emacs and jed but is adequate if you don't spend a lot of time in text mode.

Cooledit has pull-down menus and intuitive keys. It is not necessary to read any documentation before using Cooledit.

6.3.2 vi and vim

Today vi is considered the standard. It is the only editor that will be installed by default on any UNIX system. vim is a ``Charityware'' version that (as usual) improves upon the original vi with a host of features. It is important to learn the basics of vi even if your day-to-day editor is not going to be vi. The reason is that every administrator is bound to one day have to edit a text file over some really slow network link and vi is the best for this.

On the other hand, new users will probably find vi unintuitive and tedious and will spend a lot of time learning and remembering how to do all the things they need to. I myself cringe at the thought of vi pundits recommending it to new UNIX users.

In defense of vi, it should be said that many people use it exclusively, and it is probably the only editor that really can do absolutely everything. It is also one of the few editors that has working versions and consistent behavior across all UNIX and non-UNIX systems. vim works on AmigaOS, AtariMiNT, BeOS, DOS, MacOS, OS/2, RiscOS, VMS, and Windows (95/98/NT4/NT5/2000) as well as all UNIX variants.

6.3.3 Emacs

Emacs stands for Editor MACroS. It is the monster of all editors and can do almost everything one could imagine that a single software package might. It has become a de facto standard alongside vi.

Emacs is more than just a text editor. It is a complete system of using a computer for development, communications, file management, and things you wouldn't even imagine there are programs for. There is even an X Window System version available which can browse the web.

6.3.4 Other editors

Other editors to watch out for are joe, jed, nedit, pico, nano, and many others that try to emulate the look and feel of well-known DOS, Windows, or Apple Mac development environments, or to bring better interfaces by using Gtk/Gnome or Qt/KDE. The list gets longer each time I look. In short, don't think that the text editors that your vendor has chosen to put on your CD are the best or only free ones out there. The same goes for other applications.


next up previous contents
Next: 7. Shell Scripting Up: rute Previous: 5. Regular Expressions   Contents