#!/bin/bash 
############################################################################################################################
# Novell Inc.
# 1800 South Novell Place
# Provo, UT 84606-6194
# Script Name:		vic.sh
# Description:		Display vi commands  
#  			                              
# %Version:		1.0
# %Creating Date:	Wednesday Feb 20 09:9:46 MST 2013
# %Created by: 		Trevor Sanford and Rance Burker - Novell Technical Services
############################################################################################################################
# Display ASCII art
clear
echo ' ___   ___   __ __      __  ___            __                     '
echo '|   \ / __| / _|\ \    / / / _ \ __ __ ___/ /___    __  ___  __ __ '
echo '| |) |\__ \|  _| \ \/\/ / / // // // // _  // -_)_ / _|/ _ \|     |'
echo '|___/ |___/|_|    \_/\_/ /____/ \_,_/ \_,_/ \__/(_)\__|\___/|_|_|_|'
echo '                                                                   '
sleep .5


menu(){
if ["$1" = ""]
then

clear
#echo $0
echo
echo "   VI Commands "
echo
echo "   1. Input text"
echo "   2. Move marker"
echo "   3. Search text"
echo "   4. Delete text"
echo "   5. Change text"
echo "   6. Copy and move text"
echo "   7. Search and replace text"
echo "   8. Read file"
echo "   9. Save/quit"
echo "  10. Misc"
echo "  11. Advanced commands"
echo
echo "   Q or Press [Enter] to Exit"
echo
echo -n "  Enter an option: "
read IN
else
IN="$1"
fi
case $IN in
1) echo "
  Input text:
	a   Input after marker position
	A   Input after row end
	i   Input before marker position
	I   Input at start of row
	o   Open new row below
	O   Open new row above"
;;
2) echo "
   Move marker:  
Except for the "marker mover keys", that is often used, there are many special commands. 
Many of them can be preceded with an integer that repeats the command (examples below).

	h   Left one step (or left arrow)
	l   Right one step (or right arrow)
	j   Down one row (or down arrow)
	k   Up one row (or up arrow)

	w   Beginning of next (english) word, 5w = five words ahead
	W   Beginning of next word
	b   One (english) word backwards, 3b = three words back
	B   One word backwards
	e   End of english word
	E   End of word

	0   Beginning of row
	$   End of row
	^   First none blank character
	Ret First none blank character on next row, 6Ret = six rows
	+   Same as Ret, 10+ = ten rows
	-   First none blank character on row above
	9|  Column 9 on current row

	(   Beginning of current sentence
	)   Beginning of next sentence
	{   Beginning of current paragraph
	}   Beginning of next paragraph
 
	[[  Beginning of current chapter (preceded with { or beginning of file)
	]]  Beginning of next chapter, see above useful when programming

	H   First row in screen
	L   Last row in screen
	M   Middle of screen
	G   Last row in file, 7G = seventh row

	:12 To row number 12
	:+5 Five rows ahead

	Ctrl-D Half page ahead
	Ctrl-U Half page back
	Ctrl-F One page ahead
	Ctrl-B One page back"
;;
3) echo "
  Search text:
	/text Search ahead for text
	?text Search backwards for text

	n   Repeat last search (next)
	N   Search in opposite direction

	Note: If a special character exists in the search string (/ or ?), you need to put a '\' in front of it.
 
	fx  Ahead to next x in current row
	Fx  Backwards to first x in current row
	tx  To char before x in current row
	Tx  Backwards to char after x in current row
	;   Repeat last f-,F-,t- or T- command
	,   Repeat last f-,F-,t- or T- in opposite direction

	mx  Mark current row with char x
	'mx To row marked with char x"
;;
4) echo -e "
   Delete text:  
	x   Delete char under marker
	4x  Delete four chars
	X   Delete char before marker
	dw  Delete english word from marker to next word
	3dW Delete three words
	dW  Delete word from marker to next word
	db  Delete from marker to start of english word
	dB  Delete from marker to beginning of word
	de  Delete from marker to english word end
	dE  Delete from marker to word end
	d$  Delete from marker to end of row
	D   Delete from marker to end of row
	d0  Delete from marker to start of row
	dd  Delete row
	5dd Delete five rows
	d(  Delete beginning of sentence
	d)  Delete rest of sentence
	d}  Delete rest of paragraph
	dG  Delete rest of file from marker position
	d1G Delete from beginning of file to marker
	dtx Delete to next appearance on char x
	:4,12d Delete rows 4-12 (colon command)
	:.,+3d Delete current row and three ahead
	\"a3ss Delete three rows and place contents in buffer a
  
	During input mode Backspace or Ctrl-H deletes a character and Ctrl-W a word."
;;
5) echo -e "
   Change text:  
	rx    Replace char under marker with x
	stext Replace character with text, Escquits

	R   Write over old text, Escquits
	cW  Replace word under marker
	cc  Replace current row
	S   Replace current row
	c$  Replace rest of current row
	C   Replace rest of current row

	J   Join two rows

	u   Undo last change
	U   Undo all changes in current row"
;;
6) echo -e "
  Copy and move text:  
	yw  Yank english word to buffer
	yW  Yank word to buffer
	2yw Yank 2 english word to buffer
	3yW Yank 3 word to buffer

	yy  Copy row to buffer
	Y   Copy row to buffer
	y$  Copy rest of row to buffer
	y}  Copy rest of paragraph to buffer
	7yy Copy seven rows to buffer

	"\""xY  Copy row to buffer x
	"\""xdd Delete row and place in buffer x

	p   Place buffer contents after marker
	P   Place buffer contents before marker
	"\""xp Place contents of buffer x after marker

	:m 9    Move current row after row number 9
	:1,7m 9 Move rows 1-7 after row number 9"
;;
7) echo -e "
  Search and replace text: 
	Syntax:
	: [address] s/old/new/ [options]

	Examples:
	:s/old/new/      Substitute old with new
	:1,9s/old/new/   Substitute first appearance of old with new on row 1-9
	:1,9s/old/new/g  Substitute all old with new on rows 1-9
	:.,+5s/old/new/g Substitute old with new on current row and 5 rows ahead
	:.,-6s/old/new/g Substitute old with new on current row and 6 rows back
	:.,$s/old/new/g    Substitute old with new from current row to end of file
	:1,$s/old/new/g    Substitute old with new in whole file
	:%s/old/new/g    Substitute old with new in whole file
	:%s/old/new/gc   Ask before substituting old with new
	:%s/old/new/gci  Ask before substituting old with new and ignore case
	:%s/old/new/gcI  Ask before substituting old with new and case sensitive
 
	Note: Without g above, only first appearance will be substituted

	To match patterns when searching here are some useful special characters
	^   Beginning of row
	$   End of row
	.   Any character
	[]  Interval of characters
	*   Any pattern
	\<  Beginning of word
	\>  End of word

	In searchstrings the following char is useful
	&   Replace with whole string of matching characters
	(useful when adding to search string)"
;;
8) echo "
  Read file:  
	:e  file Read file for editing
	:r  file Append file at marker position"
;;
9) echo "
  Save/quit:
	ZZ  Save and quit Vi
	:wq Save and quit Vi
	:w  Save (don't quit)
	:w! Save, write over existing file (don't quit)

	:w file       Save in file
	:5,9w file    Save rows 5-9 in file
	:5,9w >> file Append rows 5-9 to file

	:q! Quit without saving"
;;
10) echo "
  Misc Commands:
	.   Repeat last edit command
	~   Replace small char to big, and reverse
	%   Find brackets: (), {} or []
	:cd path Change directory to path
	:= Show current rownumber
        :%s/pattern/gn Count occurrences of a pattern
        :%s/[^ ]\+//gn Count space delimited words
	Ctrl-G Show rownumber, number of rows and distance to start of file in %"
;;
11) echo "
  Advanced commands:
	Tab stops, line wrapping, automatic indenting etc. can be turned on and off with set-commands:

	:set option [=value]
	:set nooption Turn off option
  
	Examples:
	:set all  Show list with current values
	:set nu   Show rownumber for all rows
	:set nonu Dont show rownumbers
	:set ic   Ignore case distinctions when searching
	:set noic Dont ignore case distinctions when searching

	:set autoindent   Indent automaticly (Inc. = Ctrl-T, dec. = Ctrl-D
	:set autoindent   Indent automaticly (Inc. = Ctrl-T, dec. = Ctrl-D
	:set shiftwidth=3 Set indent size to 3 steps (default is 8)

	:!command   Escape to shell and run command
	:r !command Append output from command after marker"
#	pause 'Press [Enter] key to continue ...'; main
;;
*) exit
;;
esac

echo
}

# pause 'Press [Enter] key to finish trace ...'
pause(){
   read -p "$*"
}

main(){
menu; pause '  Press [Enter] key to continue ...'; main
}

main
