Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Advice on coding in VI ?


On Fri, 23 Nov 2001, Matt - wrote:

> I have recently spent some time in learning the VI
> editor for unix and would be interested in hearing
> from some developers on some intermediate to advanced
> key combinations for increasing speed ? Perhaps you
> have seen a common feature missed by VI newbies.

Delete up until matching line. 
==============================
You've found the need to delete a chunk of stuff, mark the beginning with
some unique code word (I use 'gloop' for instance) skim through till the
end and do "d?gloop" to delete backwards until a line with gloop is found.

Code block indent
=================
If you've got a bracketed segment of code for instance in perl I might  
have this...

open (CFG,"<$configfile") || die "Can't open $configfile\n";
my @configs = <CFG>;
close(CFG);
foreach (@configs,@ARGV)
{
        if(!/^#/ && !/^$/ && /=/)
        {
                chomp();
                my ($lv,$rv) = split(/=/,$_,2);
                $lv = trim($lv);
                $rv = trim($rv);
                $_=$lv;
		# lots of other code deleted for this excercise
	}
}
then I realise it should be in a subroutine and I like my subroutines 
indented. I'd simply add a forward brace at the beginning, one at the end 
then with the cursor on say the bottom one I'd do this ">[{<<]}<<" That's 
indent right, find matching left brace, then move the top brace back one 
indent, find matching right brace, move that back one indent.

Basically, applying delete, yank, indent to some form of navigation, 
whether it be a search or matching bracket. You can equally well grab 20 
lines by doing "20yy" or "y19j".

Manually doing a change to a bunch of files.
===========================================
vi *.h<CR>
/boguscode<CR>
cwgoodcode<ESC>:wn<CR>ncwgoodcode<ESC>:wn...
First line obviously specifies a bunch of files to edit
2nd line does a search
cw is 'change word'
:wn is write file, next file
Search is still in memory so you can just do "n" to find next.

Adding a long line of '=' signs
===============================
70i=<ESC>

You know how confusing this is using Pico in pine for email documenting vi 
commands.. I keep going to use vi commands in pico.

-- 
---<GRiP>--- 
Web: www.arcadia.au.com/gripz 
Phone/fax: 02 4950 1194   
Mobile: 0408 686 201