[CALUG] vi

Rick Radzville rradzville at hotmail.com
Tue Nov 22 07:57:52 CST 2005


Jim,

Thank you very much for such an awesome intro to vi. I'm approaching Linux 
from a sysadmin point of view, where the main question is always "What task 
that I need to do can I accomplish with this?" And your lesson illustrates 
that very effectively.

v/r,
Rick


>From: Jim Sansing <jjsansing at comcast.net>
>To: Rick Radzville <rradzville at hotmail.com>
>Subject: Re: [CALUG] Best programming app
>Date: Mon, 21 Nov 2005 19:19:20 -0500
>
>Rick,
>
>To answer your question, I am not talking about a database.  But the data
>could be a directory listing, an html file, or a C program.
>
>NOTE: In the following examples, type what is in the single quotes, but
>not the single quotes themselves.
>
>The key to learning vi is to understand the difference between command
>mode and
>input mode.  Input mode is what you are used to in most editors/word
>processors,
>you type and the characters are added to the working copy of the file.
>When you
>start vi, you are in command mode.  Type 'i' and you are in input mode.
>Start
>typing.  To get out of input mode, press ESC.
>
>Command mode allows you to operate on the characters indirectly.  Cut
>and paste
>is an example of this.  You select a block, copy it to the clipboard,
>and paste it to
>the destination.  In vi, copy is "y" for yank.  In order to be able to
>yank what you
>want, you need to know some navigation commands.  Note that current line
>is the
>line where the cursor is.  You have to learn vi by doing, so vi a file
>and move the
>cursor to the middle of a line and press '0', then press '$'.  You'll
>get the idea.
>Oh, and to get out without saving the changes, press ESC, then type
>':q!' and
>press Enter.  (Some people press Ctrl-Z, but that leaves the recovery
>file on
>Linux which is confusing the next time you edit that file.)
>
>0 / $ = Start / End of the current line
>( / ) = Beginning of / Next  sentence (identified by a period)
>{ / } = Beginning / End of a paragraph
>% = Corresponding paren, bracket, or brace
>
>So, now to yank from the cursor to the end of the line, type 'y$'.  Now
>use the
>arrow keys to move to a blank line and press 'p' for put.
>
>Now move to the beginning of a paragraph by typing '{'.  Type 'y}' and
>then 'P'.
>
>Now type '3y}' and then 'P'.  You have just cut and pasted 3 paragraphs.
>
>OK, so cut and paste isn't very exciting.  But you have to walk before
>you run.
>
>The next thing is substitute, or Find and Replace.  You have to work on
>the vi
>command line for this.  Press ':' and the cursor moves to the bottom of
>the screen.
>Type '1,$ s/a/@/g' and press Enter.  Every lowercase a in your file just
>changed
>to @.  Press 'u' to undo that.
>
>Now type ':1,11 s/^...//' and press Enter.  You have just deleted the
>1st 3 characters
>of the first 11 lines in your file.  (Again press 'u' to undo it).
>
>Now type ':1,$ s/^$/---/' and press Enter.  You have just turned every
>blank line
>into a line with 3 dashes.
>
>Now type ':1,$ s/[0-9]/#/g' and press Enter.  You have just turned every
>number
>in your file into a # sign.
>
>Now do a search by typing '/...e' and press Enter.  To change from the
>cursor to
>the end of the word and the next word, type '2cwXXX' and press ESC.  Now
>press 'n' to find the next instance of any 3 characters preceeding an e,
>and press '.'
>to repeat the change command.
>
>NOTE:  Period in a search is the single character wildcard.  So 'p.t'
>finds pat,
>pet, pit, pot, prt, and put.  Period in command mode repeats the last
>text modify
>command.  So if you did a yank and put, it will put the yanked data again.
>
>Now exit your file.  Here is a trivial example of building a shell
>script.  Enter
>'ls > foo; ls >> foo; sort foo > bar; rm foo; vi bar' on the bash
>command line.
>You should see 2 lines of each of your files.
>
>On the 1st line press 'J'.  This joins the 1st and 2nd line.  Now press
>Enter and '.'
>until you have joined all of the lines and every line has 2 copies of a
>filename.
>
>Type ':1,$ s" " foo/"' and press Enter.  (In case you are having trouble
>reading this,
>it is: s<double_quote><space><double_quote><space>foo<slash><double_quote>.
>You do this because the standard seperator charactor, '/' is part of the
>substitution
>string.  I have heard that any seperator charactor will do, but I
>haven't tested it.
>
>Now type ':1,$ s/^/cp /' and press Enter.
>
>Now type 1G to go to the top of the file.  Press 'O' to go into input
>mode on the
>line before the cursor (open a new line) and type:
>
>#!/bin/sh
>#
>mkdir foo
>
>Press ESC, and type ':wq' to save your file, bar.  Now, from the bash
>command
>line, enter 'chmod 700 bar' to make the bar file executable.  Enter 'ls
>-l bar' to
>see the permissions.  Now enter './bar'.  Now enter 'ls temp'. To get
>rid of the
>temp directory, enter 'rm -r foo'.
>
>Like I said, this is a trivial example.  But I have created shell
>scripts that do the
>following on over 100 files just as easily:
>
>grep foo file | tr -s ' ' | cut -f1,3-5,7 -d' ' > file.temp
>
>This will find every line that has the characters foo in it in each
>file, turn multiple
>spaces into a single space, get the 1st, 3rd, 4th, 5th, and 7th space
>seperated fields,
>and write that to the same file with a .temp extension.  It might take a
>few minutes
>to run, but it beats hours of trying to use cut and paste to get only
>those fields.
>
>And when it comes to editing C code, vi has lots of nice features like
>verifying
>that your parens, brackets, and braces are closed, color coding reserved
>words,
>you can even set it to automatically complete things like for conditions or
>if/else statements.
>
>So, that's why I call vi powerful.  I highly recommend 'Learning the Vi
>editor' from
>O'Reilly.  It not only covers vi, it also discusses the variants like
>vim and what they
>add to basic vi.
>
>Later . . .   Jim
>
>
>
>Rick Radzville wrote:
> >Wow. I'm an expert microsoftie, linux newbie. This comment is the reason 
>why
> >I'm experimenting w/Linux. Jim, would you mind sharing a few more 
>details?
> >When you refer to "data", are you talking about "a listing of the
> >directories & files on the user's PC" or "a database"?
> >v/r,
> >Rick Radzville
> >
> >




More information about the lug mailing list