开发者

Switching to VIM window by name

There are some excellent Vim plugins for switching to a specific file or buffer by typing part of the name. Does anyone know of开发者_Python百科 a plugin like these that allows for quickly switching between open windows?

For example, if I had a vsplit with a file named 'a.txt' on one side and 'b.txt' on another I'd like to be able to switch between them by typing the filename (or just 'a' or 'b' with incremental searching). This might not be too useful for two windows, but I often have up to 5 windows open, so switching between them using the normal navigation buttons can be a pain.

WinWalker seems to support this type of functionality, but wrapped inside of a much larger framework for window navigation.


:b does that happily if you have :setswitchbuf+=useopen (in your vimrc). It supports file name completion, e.g. with Ctrl-D.

by MarcWeber et al from #vim


@Aaron Thoma' s answer with switchbuf+=useopen did not work for me. Actually the doc does not mention that the setting is supported by :b comand.

I've used the following snippet:

let windowNr = bufwinnr(pattern)
if windowNr > 0
  execute windowNr 'wincmd w'
endif  

See bufwinnr()


Here's a small function I came up with after trying to achieve the same thing

function! ActivateWindowByName(name)
    let l:bufid = bufnr(a:name)
    let l:winids = win_findbuf(l:bufid)
    call win_gotoid(l:winids[0])
endfunction

Explanation:

  • bufnr('substring-of-buffer-name') returns the buffer number
  • win_findbuf(buffer_number) returns a list of window IDs (not the same thing as buffer number) that use the buffer identified with buffer_number
  • win_gotoid(window_ID) goes to the window identified with window_ID that can be in a different tabpage from the current one

bufnr isn't guaranteed to find a buffer, so it can return -1. In that case, win_findbuf will find an empty list and [0] will be out of range.


This uses the python api, so you'll only be able to use it in vim, but I'm sure someone who loves vim script could convert it easily enough.

Use it like this:

:WSeek win
:WSeek READ
:WSeek README
:WSeek a.
etc.

No auto completion I'm afraid. Sorry~

" Call using:
" :source window-swap.vim or put in plugins dir

function! WSeek(target) 
  if !has('python')
    echo "Error: this plugin requires vim with +python"
    finish
  else
python << EOF

import vim

target = vim.eval("a:target")
found = False
for i in range(0, len(vim.windows)):
  vim.command(str(i+1) + "wincmd w")
  bits = vim.current.buffer.name.split("/")
  bits = bits[-1]
  if (bits.startswith(target)):
    found = True
    break

# Go back to the start on no match
if not found:
  vim.command("1wincmd w")

EOF
  endif
endfunction 

command! -nargs=1 WSeek :call WSeek(<q-args>)


I don't know how to switch to a window based on the filename, but I use the following shortcuts in my .vimrc to move between windows.

noremap <S-W> <C-w><Up>
noremap <S-S> <C-w><Down>
noremap <S-A> <C-w><Left>
noremap <S-D> <C-w><Right>

These are just as simple, and I don't have more than 4 windows open, so I'm never more than 2 window movements away from any window. Also, you don't have to type the filename.

I use the standard FPS movement keys, wasd to move between windows as it's intuitive to me, and I don't have to shift my fingers to the arrow keys -- pinky on the shift and the other three on the letters. You can change them to whatever you're comfortable with (hjkl would be a good alternate)

I prefer to use Shift instead of Ctrl/Alt/Option/Cmd because the locations and sizes vary between keyboards (e.g., Fn is the leftmost on a macbook pro, whereas Ctrl is the leftmost on a full mac keyboard) and I've had issues with vim on Linux not recognizing the Cmd key.


I suggest using the Command-T plugin. It indexes the files in the current folder (and its subfolders) and allows you to type part of a file name to open it. In my .vimrc, I also added this link:

nnoremap <S-F12> :CommandT<CR>

this way I press Shift-F12 and type the file name to open it.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜