[CentOS] Vim scripting - cursor motion

Sat Jun 11 09:03:52 UTC 2011
Jussi Hirvi <listmember at greenspot.fi>

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)
jussi.hirvi at greenspot.fi * http://www.greenspot.fi