quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif UNIX Editors

A text editor is a program which enables you to create and manipulate character data (text) in a computer file. A text editor is not a word processor although some text editors do include word processing facilities. Text editors often require "memorizing" commands in order to perform editing tasks. The more you use them, the easier it becomes. There is a "learning curve" in most cases though. There are several standard text editors available on most UNIX systems:

   

space.gif

  • ed - standard line editor
  • ex - extended line editor
  • vi - a visual editor; full screen; uses ed/ex line-mode commands for global file editing
  • sed - stream editor for batch processing of files

In addition to these, other local "favorites" may be available:

   

space.gif

  • emacs - a full screen editor and much more
  • pico - an easy "beginner's" editor
  • lots of others
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Standard Editor "vi"

vi supplies commands for:

   

space.gif

  • inserting and deleting text
  • replacing text
  • moving around the file
  • finding and substituting strings
  • cutting and pasting text
  • reading and writing to other files

vi uses a "buffer"

   

space.gif

  • While using vi to edit an existing file, you are actually working on a copy of the file that is held in a temporary buffer in your computer's memory.
  • If you invoked vi with a new filename, (or no file name) the contents of the file only exist in this buffer.
  • Saving a file writes the contents of this buffer to a disk file, replacing its contents. You can write the buffer to a new file or to some other file.
  • You can also decide not to write the contents of the buffer, and leave your original file unchanged.

vi operates in two different "modes":

   

space.gif

Command mode : vi starts up in this mode, Whatever you type is interpreted as a command - not text to be inserted into the file. The mode you need to be in if you want to "move around" the file.

   

space.gif

Insert mode : This is the mode you use to type (insert) text. There are several commands that you can use to enter this mode. Once in this mode, whatever you type is interpreted as text to be included in the file. You can not "move around" the file in this mode. Must press the ESC (escape) key to exit this mode and return to command mode.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif vi Commands
   

space.gif

  ../images/main/bullet_star_pink.gif Entering vi

vi filename - The filename can be the name of an existing file or the name of the file you want to create.

view filename - Starts vi in "read only" mode. Allows you to look at a file without the risk of altering its contents.

   

space.gif

  ../images/main/bullet_star_pink.gif Exiting vi

:q - quit - if you have made any changes, vi will warn you of this, and you'll need to use one of the other quits.

:w - write edit buffer to disk

:w filename - write edit buffer to disk as filename

:wq - write edit buffer to disk and quit

ZZ - write edit buffer to disk and quit

:q! - quit without writing edit buffer to disk

   

space.gif

  ../images/main/bullet_star_pink.gif Positioning within text
   

space.gif

By character

Key Stroke

Operation

left arrow

left one character

right arrow

right one character

backspace

left one character

space

right one character

h

left one character

l

right one character

   

space.gif

By word

Key Stroke

Operation

w

beginning of next word

nw

beginning of nth next word

b

back to previous word

nb

back to nth previous word

e

end of next word

ne

end of nth next word

   

space.gif

By line

Key Stroke

Operation

down arrow

down one line

up arrow

up one line

j

down one line

k

up one line

+

beginning of next line down

-

beginning of previous line up

0

first column of current line (zero)

^

first character of current line

$

last character of current line

   

space.gif

By block

Key Stroke

Operation

(

beginning of sentence

)

end of sentence

{

beginning of paragraph

}

end of paragraph

   

space.gif

By screen

Key Stroke

Operation

CTRL-f

forward 1 screen

CTRL-b

backward 1 screen

CTRL-d

down 1/2 screen

CTRL-u

up 1/2 screen

H

top line on screen

M

mid-screen

L

last line on screen

   

space.gif

Within file

Key Stroke

Operation

nG

line n within file

1G

first line in file

G

last line in file

   

space.gif

  ../images/main/bullet_star_pink.gif Inserting text
   

space.gif

Key Strok

Operation

a

append text after cursor *

A

append text at end of line *

i

insert text before cursor *

I

insert text at beginning of line *

o

open a blank line after the current line for text input *

O

open a blank line before the current line for text input *

Note: hit ESC (escape) key when finished inserting!

   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Deleting text
   

space.gif

Key Stroke

Operation

x

delete character at cursor

dh

delete character before cursor

nx

delete n characters at cursor

dw

delete next word

db

delete previous word

dnw

delete n words from cursor

dnb

delete n words before cursor

d0

delete to beginning of line

d$

delete to end of line

D

delete to end of line

dd

delete current line

d(

delete to beginning of sentence

d)

delete to end of sentence

d{

delete to beginning of paragraph

d}

delete to end of paragraph

ndd

delete n lines (start at current line)

   

space.gif

  ../images/main/bullet_star_pink.gif Changing text
   

space.gif

Key Strok

Operation

cw

replace word with text *

cc

replace line with text *

c0

change to beginning of line *

c$

change to end of line *

C

change to end of line *

c(

change to beginning of sentence *

c)

change to end of sentence *

c{

change to beginning of paragraph *

c}

change to end of paragraph *

r

overtype only 1 character

R

overtype text until ESC is hit *

J

join two lines

Note: hit ESC (escape) key when finished changing!

   

space.gif

  ../images/main/bullet_star_pink.gif Copying lines
   

space.gif

Key Stroke

Operation

yy

"yank" copy 1 line into buffer

nyy

"yank" copy n lines into buffer

p

put contents of buffer after current line

P

put contents of buffer before current line

   

space.gif

  ../images/main/bullet_star_pink.gif Moving lines (cutting and pasting)
   

space.gif

Key Stroke

Operation

ndd

delete n lines (placed in buffer)

p

put contents of buffer after current line

P

put contents of buffer before current line

   

space.gif

  ../images/main/bullet_star_pink.gif Searching / Substituting
   

space.gif

Key Stroke

Operation

/str

search forward for str

?str

search backward for str

n

find next occurrence of current string

N

repeat previous search in reverse direction

   

space.gif

The substitution command requires a line range specification. If it is omitted, the default is the current line only. The examples below show how to specify line ranges.

   

space.gif

command

Description

s/old/new

substitute new for first occurrence of old in current line

s/old/new/g

substitute new for all occurrences of old in current line

1,10s/old/new

substitute new for first occurrence of old in lines 1 - 10

.,$s/old/new

substitute new for first occurrence of old in remainder of file

.,+5s/old/new

substitute new for first occurrence of old in current line and next 5 lines

.,-5s/old/new

substitute new for first occurrence of old in current line and previous 5 lines

%s/old/new/g

substitute new for all occurrences of old in the entire file

%s/old/new/gc

%s/\r//g

   

space.gif

  ../images/main/bullet_star_pink.gif Miscellaneous commands
  • u - undo the last command (including undo)
  • . - repeat last command
  • xp - swap two adjacent characters
  • m[a-z] - set a marker (a - z)
  • '[a-z] - go to a previously set marker (a - z)
  • :!command - execute specified UNIX command
  • :r filename - read/insert contents of filename after current line.
  • :1,100!fmt - reformat the first 100 lines
  • :!fmt - reformat the entire file
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Setting vi Options

You can change the way vi operates by changing the value of certain options which control specific parts of the vi environment.

   

space.gif

To set an option during a vi session, use one of the commands below as required by the option:

   

space.gif

:set option_name

:set option_name=value

   

space.gif

Some examples of the more common options are described below.

   

space.gif

  • :set all - shows all vi options in effect
  • :set ai - set autoindent - automatically indents each line of text
  • :set noai - turn autoindent off
  • :set nu - set line numbering on
  • :set nonu - turn line numbering off
  • :set scroll=n - sets number of lines to be scrolled to n. Used by screen scroll commands.
  • :set sw=n - set shiftwidth to n. Used by autoindent option.
  • :set wm=n - set wrapmargin to n. Specifies number of spaces to leave on right edge of the screen before wrapping words to next line.
  • :set showmode - reminds you when you are inserting text.
  • :set ic - ignore case of characters when performing a search.

Options can be set permanently by putting them in a file called .exrc in your home directory. A sample .exrc file appears below. Note that you do not need the colon (:) as part of the option specification when you put the commands in a .exrc file. Also note that you can put them all on one line.

   

space.gif

set nu ai wm=5 showmode ic

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Advanced VI

Vi is powerfull editor, there lot options and ways it can be used. Below is few of this advanced methods.

   

space.gif

  • Ranges
  • Marking
  • Registers
  • Vim scripting
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com