Tuesday, March 8, 2011

Re: How does one mix ex commands and normal commands such as ma (to set a mark) in a script?

To answer the subject question, one uses :normal in scripts to enter
such commands. Note, however, there is a :mark ex command.

> I'd like to write me a script which for example does the following:...
> - set a mark at the current line        ma
> - find a line with a  pattern  x           /^$/
> - set a mark there                               mb
> - find a line with pattern y              /^From - /
> - set a mark there                               mc
> - write the lines a-b to file1             :'a,'b  w  file1
> - write the lines b-c  to file2            :'b,'c   w  file2
> ------------------------------------------------------------------------
It translates to script directly:

mark a
try
/^$/
catch /E486/
echo 'pattern x not found'
finish
endtry
mark b
try
/^From - /
catch /E486/
echo 'pattern y not found'
finish
endtry
mark c
'a,'bw! file1
'b,'cw! file2

or more simply, without marks:

try
.,/^$/w! file1
catch /E486/
echo 'pattern x not found'
finish
endtry
normal n
try
.,/^If you /w! file2
catch /E486/
echo 'pattern y not found'
endtry

(Note that I've used w! to faciliate rerunning, any existing file1 or
file2 is overwritten.)

> - find a line with pattern x /?x?x?x/
> - ma
> - find a line with pattern y /?y?y?y/
> - mb
> - move all lines with pattern z in a-b to 'a-1

How about for the second task:

try
/?x?x?x/
mark a
.,/?y?y?y/g/pattern z/m 'a-1
catch /E486/
echo v:exception
endtry

Regards, John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: