From: Gerald Lai <[EMAIL PROTECTED]>
Subject: Re: Newbie problem: Beginners script pitfall
Date: Sun, 7 May 2006 12:08:46 -0700 (PDT)
Hi,
thanks a lot for the help!
I got the script running...nearly.
In my opinion the help system of vim cann't help a newbie to find the
things and answers s/he is looking for.
Often the problem is just the "context" I have to feed into the
system to get help. What context ???
Example:
help script
gives me an "Introduction"
help scripts
shows me, how to debug scripts and
helpgrep scripts
clist
gives me 229 matches.
As newbie...one is lost. A program of the complexity and power of vim
deserves something more than grep. What's about an extra user_* file,
which shows contexts (what the plural of context?) of certain topics
and will combine tags in a contextual manner? As for my last
question: How would a newbie guess correctly, that failing with "set
history=100" has something to do with "set nocompatible", without
knowing that in beforehand ?
But this is another story.
My current problem is:
The script combines two consecutive lines of text, which consist of
different contents. The second line will be deleted after the
contents of both are processed.
I want to run the script over an input of different length. The first
line has to be skipped (it is the famous "#!/bin/zsh" :O).
My problem here is, that the script does not stop corrrectly. As it
modifies the length of the text while running, my attempts to pass
a range to the script or to determine the range by the script
itsself, which would be the better solotion, failed.
In Emacs, there is the possibility to stop a looping macro/script
automagically when it tries to access something beyond EOF.
I found a hint for the abort() command, but
help abort
said, that
This function causes abnormal program termination.
despite the fact, that
help function
states:
When the [abort] argument is added, the function will
abort as soon as an error is detected.
. By the way: <C-]> on [abort] gives me
"E426: tag not found [abort]
.
How can I solve this problem?
Thank you very much for any help in advance!
mcc
> On Sun, 7 May 2006, Meino Christian Cramer wrote:
>
> > Hi,
> [snip]
>
> OK, if you're just starting out, before writing scripts, you would need
> to jump through a few hoops first. Please pardon me if you are already
> familiar with what I'm about to write. I'm just taking it from the top.
>
> First, get familiar with the :help system of Vim. You will be using it a
> lot, so make sure you understand
>
> :help help-context
>
> > It consists of foru lines:
> >
> > :normal gg
> > :normal %s/^[ ]*//g
> > :normal gg
> > :normal %s/[ ]*$//g
> >
> > Other versions I tried were:
> >
> > :normal gg
> > :normal %s/^[ ]*//g<CR>
> > :normal gg
> > :normal %s/[ ]*$//g<CR>
> >
> > and
> >
> > :normal gg
> > :normal :%s/^[ ]*//g<CR>
> > :normal gg
> > :normal :%s/[ ]*$//g<CR>
>
> Then as Eric mentioned, you would need to get familiar with all the
> modes of Vim to really make it fly. For this, read this and commit to
> memory if possible:
>
> :help mode-switching
>
> Commiting it to memory is as easy as trying it out to switch between
> modes. Don't bother memorizing, just try to use it daily.
>
> One thing to remember when scripting in Vim is that you can always
> ___manually___ go through every script line. In the case of your 4
> lines, you would find that
>
> :normal gg
>
> does its job. That is, start Vim, type ":normal gg" and hit Enter.
>
> But when you try all of these
>
> :normal %s/^[ ]*//g
> :normal %s/^[ ]*//g<CR>
> :normal :%s/^[ ]*//g<CR>
>
> and press Enter,
>
> it just won't work. You'll know that the second line has problems. What
> would you do if you wanted to do a :substitute command while you were in
> Vim? I think you would do
>
> :%s/^[ ]*//g
>
> and press Enter, just as you illustrated in your script (with the
> unnecessary <CR>). See how the :s command above matches the ":normal gg"
> command that worked? Place the :s command into your script then:
>
> :normal gg
> :%s/^[ ]*//g
>
> and now you have 2 working lines of script code.
>
> > (Intention is to strip off all leading and trailing white space from
> > a file. It is an experiment, so I choose this basic task. May be
> > there is one command to achieve this, but as said...I used this as an
> > exercise.
> [snip]
>
> To be really productive in Vim, you could learn more about Vim's regular
> expressions. Regexes allow you to do the "one command to achieve this"
> deal.
>
> So, in your case of removing leading and trailing white space, this
> would do it:
>
> :%s/^\s\+\|\s\+$//g
>
> Info on the regex elements I used can be found at
>
> :help /\s
> :help /\+
> :help /\|
>
> Now I noticed that you did a couple of
>
> :normal gg
>
> It's good that you're thinking in terms of the cursor position. It'll
> become useful later (in search ranges, see ":help :range"), but in your
> case, you are applying the :substitute command to all lines by
> specifying "%" in ":%s".
>
> Hopefully, that's enough to get you started :)
>
> HTH.
> --
> Gerald
>