Tuesday, September 26, 2006 

VI (and Clone) Editor Reference Manual

VI (and Clone) Editor Reference Manual



1. Introduction

This is a brief, introductory reference for vi. It is supplemental to the VI Introductory Guide and your vendor documentation. The information here also applies to all the clones of vi, such as vim, elvis, and stevie.

Vi is actually a very powerful editor, and is organized in a logical fashion. It is different from other editors or word processors, which may make learning it just a little bit tricky for some people. If you approach it analytically and fearlessly, you should become proficient with it in a short period of time.

I highly recommend experimentation. For obvious reasons you should restrict early experimentation to test files.

Some non-display character keys and other keys have special functions. These keys will be shown by placing their name (such as RETURN) in square brackets, like this: [RETURN].

Unlike most non UNIX1 products, vi is case-sensitive. Be careful with the [CAPS LOCK] key (except in input mode). If things are acting strange, make sure [CAPS LOCK] is off. If you are unsure about this, a good test is to press the `j' key; if it moves the cursor down a line, [CAPS LOCK] is not on; if it joins the next line to the current line, [CAPS LOCK] is on.

Not all of the commands mentioned here are covered in the VI Introductory Guide. There are also far more commands available in vi than are covered here - for further information check your system documentation or get one of the books available on vi.

Special Keys
KeyCommand ModeText Mode
Arrow Keys cursor movement N/A
[CTRL] used with other keys for extra commands insert control characters in text
[ENTER] or [RETURN] down 1 line normal function
[ESC] N/A leave text mode
Space Bar move right 1 character normal function
[TAB] N/A normal function

When a command is shown as a combination of the `^' (caret) and another character, as in ^V, this means press the [CTRL] key first, and hold it down while you press and release the other key.

2. Command Mode vs Text Mode

Vi is always either in command mode or text (input) mode. In text mode any character key pressed is enetered into the file. Most other typewriter keys have their normal effect. On some systems to enter a [CTRL] key sequence, you may first have to enter ^V before the sequence you want.

In command mode nearly every key on the keyboard performs some command or modifies the next command. Some of these commands may be difficult to recover from, so be careful in command mode to enter only the commands you wish to enter.

The [RETURN] key is not needed in command mode except with commands that begin with a `:' (colon) and with the search comands.

Mode Change Commands
aappend text after cursor
iinsert text before cursor
oopen new line after current line & add text
Oopen new line before current line & add text
[ESC]leave text mode

3. Cursor Motion

Cursor motion should nominally include use of the cursor keys. If your terminal lacks or has problems with arrow keys in vi command mode, the `h', `j', `k', and `l' keys will perform the same functions, as noted below.

Cursor Motion Commands
hmove back 1 character
lmove forward 1 character
jmove down 1 line
kmove up 1 line

bback to beginning of word
eforward to end of word
wforward to beginning of next word
^go to first displayable character of line
0go to beginning of line
$go to end of line

Fforward 1 screen
Bbackward 1 screen
Ddown (forward) 1/2 screen
Uup (backward) 1/2 screen

4. Editing Commands

These are the commands that actually manipulate text. the commands listed here include those which delete, replace, search, cut, and paste text, as well as those used for saving text and abandoning an edit session. Finally, a few miscellaneous commands are included which don't fit into the other categories.

4.1. Delete (Cut) Commands

In some editors the delete commands may be called cut commands. The last item deleted is saved in a buffer and may be put (or pasted) elsewhere in the file as noted later.

Delete Commands
xdelete character
dwdelete rest of word
d$delete rest of line
Ddelete rest of line
dddelete line

4.2. Replace, Change and Substitute Commands

Some editors provide commands to substitute new text occurances of search strings; this is not the same thing. These following vi commands allows you to replace some number of characters, words, lines or parts of lines with new text.

The change and substitute commands, the editor puts you into insert mode until you press [ESC]. The replace command replaces 1 (or more) characters with the next character you type.

Replace Commands
rreplace character
cwchange rest of word
c$change rest of line
Cchange rest of line
Nssubstitute text for N characters

4.3. Search Commands

These commands let you search for a text string, which may include regular expressions. They must be followed by a [RETURN].

Search Commands
/textsearch forward for text
?textsearch backward for text
nsearch in same direction for next occurance of last-searched-for text
Nsearch in other direction for next occurance of last-searched-for text

4.4. Undo Command

IN the real vi, the undo command only undoes the last command, even if that is an undo command. Repeated undo commands simply toggle the effect of the last command before the series of undo commands.

Some vi clones, such as vim, allow multiple undo's. These usually offer a way to set the undo level to the standard vi mode. See your editor's man page or reference manual for details.

Undo Command
uundo last command (BE CAREFUL WITH THIS)

4.5. Saving & Exiting Commands

These commands must be followed by a [RETURN].

Saving & Exiting Commands
:wwrite (save) the file
:w NAMEwrite the file and name it NAME
:w! NAMErewrite the file named NAME
:wqwrite the file and quit the editor
:qquit the editor
:q!quit the editor (abandoning any changes)

4.6. Search & Replace Command

To find & replace all occurances of a particular text string, use the command:
    :%s/text1/text2/g[RETURN]
which replaces all occurances of text1 with text2.

4.7. Yank (Copy) Commands

The vi yank command is similar to the copy command in many editors. It copies the text into a buffer. The text may be put (pasted) elsewhere in the file as described under the Put command.

Yank Commands
ywyank rest of word
y$yank rest of line
yyyank entire line
Yyank entire line

4.8. Put (Paste) Commands

The vi put command is similar to the paste command in many other editors. It will paste whatever is in the buffer from either the previous delete or yank command.

Put Commands
pput yanked/deleted text before cursor
Pput yanked/deleted text after cursor

If you yanked/deleted a whole line (or group of lines), p and P paste the text before or after the current line, respectively; otherwise, they paste the text before or after the cursor, respectively, on the current line.

4.9. Miscellaneous Commands

Miscellaneous Commands
.repeat last command (BE CAREFUL WITH THIS)
Jjoin next line to end of this line

NOTE

The period, or dot, command repeats most commands, but not quite all. It should work with all of the commands listed in this document; if you aren't sure of what you're doing, save your work before you try something.

5. Miscellaneous Notes

Most commands (other than those beginning with a colon (`:') may be preceded by a repeat count. For example,
    3dd
would delete 3 lines, starting with the current one.

Vi will not let you delete more lines than are in the file, so the above example would not work on the last line; vi would beep at you instead.


Notes:
  1. UNIX is a trademark of Western Electric, AT&T, SCO, or whoever bought it this week.

Copyright 1988, 1989, 1991, 1993, 1994, & 1998 by Susan Liebeskind (Atlanta, GA) and Miles O'Neal (Austin, TX). All rights reserved. Permission is hereby granted to redistribute this in either source or formatted form, so long as this copyright & the author's names are included, unmodified in content, and so long as no charge beyond reasonable cost of reproduction is charged. Notwithstanding, inclusion in any other work or collection which is sold, rented, or otherwise charged for, is prohibited without express consent of the authors.

 

VI (and Clone) Editor Reference Manual



1. Introduction

This is a brief, introductory reference for vi. It is supplemental to the VI Introductory Guide and your vendor documentation. The information here also applies to all the clones of vi, such as vim, elvis, and stevie.

Vi is actually a very powerful editor, and is organized in a logical fashion. It is different from other editors or word processors, which may make learning it just a little bit tricky for some people. If you approach it analytically and fearlessly, you should become proficient with it in a short period of time.

I highly recommend experimentation. For obvious reasons you should restrict early experimentation to test files.

Some non-display character keys and other keys have special functions. These keys will be shown by placing their name (such as RETURN) in square brackets, like this: [RETURN].

Unlike most non UNIX1 products, vi is case-sensitive. Be careful with the [CAPS LOCK] key (except in input mode). If things are acting strange, make sure [CAPS LOCK] is off. If you are unsure about this, a good test is to press the `j' key; if it moves the cursor down a line, [CAPS LOCK] is not on; if it joins the next line to the current line, [CAPS LOCK] is on.

Not all of the commands mentioned here are covered in the VI Introductory Guide. There are also far more commands available in vi than are covered here - for further information check your system documentation or get one of the books available on vi.

Special Keys
KeyCommand ModeText Mode
Arrow Keys cursor movement N/A
[CTRL] used with other keys for extra commands insert control characters in text
[ENTER] or [RETURN] down 1 line normal function
[ESC] N/A leave text mode
Space Bar move right 1 character normal function
[TAB] N/A normal function

When a command is shown as a combination of the `^' (caret) and another character, as in ^V, this means press the [CTRL] key first, and hold it down while you press and release the other key.

2. Command Mode vs Text Mode

Vi is always either in command mode or text (input) mode. In text mode any character key pressed is enetered into the file. Most other typewriter keys have their normal effect. On some systems to enter a [CTRL] key sequence, you may first have to enter ^V before the sequence you want.

In command mode nearly every key on the keyboard performs some command or modifies the next command. Some of these commands may be difficult to recover from, so be careful in command mode to enter only the commands you wish to enter.

The [RETURN] key is not needed in command mode except with commands that begin with a `:' (colon) and with the search comands.

Mode Change Commands
aappend text after cursor
iinsert text before cursor
oopen new line after current line & add text
Oopen new line before current line & add text
[ESC]leave text mode

3. Cursor Motion

Cursor motion should nominally include use of the cursor keys. If your terminal lacks or has problems with arrow keys in vi command mode, the `h', `j', `k', and `l' keys will perform the same functions, as noted below.

Cursor Motion Commands
hmove back 1 character
lmove forward 1 character
jmove down 1 line
kmove up 1 line
bback to beginning of word
eforward to end of word
wforward to beginning of next word
^go to first displayable character of line
0go to beginning of line
$go to end of line
Fforward 1 screen
Bbackward 1 screen
Ddown (forward) 1/2 screen
Uup (backward) 1/2 screen

4. Editing Commands

These are the commands that actually manipulate text. the commands listed here include those which delete, replace, search, cut, and paste text, as well as those used for saving text and abandoning an edit session. Finally, a few miscellaneous commands are included which don't fit into the other categories.

4.1. Delete (Cut) Commands

In some editors the delete commands may be called cut commands. The last item deleted is saved in a buffer and may be put (or pasted) elsewhere in the file as noted later.

Delete Commands
xdelete character
dwdelete rest of word
d$delete rest of line
Ddelete rest of line
dddelete line

4.2. Replace, Change and Substitute Commands

Some editors provide commands to substitute new text occurances of search strings; this is not the same thing. These following vi commands allows you to replace some number of characters, words, lines or parts of lines with new text.

The change and substitute commands, the editor puts you into insert mode until you press [ESC]. The replace command replaces 1 (or more) characters with the next character you type.

Replace Commands
rreplace character
cwchange rest of word
c$change rest of line
Cchange rest of line
Nssubstitute text for N characters

4.3. Search Commands

These commands let you search for a text string, which may include regular expressions. They must be followed by a [RETURN].

Search Commands
/textsearch forward for text
?textsearch backward for text
nsearch in same direction for next occurance of last-searched-for text
Nsearch in other direction for next occurance of last-searched-for text

4.4. Undo Command

IN the real vi, the undo command only undoes the last command, even if that is an undo command. Repeated undo commands simply toggle the effect of the last command before the series of undo commands.

Some vi clones, such as vim, allow multiple undo's. These usually offer a way to set the undo level to the standard vi mode. See your editor's man page or reference manual for details.

Undo Command
uundo last command (BE CAREFUL WITH THIS)

4.5. Saving & Exiting Commands

These commands must be followed by a [RETURN].

Saving & Exiting Commands
:wwrite (save) the file
:w NAMEwrite the file and name it NAME
:w! NAMErewrite the file named NAME
:wqwrite the file and quit the editor
:qquit the editor
:q!quit the editor (abandoning any changes)

4.6. Search & Replace Command

To find & replace all occurances of a particular text string, use the command:
    :%s/text1/text2/g[RETURN]
which replaces all occurances of text1 with text2.

4.7. Yank (Copy) Commands

The vi yank command is similar to the copy command in many editors. It copies the text into a buffer. The text may be put (pasted) elsewhere in the file as described under the Put command.

Yank Commands
ywyank rest of word
y$yank rest of line
yyyank entire line
Yyank entire line

4.8. Put (Paste) Commands

The vi put command is similar to the paste command in many other editors. It will paste whatever is in the buffer from either the previous delete or yank command.

Put Commands
pput yanked/deleted text before cursor
Pput yanked/deleted text after cursor

If you yanked/deleted a whole line (or group of lines), p and P paste the text before or after the current line, respectively; otherwise, they paste the text before or after the cursor, respectively, on the current line.

4.9. Miscellaneous Commands

Miscellaneous Commands
.repeat last command (BE CAREFUL WITH THIS)
Jjoin next line to end of this line

NOTE

The period, or dot, command repeats most commands, but not quite all. It should work with all of the commands listed in this document; if you aren't sure of what you're doing, save your work before you try something.

5. Miscellaneous Notes

Most commands (other than those beginning with a colon (`:') may be preceded by a repeat count. For example,
    3dd
would delete 3 lines, starting with the current one.

Vi will not let you delete more lines than are in the file, so the above example would not work on the last line; vi would beep at you instead.


Notes:
  1. UNIX is a trademark of Western Electric, AT&T, SCO, or whoever bought it this week.

Copyright 1988, 1989, 1991, 1993, 1994, & 1998 by Susan Liebeskind (Atlanta, GA) and Miles O'Neal (Austin, TX). All rights reserved. Permission is hereby granted to redistribute this in either source or formatted form, so long as this copyright & the author's names are included, unmodified in content, and so long as no charge beyond reasonable cost of reproduction is charged. Notwithstanding, inclusion in any other work or collection which is sold, rented, or otherwise charged for, is prohibited without express consent of the authors.

Friday, September 15, 2006 

Booting Linux into Rescue Mode

Booting into Rescue Mode

Rescue mode provides the ability to boot a small Red Hat Enterprise Linux environment entirely from CD-ROM, or some other boot method, instead of the system's hard drive.

As the name implies, rescue mode is provided to rescue you from something. During normal operation, your Red Hat Enterprise Linux system uses files located on your system's hard drive to do everything — run programs, store your files, and more.

However, there may be times when you are unable to get Red Hat Enterprise Linux running completely enough to access files on your system's hard drive. Using rescue mode, you can access the files stored on your system's hard drive, even if you cannot actually run Red Hat Enterprise Linux from that hard drive.

To boot into rescue mode, you must be able to boot the system using one of the following methods[1]:

  • By booting the system from an installation boot CD-ROM.

  • By booting the system from other installation boot media, such as USB flash devices.

  • By booting the system from the Red Hat Enterprise Linux CD-ROM #1.

Once you have booted using one of the described methods, add the keyword rescue as a kernel parameter. For example, for an x86 system, type the following command at the installation boot prompt:

linux rescue

You are prompted to answer a few basic questions, including which language to use. It also prompts you to select where a valid rescue image is located. Select from Local CD-ROM, Hard Drive, NFS image, FTP, or HTTP. The location selected must contain a valid installation tree, and the installation tree must be for the same version of Red Hat Enterprise Linux as the Red Hat Enterprise Linux CD-ROM #1 from which you booted. If you used a boot CD-ROM or other media to start rescue mode, the installation tree must be from the same tree from which the media was created. For more information about how to setup an installation tree on a hard drive, NFS server, FTP server, or HTTP server, refer to the Red Hat Enterprise Linux Installation Guide.

If you select a rescue image that does not require a network connection, you are asked whether or not you want to establish a network connection. A network connection is useful if you need to backup files to a different computer or install some RPM packages from a shared network location, for example.

The following message is displayed:

The rescue environment will now attempt to find
your Linux installation and mount it under the
directory /mnt/sysimage. You can then make any
changes required to your system. If you want
to proceed with this step choose 'Continue'.
You can also choose to mount your file systems
read-only instead of read-write by choosing
'Read-only'.

If for some reason this process fails you can
choose 'Skip' and this step will be skipped and
you will go directly to a command shell.

If you select Continue, it attempts to mount your file system under the directory /mnt/sysimage/. If it fails to mount a partition, it notifies you. If you select Read-Only, it attempts to mount your file system under the directory /mnt/sysimage/, but in read-only mode. If you select Skip, your file system is not mounted. Choose Skip if you think your file system is corrupted.

Once you have your system in rescue mode, a prompt appears on VC (virtual console) 1 and VC 2 (use the [Ctrl]-[Alt]-[F1] key combination to access VC 1 and [Ctrl]-[Alt]-[F2] to access VC 2):

sh-3.00b#

If you selected Continue to mount your partitions automatically and they were mounted successfully, you are in single-user mode.

Even if your file system is mounted, the default root partition while in rescue mode is a temporary root partition, not the root partition of the file system used during normal user mode (runlevel 3 or 5). If you selected to mount your file system and it mounted successfully, you can change the root partition of the rescue mode environment to the root partition of your file system by executing the following command:

chroot /mnt/sysimage

This is useful if you need to run commands such as rpm that require your root partition to be mounted as /. To exit the chroot environment, type exit to return to the prompt.

If you selected Skip, you can still try to mount a partition or LVM2 logical volume manually inside rescue mode by creating a directory such as /foo, and typing the following command:

mount -t ext3 /dev/mapper/VolGroup00-LogVol02 /foo

In the above command, /foo is a directory that you have created and /dev/mapper/VolGroup00-LogVol02 is the LVM2 logical volume you want to mount. If the partition is of type ext2, replace ext3 with ext2.

If you do not know the names of all physical partitions, use the following command to list them:

fdisk -l

If you do not know the names of all LVM2 physical volumes, volume groups, or logical volumes, use the following commands to list them:

pvdisplay
vgdisplay
lvdisplay

From the prompt, you can run many useful commands, such as:

  • ssh, scp, and ping if the network is started

  • dump and restore for users with tape drives

  • parted and fdisk for managing partitions

  • rpm for installing or upgrading software

  • joe for editing configuration files

Add to Google

The Author

  • Nick Perrydoo
  • Spawn at Philippines
My profile

Links


Read Ons

Article of the Day

This Day in History

Today's Birthday

In the News

Quotation of the Day

Word of the Day


Powered by Blogger
and Blogger Templates
© Copyright 2006 Ba-zoo-ra - All Rights Reserved.