On 22/04/11 05:04, Rostyslaw Lewyckyj wrote:
Suppose that I do a pattern search and the search function returns
the location into a variable xxx. e.g.
let xxx = search("pat","flags",line("'y"))
and *later* , not immediatly, in the script I'd like to do something like
continue my script at xxx or
print the line at xxx (i.e. xxx p ) or
join the line at xxx+1 with the line at xxx (i.e. xxx j)
How do I do this?
Well, you must make sure that the contents of variable xxx are not
modified between running the function and using the value.
In addition, the variable must be visible both where it is set and where
it is used: this is where scopes come into play:
- Variables used without a scope are function-local if used in a
function, otherwise they are global
- Variables prefixed with l: are local to function
- Variables prefixed with g: are global
- Variables prefixed with s: are local to script
- Variables prefixed with b: are local to buffer
- Variables prefixed with w: are local to window
- Variables prefixed with t: are local to tabpage
- For this purpose you should probably not use a variable prefixed with
v:, these are special variables or constants predefined by Vim.
In a script, you must use ex-commands rather than Normal mode commands
(and, in a script, the initial colon may be omitted); but see also
":help :normal":
to display the value of a variable:
echo xxx
or
echomsg xxx
or even
echoerr xxx
to print the xxx-th line:
exe xxx 'p' " using the :p[rint] command
to join the xxx-th line with the next one:
with an intervening space
exe xxx 'j' " using the :j[oin] command
with no intervening spaces
exe xxx 'j!'
to go to the xxx-th window from top
exe xxx 'wincmd w'
to yank xxx lines (starting at the current one) into register p
exe '.,' . (line('.') + xxx - 1) 'y p'
" using the :y[ank] command
" the outermost parentheses are not really necessary
or
exe 'normal!' xxx . ":y p\r"
What you *cannot* do is continue execution at line xxx of your script,
because Vim-script is a strictly structured language, where there is no
"goto" command similar to the one used in FORTRAN or in DOS batch script
language. You *can* set the cursor on line xxx of the current editfile,
by using simply
exe xxx
Beware that all this requires that no lines be added or removed anywhere
in the first xxx lines of the editfile beween the times when you store
the value and use it. If you may alter the number of lines, you should
mark your place using a mark, and later return to that mark:
call search(pattern)
" where pattern is a variable name,
" for a literal value you would of course need quotes
normal! mz
" ... do something
normal! `z
Best regards,
Tony.
--
"Calvin Coolidge was the greatest man who ever came out of Plymouth
Corner, Vermont."
-- Clarence Darrow
--
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