dkFlyer

vi, The Visual Editor


vi (pronounced vee-eye) is a full-screen text editor available on UNIX. vi lets you view and edit many lines of text at once, many files one right after another, and is one of the faster to use editors.

vi has a 'command' mode (for moving withing the file) and an 'insert' mode (to insert text into the file). pressing 'escape' puts you from insert mode into the command mode.

vi Commands

To enter vi:vi filename
Local Cursor Movement
Right One Characterl
Left One Characterh
Up One Linek
Down One Linej
Page Cursor Movement
Forward a PageCtrl-f
Back a PageCtrl-b
Forward a Half PageCtrl-d
Back a Half PageCtrl-u
Screen Cursor Movement
Top LeftH
Bottom LeftL
Middle LineM
Line Cursor Movement
Left Margin0
End$
Next Line+
Prior Line-
n-th LinenG or :n
last LineG or :$
Show Current LineCtrl-G
Word Cursor Movement
Next Wordw
Five Words5w
Prior Wordb
End of Worde
Search
Search Forward/string
Search Backward?string
Repeat Last Searchn
Reverse Last SearchN
Substitute
Current Line:s/str1/str2/
All Occurrences:s/str1/str2/g
Range of Lines:l1,l2s/str1/str2/
Deletions
Character Under Cursorx
Character Before CursorX
Linedd
To End of LineD
To Start of Next Worddw
Text Mode
Append After Cursora
Insert Before Cursori
Append to End of LineA
Insert at Start of LineI
Open Next Lineo
Open Prior LineO
Text Replacement
Character Under Cursorr
Overwrite CharactersR...
Change Wordcw
Change Five Wordsc5w
Change to End of Linec$
Save / Exit
Save:w
Quit (no changes):q
Quit (changes):q!
Save & Quit:x or :wq or ZZ
Write to a File:w file
Overwrite to a File:w! file
Text Movement
Yank Lines[cnt]Y or [cnt]yy
Paste After Cursorp
Paste Before CursorP
Insert File:r file
Repeat Last Command. (the period)
Miscellaneous
Undo Changesu


Some Other Common Unix Commands

ls
lsafile bfile cfile dfile efile ffile runfile tmp
ls -F
with the '-F' switch
afile bfile cfile dfile efile ffile runfile* tmp/
(notice that 'runfile*' is a program (rwx) and 'tmp/' is a directory.)
ls -l
with the '-l' switch (long format)
total 8
-rw-rw-rw- 1 dkennedy devel 71 JJul 24 09:48 afile
-rw-rw-rw- 1 dkennedy devel 81 JJul 24 09:48 bfile
-rw-rw-rw- 1 dkennedy devel 0 JJul 24 09:49 cfile
-rw-rw-rw- 1 dkennedy devel 544 JJul 24 09:47 dfile
-rw-rw-rw- 1 dkennedy devel 126 JJul 24 09:45 efile
-rw-rw-rw- 1 dkennedy devel 132 JJul 24 09:45 ffile
-rwxrwxrwx 1 dkennedy devel 44 JJul 24 09:46 runfile
drwxrwxrwx 2 dkennedy devel 512 Jul 24 09:47 tmp
ls -laF
with the '-l' switch (long format)
the '-a' switch (hidden files)
and the '-F' switch
total 11
drwxrwxrwx 3 dkennedy devel 512 Jul 24 09:47 ./
drwxrwxr-x 3 dkennedy devel 1536 Jul 24 09:45 ../
-rw-rw-rw- 1 dkennedy devel 71 JJul 24 09:48 afile
-rw-rw-rw- 1 dkennedy devel 81 JJul 24 09:48 bfile
-rw-rw-rw- 1 dkennedy devel 495 JJul 24 09:49 cfile
-rw-rw-rw- 1 dkennedy devel 0 JJul 24 09:49 dfile
-rw-rw-rw- 1 dkennedy devel 126 JJul 24 09:45 efile
-rw-rw-rw- 1 dkennedy devel 132 JJul 24 09:45 ffile
-rwxrwxrwx 1 dkennedy devel 44 JJul 24 09:46 runfile*
drwxrwxrwx 2 dkennedy devel 512 Jul 24 09:47 tmp/
find
find /directory -name filename.ext -print will find the file 'filename.ext', starting from /directory and print them out
find /usr/directory -name "*.4gl" -exec grep -il $1 {} \; > $HOME/tmp/$1.txt will find any file that ends with ".4gl", starting from /usr/directory, then grep inside those files for the string passed on the command line ($1), saving any matches into the home test file.
grep
grep string filename.ext looks for the string 'string' in the file 'filename.ext'.
grep 'function st' *.c looks for any function that starts with 'st' in all files that end in '.c'.
sed
sed 's/old/new/g' < infile > outfile replaces all occurances of 'old' with 'new' from 'infile' and writes to 'outfile'.
sed -e '2,$d' \
    -e 's,[()],,g' \
    -e 's,\.$,,' \
    -e 's,..* /,/,' \
    -e 's, .*,,'
# 1) remove all but the first line
# 2) remove any parenthesis
# 3) remove trailing period
# 4) eliminate everthing up to the final word
# 5) eliminate everything after the first (was final) word
nawk
nawk '{
  print "echo " $0 (cont)
    " >> $HOME/tmp/in.txt"
  print "grep \"update \" " $0 (cont)
    " >> $HOME/tmp/in.txt"
}' $1
this nawk script is used with the find / -exec grep command above. once the list of found files is stored in the 'txt' file, this nawk script would then get all the occurance inside those files into 'in.txt'. (note that the print lines '(continued)' into 'in.txt' are actually one line of code.)
nawk '
  BEGIN {
    linecomp = ""
  }
  {
    counter($0)
  }
  END {
    counter($0)
  }
function counter(liner) {
  if (liner != linecomp) {
    printf ("%s\n", linecomp)
    linecomp = liner
  }
}' $1
this nawk script merely looks inside a sorted file for duplicate lines then prints out only one occurance. it uses a function, allowing the END portion of the script to call this function, too (insuring that you have checked the last line).
nawk '{
  invno = substr($0, 1, 16)
  lineno = substr($0, 17, 5)
  boxno1 = substr($0, 1, 16)
  sub(/ *$/, "", boxno1)
  boxno2 = substr($0, 22, 4)
  sub(/^ */, "", boxno2)
  newboxno = boxno1 "-" boxno2
  printf("%-16.16s", invno)
  printf("%5s", lineno)
  printf("%-19.19s\n", newboxno)
}' $1
this nawk script accepts a file, reformatting each line, then printing out the new format. the first sub removes all trailing spaces while the second sub removes all leading spaces. the '-16.16' formats the string as 16 spaces, right justified. only the last printf has the '\n' carriage return.


Author: Dennis Kennedy

Copyright © 1998 dkFlyer
If you have any questions, email me at [email protected]
Hosted by www.Geocities.ws

1