On 05/13/12 13:44, richard emberson wrote:
> In a script when one calls "cursor(line, column)" or
> "execute normal 4rx", implicitly it is the current buffer
> that is the target; it is the current buffer that is changed.
> I'd like to know if its possible to explicitly alter the
> target buffer in scripts such that, optionally, the same
> edit commands can be applied to either the default buffer
> or to some other, possibly hidden buffer.

I do it regularly in slightly different contexts (macros), and a
quick test of a function worked for me:

function! SomeEdits(buffer)
    if a:buffer
        exec 'sp #'.(a:buffer)
    endif
    try
        " echo bufnr('%')
        " some boring edit
        g/foo/s/$/XXX
    finally
        if a:buffer
            q
        endif
    endtry
endfunction

call SomeEdits(0) " do some edits in the current buffer
call SomeEdits(14) " do the edits in buf#14

In my case, I based the decision off an argument named a:buffer, but
you could just as easily come from a global.  It might choke a
little if the 'buftype' isn't empty (it did for "nofile" when I
tested it).  I chose to do a split/quit pair as it tends to
interfere less with my current editing, but you could also "setlocal
hidden" then ":e"dit the other file, then return to the original by
saving/restoring bufnr().

-tim


-- 
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

Reply via email to