Tech Musings

Wednesday, December 14, 2005

Pico vs. the vi editor

UPDATE! I have since discovered there is a difference between the vi editor and the Vim "Vi IMproved" editor. I was reproved on this point when I asked for help on the comp.unix.shell listserv. The braggadocio of UNIX folk has a certain charm to it, don't you think?


I admit I use pico for most text editing tasks in the terminal. UNIX jocks will laugh at me, but it's easier for me to remember how to use (as opposed to vi's esoteric syntax) pico because I don't engage in heavy terminal work on a daily basis. That said, I will use vi on occasion especially if I need to reference line numbers in a file, as I did on line 2642 in quiztest.cgi in order to add some white space between my generated quiz questions!! The other thing I like about vi is that it offers a level of protection from miscues and unwanted typos in your code due to an errant keystroke here and there. vi offers this safety because edits to the code can only occur after typing "I" and entering Insert mode.

Here is a decent vi article:
http://www.uic.edu/depts/accc/software/unixgeneral/vi101.html

You typically toggle between "Command" mode and "Insert" mode in vi. To insert text, press i (insert), then type your text. Use the backspace key to go back and then type over any mistakes.

Here are the vi commands I tend to use most frequently:

Esc exit insert mode; switch to command mode
:q quit vi
:q! quit vi without saving changes
u undo last change
. redo last command
:set number turn on line numbering
:w save current version of file
:w yyyy save current version of file, named yyyy
/aaaa search forward for pattern aaaa
?aaaa search backward for pattern aaaa
n repeat
k or up arrow move up one line
j or down arrow move down one line
i insert text before cursor position
x delete current character
dd delete current line
^f (Ctrl-f) move one page forward
^b (Ctrl-b) move one page back
^d (Ctrl-d) scroll down
^u (Ctrl-u) scroll up
^g (Ctrl-g) display current line number
:$ move to last line in file (or type capital G)
:# move to line number in file (e.g. :1 to move to top of file)

To find & replace all occurances of a particular text string, use the command:
:%s/text1/text2/g [RETURN ]
which replaces all occurances of text1 with text2. I use this one to clean up sitemap urls which are auto-generated using a Google python script. It makes it easy to replace those unwanted spaces (i.e. %20) which have a habit of finding their way into URLs referenced in Web access logs.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home