Re: [CentOS] Vim scripting - cursor motion

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Jussi Hirvi
On 10.6.2011 21.42, Les Mikesell wrote:
> I thought the point of using vim instead of something more appropriate
> for scripting was that you already knew how to use it.

I only wrote that I know vim *better* than sed, awk or perl. Obviously
there is a lot about vim that I don't know.

 > Why not do:
> vim -W script testfile
> and go through the motions you know (which can include 1G to go to the
> 1st line and G to go to the last).
> Then run
> vim -s script realfile
> to do the same actions again.

Ok, that helped me along. For some reason the motions (like 1G0 for
beginning of file, or G$ for last char in file) work, when the script is
called from command-line with -s flag (vim -s myscript myfile). But they
don't work when the script is called from inside vim (:source myscript).
I wonder why.

However, this script does more or less what I want - transforms a
tab-text file to a comma-separated (CVS) file:

:% s/\t/","/g
:% s/\r\n/\r/g
:% s/\n/"),\r("/g
1G0I("^[
Gdd
G$xa;^[
:w

Comments to lines:
1) replace tabs
2) make line endings regular
3) insert quotes and brackets to end & beginning fo each line
4) handle the beginning of file
5) remove the last line (created by this script)
    (the command G works, but it rings the bell for some reason)
6) handle the end of file
7) write file

So this was my first-ever vim script. So far I am not convinced about
vim scripting (ok, I was warned, too)... Test cycle is slow (modify
script, quit the realfile, open realfile again with vim -s script).
Verbal error messages would be useful. There is supposed to be
"integrated debugger". I would like to know more.

- Jussi

--
Jussi Hirvi * Green Spot
Suvilahdenkatu 1 B 78 * 00500 Helsinki * Finland
Tel. +358 9 493 981 * Mobile +358 40 771 2098 (only sms)
[hidden email] * http://www.greenspot.fi
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Les Mikesell
On 6/11/11 4:03 AM, Jussi Hirvi wrote:
>>
> So this was my first-ever vim script. So far I am not convinced about
> vim scripting (ok, I was warned, too)... Test cycle is slow (modify
> script, quit the realfile, open realfile again with vim -s script).
> Verbal error messages would be useful. There is supposed to be
> "integrated debugger". I would like to know more.

I'd still recommend learning to do it in perl as being likely faster and more
generally useful, especially if the sql db you mentioned can be accessed
directly. The regeps will be approximately the same and it is easy to find perl
example code for DBI operations and manipulating files.  And unlike working in
shell/awk/editors, you very seldom find an operation that perl can't do itself
so it often ends up simpler than the shell wrapper you need for other tools.

--
   Les Mikesell
    [hidden email]

_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Alain Péan
Le 11/06/2011 17:56, Les Mikesell a écrit :

> On 6/11/11 4:03 AM, Jussi Hirvi wrote:
>> So this was my first-ever vim script. So far I am not convinced about
>> vim scripting (ok, I was warned, too)... Test cycle is slow (modify
>> script, quit the realfile, open realfile again with vim -s script).
>> Verbal error messages would be useful. There is supposed to be
>> "integrated debugger". I would like to know more.
> I'd still recommend learning to do it in perl as being likely faster and more
> generally useful, especially if the sql db you mentioned can be accessed
> directly. The regeps will be approximately the same and it is easy to find perl
> example code for DBI operations and manipulating files.  And unlike working in
> shell/awk/editors, you very seldom find an operation that perl can't do itself
> so it often ends up simpler than the shell wrapper you need for other tools.
>

I hope not to begin a flame war, but I would recommend Python. It can do
the same things as Perl (regexp ansd so on), but is easier and faster to
learn, and the code is also much more readeable...

Alain
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Les Mikesell
On 6/11/11 11:08 AM, Alain Péan wrote:

>
>>> So this was my first-ever vim script. So far I am not convinced about
>>> vim scripting (ok, I was warned, too)... Test cycle is slow (modify
>>> script, quit the realfile, open realfile again with vim -s script).
>>> Verbal error messages would be useful. There is supposed to be
>>> "integrated debugger". I would like to know more.
>> I'd still recommend learning to do it in perl as being likely faster and more
>> generally useful, especially if the sql db you mentioned can be accessed
>> directly. The regeps will be approximately the same and it is easy to find perl
>> example code for DBI operations and manipulating files.  And unlike working in
>> shell/awk/editors, you very seldom find an operation that perl can't do itself
>> so it often ends up simpler than the shell wrapper you need for other tools.
>>
>
> I hope not to begin a flame war, but I would recommend Python. It can do
> the same things as Perl (regexp ansd so on), but is easier and faster to
> learn, and the code is also much more readeable...

There is sort-of a tradeoff in the syntax choices between the languages.  Perl
is easier to write because it is flexible and you can use a syntax that
resembles something you already know (shell/c/awk) with simple changes.  That
makes other peoples perl less readable, but not your own.  The other win for
perl is that any operation that would take more than a page of code that you are
likely to want to do has almost certainly already been done and is available as
a module on CPAN (and possibly packaged as an rpm).  Does python have anything
to match that yet?  How many database types can it access with available
modules?  Perl's DBI/DBD connector list is pretty large.

--
   Les Mikesell
     [hidden email]
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Alain Péan
Le 11/06/2011 18:22, Les Mikesell a écrit :

> On 6/11/11 11:08 AM, Alain Péan wrote:
>>>> So this was my first-ever vim script. So far I am not convinced about
>>>> vim scripting (ok, I was warned, too)... Test cycle is slow (modify
>>>> script, quit the realfile, open realfile again with vim -s script).
>>>> Verbal error messages would be useful. There is supposed to be
>>>> "integrated debugger". I would like to know more.
>>> I'd still recommend learning to do it in perl as being likely faster and more
>>> generally useful, especially if the sql db you mentioned can be accessed
>>> directly. The regeps will be approximately the same and it is easy to find perl
>>> example code for DBI operations and manipulating files.  And unlike working in
>>> shell/awk/editors, you very seldom find an operation that perl can't do itself
>>> so it often ends up simpler than the shell wrapper you need for other tools.
>>>
>> I hope not to begin a flame war, but I would recommend Python. It can do
>> the same things as Perl (regexp ansd so on), but is easier and faster to
>> learn, and the code is also much more readeable...
> There is sort-of a tradeoff in the syntax choices between the languages.  Perl
> is easier to write because it is flexible and you can use a syntax that
> resembles something you already know (shell/c/awk) with simple changes.  That
> makes other peoples perl less readable, but not your own.  The other win for
> perl is that any operation that would take more than a page of code that you are
> likely to want to do has almost certainly already been done and is available as
> a module on CPAN (and possibly packaged as an rpm).  Does python have anything
> to match that yet?  How many database types can it access with available
> modules?  Perl's DBI/DBD connector list is pretty large.
>

Here it is. It seems to me rather large, even if I don't know the
equivalent list for Perl :
http://wiki.python.org/moin/DatabaseInterfaces

There are also a very large number of Python modules available, and
tools to easily install them, for example there :
http://pypi.python.org/pypi

Alain
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Jussi Hirvi
In reply to this post by Alain Péan
On 11.6.2011 19.08, Alain Péan wrote:
> I hope not to begin a flame war, but I would recommend Python. It can do
> the same things as Perl (regexp ansd so on), but is easier and faster to
> learn, and the code is also much more readeable...

In practice, any language you know well enough... This is a simple task.

- Jussi
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos
Reply | Threaded
Open this post in threaded view
|

Re: [CentOS] Vim scripting - cursor motion

Kahlil Hodgson
In reply to this post by Jussi Hirvi
On 11/06/11 19:03, Jussi Hirvi wrote:
> Ok, that helped me along. For some reason the motions (like 1G0 for
> beginning of file, or G$ for last char in file) work, when the script is
> called from command-line with -s flag (vim -s myscript myfile). But they
> don't work when the script is called from inside vim (:source myscript).
> I wonder why.

Try adding :normal to every 'key stroke' command, i.e.,

> :% s/\t/","/g
> :% s/\r\n/\r/g
> :% s/\n/"),\r("/g
> :normal 1G0I("^[
> :normal Gdd
> :normal G$xa;^[
> :w

off the top of my head, that should then work in both scenarios (not
tested).

Yes, vim scripting is a bit weird, nowhere near as elegant as emacs
lisp, but it does get the job done if you have the patience.

For debugging try the online help

:h debug-scripts

Happy Hacking!

Kal
_______________________________________________
CentOS mailing list
[hidden email]
http://lists.centos.org/mailman/listinfo/centos