Am 09.06.2011 21:06, schrieb Charles Campbell:
[email protected] wrote:
Hi,
In my script, I split the window into 4 windows. In my script, I jump
to each window when I need by using the command exe g:window_number .
" wincmd w". However, when I split the window during the editing, the
g:window_number will be changed. So the command does not work since it
switches to the different window.

Does anyone know how to solve this problem? Can I assign each window a
permanent number or lable so the script can switch to them later?

Windows are numbered by vim; additional window splits will, as you've
noticed, change windows' numbers. So, no, you can't assign permanent
numbers to windows.

However, buffers are numbered by vim when loaded, and don't change
during the session. Perhaps you could use bufwinnr(), although that
admittedly won't differentiate between different windows accessing the
same buffer.

Here is a plugin that might solve your problem:


" Commands:
"
" :WincmdTag
"
"   give each window a label, i.e. set a variable w:tagwinnr to its window
"   number.
"
"
" :WincmdW [N]  or
" :[N]WincmdW
"
"   go to the window labelled with [N].
"
"   Default [N] is 0, which is out of range.  [N] being out of range goes to
"   the window with the smallest (highest) available label.
"

com! -bar WincmdTag  call s:WincmdTag()
com! -bar -count WincmdW  call s:WincmdW(<count>)

func! s:WincmdTag()
    for wnr in range(1, winnr("$"))
        " w:tagwinnr
        call setwinvar(wnr, "tagwinnr", wnr)
    endfor
    let t:tagwinnr_max = winnr("$")
endfunc

func! s:WincmdW(num)
    " w:tagwinnr
    let labels = map(range(1, winnr("$")), 'getwinvar(v:val, "tagwinnr")')
    let numbers = filter(copy(labels), 'type(v:val)==0')
    if empty(numbers)
        echo 'WincmdW: no labels found (try :WincmdTag first)'
        return
        " or all labelled windows removed
    endif
    if !exists("t:tagwinnr_max")
        " not expected to happen
        let t:tagwinnr_max = max(numbers)
    endif
    if a:num == 0
        let num = min(numbers)
    elseif a:num > t:tagwinnr_max
        let num = max(numbers)
    elseif index(numbers, a:num) == -1
        echo printf('WincmdW: label %d not found (window closed?)', a:num)
        return
    else
        let num = a:num
    endif
    let wnr = 1 + index(labels, num)
    exec wnr. "wincmd w"
endfunc

--
Andy

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