Jussi
There is a good article on vimscript here: http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html) Sent via BlackBerry by AT&T
On 10.6.2011 18.39, flapeccino@gmail.com wrote:
There is a good article on vimscript here: http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html)
Sorry there was a typo, the correct URL is: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html
Thanks, I found that already, and it is a good one. But it didn't help me solve my problem about cursor motions.
Maybe my question is wrong - maybe I should just use line ranges in commands, for example for the first line: :1,1s/foo/bar/g and for the last line: :$,$s/foo/bar/g
- Jussi
On 6/10/2011 1:03 PM, Jussi Hirvi wrote:
On 10.6.2011 18.39, flapeccino@gmail.com wrote:
There is a good article on vimscript here: http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html)
Sorry there was a typo, the correct URL is: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html
Thanks, I found that already, and it is a good one. But it didn't help me solve my problem about cursor motions.
Maybe my question is wrong - maybe I should just use line ranges in commands, for example for the first line: :1,1s/foo/bar/g and for the last line: :$,$s/foo/bar/g
I thought the point of using vim instead of something more appropriate for scripting was that you already knew how to use it. 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.
Jussi
I tried various ways but it seems the only way to insert a line from a script is to use the append() function (do help append) specifying the line number as a parameter.
I tried it on with an example script "moo.vim" as shown below
flapeccino@T4410 ~ $ cat moo.vim :1,$s/ /,/g :call append(0,"This is the first line") :call append(line('$'),"This is the last line") :w foox :q! flapeccino@T4410 ~ $ cat foo one two three four 1 2 3 4 ichi ni san shi flapeccino@T4410 ~ $ vi -s moo.vim foo flapeccino@T4410 ~ $ cat foox This is the first line one,two,three,four 1,2,3,4 ichi,ni,san,shi This is the last line flapeccino@T4410 ~
BTW thank you for this, I have been using vi for a very long time, and I never realized until now that at least in its vim incarnation it has such a powerful scripting language. I've used sed/awk/perl but never happily and always felt an inferiority complex to the emac brethrens showing off with their emac lisp macros. It must be a deficiency but my fingers never could do emacs.
On Fri, Jun 10, 2011 at 11:42 AM, Les Mikesell lesmikesell@gmail.comwrote:
On 6/10/2011 1:03 PM, Jussi Hirvi wrote:
On 10.6.2011 18.39, flapeccino@gmail.com wrote:
There is a good article on vimscript here:
http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html)
Sorry there was a typo, the correct URL is:
http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html
Thanks, I found that already, and it is a good one. But it didn't help me solve my problem about cursor motions.
Maybe my question is wrong - maybe I should just use line ranges in commands, for example for the first line: :1,1s/foo/bar/g and for the last line: :$,$s/foo/bar/g
I thought the point of using vim instead of something more appropriate for scripting was that you already knew how to use it. 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.
-- Les Mikesell lesmikesell@gmail.com _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
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
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.
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
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.
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
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
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