|
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.
| To enter vi: | vi filename |
| Local Cursor Movement | |
| Right One Character | l |
| Left One Character | h |
| Up One Line | k |
| Down One Line | j |
| Page Cursor Movement | |
| Forward a Page | Ctrl-f |
| Back a Page | Ctrl-b |
| Forward a Half Page | Ctrl-d |
| Back a Half Page | Ctrl-u |
| Screen Cursor Movement | |
| Top Left | H |
| Bottom Left | L |
| Middle Line | M |
| Line Cursor Movement | |
| Left Margin | 0 |
| End | $ |
| Next Line | + |
| Prior Line | - |
| n-th Line | nG or :n |
| last Line | G or :$ |
| Show Current Line | Ctrl-G |
| Word Cursor Movement | |
| Next Word | w |
| Five Words | 5w |
| Prior Word | b |
| End of Word | e |
| Search | |
| Search Forward | /string |
| Search Backward | ?string |
| Repeat Last Search | n |
| Reverse Last Search | N |
| Substitute | |
| Current Line | :s/str1/str2/ |
| All Occurrences | :s/str1/str2/g |
| Range of Lines | :l1,l2s/str1/str2/ |
| Deletions | |
| Character Under Cursor | x |
| Character Before Cursor | X |
| Line | dd |
| To End of Line | D |
| To Start of Next Word | dw |
| Text Mode | |
| Append After Cursor | a |
| Insert Before Cursor | i |
| Append to End of Line | A |
| Insert at Start of Line | I |
| Open Next Line | o |
| Open Prior Line | O |
| Text Replacement | |
| Character Under Cursor | r |
| Overwrite Characters | R... |
| Change Word | cw |
| Change Five Words | c5w |
| Change to End of Line | c$ |
| 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 Cursor | p |
| Paste Before Cursor | P |
| Insert File | :r file |
| Repeat Last Command | . (the period) |
| Miscellaneous | |
| Undo Changes | u |
| ls | |
| ls | afile 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