URL.biz - where people find experts

 
HOME ARTICLES LIST NOW FOR FREE! ABOUT US CONTACT US LOG IN

Unix Fundamentals Module #7 Unix Editors
7th in series of Unix Fundamentals modules from JayrConsulting Ltd. A FREE copy of this module in MsWord format (including missing diagrams) can be got from john.roberts@jayrconsulting.co.uk

Go to Web Site

UNIX Fundamentals

MODULE 7 – UNIX Screen Editor



You will cover

* Opening Editing Sessions

* Editor Modes

* Saving and Quitting

þ Editor Configuration

þ Command Stacker

































































































MODULE 7 – CONTENTS

7 UNIX EDITING 4

7.1 OPENING AN EDITING SESSION 4

7.2 EDITOR MODES 5

7.2.1 COMMAND MODE 5

Cursor Positioning 5

Screen Control 5

Text alterations 5

7.2.2 TEXT MODES 6

7.2.3 EX MODE 6

7.3 SAVING AND QUITTING 8

7.4 EDITOR CONFIGURATION 8

7.5 COMMAND STACKER 9































7 UNIX Editing



UNIX systems normally support many variants of editors, such as ed, sed, emacs, vi, etc. This module covers the ‘vi' variant, but many of the commands and techniques are common to the other editors.



7.1 Opening an Editing Session



vi filename will open an editing session on filename.



Proper screen handling requires that the editor process knows the features of the terminal from which it has been called. This is achieved by use of a system variable called TERM. It can be defined prior to an editing session, as follows:



TERM=vt220;export TERM



Best results are obtained with an ‘ansi 7 bit' emulation, where possible.



























7.2 Editor Modes



7.2.1 Command Mode



Initially, vi places you in command mode. In this mode, you can move around the screen and carry out some basic editing actions.



Cursor Positioning



Normal cursor control keys(arrow keys) may be used to move the cursor around the screen. If these do not work properly, due to emulation problems, the ‘H', ‘J', ‘K' and ‘L' keys will provide left, down, up and right movements respectively.



Screen Control



nw move cursor forward ‘n' words

nb move cursor back ‘n' words

$ move cursor to end of line

^ move cursor to beginning of line

refresh screen

scroll forward one full screen

scroll backward one full screen



Text alterations



J join current line to next line

4j join current line and next 3

x delete character at cursor position

5x delete 5 characters, starting from cursor position

dd delete current line

yy yank current line and store in buffer

3yy yank 3 lines and store in buffer

p place text lines from buffer after cursor

u undo last change

7.2.2 Text Modes



The current line and character are indicated by the position of the cursor on the screen. The following commands all switch ‘vi' into text mode.



a append after the cursor position

A append at end of line

i insert before cursor position

I insert at beginning of line

o open new line below current line

O open new line above current line

r replace single character at cursor position

R replaces characters from cursor position onwards



To exit Text Mode, returning to Command Mode, press [Escape]



7.2.3 Ex Mode



The following are entered from the Command Mode by entering a colon, followed by the appropriate command or symbols. You are returned to command mode after completion.



: enter ex mode, wait for input

:set show configuration parameters for this session

:set all show all available configuration parameters

:set parameter_name set named parameter on

:set noparameter_name set named parameter off

:sh break to shell prompt, return with

:! command run shell command and return

:r file_name copy in file_name at cursor position

:r !command read in output of command at cursor position

:e! re-edit, disregarding any changes

:r !sed –n x,yp file_name merge lines ‘x' to ‘y' of file_name at cursor position

:15p print line 15

:g/fred/p print all lines containing fred

:17,23d delete lines 17-23

:g/bill/d delete all lines containing bill

:g/^$/d delete all blank lines

:5,9m20 move lines 5-9, place after line 20

:19m0 move line 19 to top of file

:17t10 copies line 17, places copy after line 10

:4,5j joins line 5 to end of line 4

:u undoes last command entered

:s/fred/bill/ substitute first occurrence of fred with bill in current line

:4,20s/fred/bill/ substitute first occurrence of fred with bill on lines 4-20

:s/fred/bill/g substitute all occurrences of fred on the current line

:g/fred/s/fred/bill/g search file for all occurrences of fred and replace with bill









7.3 Saving and Quitting



The following all assume starting from the command mode.



ZZ write file away and quit

:w write file away without quitting

:wq write file away and quit

:q! quit without writing changes to file

:wq! write and quit, overriding file permissions where possible

:w filename write to another file, instead of original



7.4 Editor Configuration



The editor can be customised for personal use by creating a file in the user's home directory called ‘.exrc' . This file should contain a list of ‘set parameter' statements that are to be invoked each time the editor is used.



Example .exrc file



set number

set nomesg

set term=vt220









7.5 Command Stacker



There are command stackers available in UNIX, but not in the Bourne shell. For example, the stacker in the Korn shell can be used as follows.



ksh –o vi turns on stacker

K enters command list

H,J,K,L keys enables movement within command list



When you reach the command you want it can be altered by using normal editor commands and is then re-invoked with .


 
Other Articles Written By This User


Copyright © 2003 - 2012 URL.biz. All rights reserved.