如何制作在Vim窗口之间移动的快捷方式? [英] How to make a shortcut for moving between Vim windows?

查看:50
本文介绍了如何制作在Vim窗口之间移动的快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Vim标签,其中显示9个缓冲区(均等分开,例如3×3表).

当前,要从左上方的窗口转到右下方的窗口,我必须按 3 Ctrl + W J ,然后是 3 Ctrl + W L .这很麻烦,我只想按 Ctrl + 9 进入第9个窗口,然后按 Ctrl + 3 进入第三个窗口,等等.

有什么简单的方法可以在Vim中映射这样的内容?

解决方案

有一个简单得多的解决方案,它比使用鼠标或固定设置的移动映射要好.如果窗口编号与您对于3x3矩阵所想到的不同,或者如果您决定使用少于9个窗口,则它们将中断.方法如下:

在您的 .vimrc 中包括以下内容:

 让i = 1而我< = 9执行'nnoremap< Leader>'.一世 .':'.一世 .'wincmd w< CR>'让我=我+ 1最后 

现在,您只需按< Leader>< number> ,然后转到所需的窗口编号.我不建议超过9,因为IMO(具有多个视口的实用程序)遵循在评论中询问我的状态行,所以这里是:

 "状态栏StatusLine term = bold cterm = bold ctermfg = White ctermbg = 235您好StatusHostname term = bold cterm = bold ctermfg = 107 ctermbg = 235 guifg =#799d6a嗨StatusGitBranch条款=粗体cterm =粗体ctermfg = 215 ctermbg = 235 guifg =#ffb964功能!MyGitBranchStyle()让分支= GitBranch()如果分支==''让branchStyle =''别的让branchStyle ='git:'.分支结尾返回branchStyle终端功能功能!WindowNumber()让str = tabpagewinnr(tabpagenr())返回str终端功能设置laststatus = 2设置statusline =%#StatusLine#%F%h%m%r \%h%w%y \ col:%c \ lin:%l \,%L \ buf:%n \ win:%{WindowNumber()}\ reg:%{v:register} \%#StatusGitBranch#%{MyGitBranchStyle()} \ \%=%#StatusLine#%{strftime(\%d/%m/%Y-%H:%M \")} \%#StatusHostname#%{hostname()} 

最后一行应该是一行(请注意,如果您的设置自动将其分成多行).我知道有一些方法可以在每个步骤中使用增量字符串连接来组织它,但是我懒得更改它.:) GitBranch()函数(具有其他git功能)由提供git.vim插件.如此处所述,其中有一个错误,我使用了 解决方案

There's a much simpler solution than using the mouse or hard-set movement mappings; they will break if the window numberings are different from what you have in mind for a 3x3 matrix, or if you decide to work with less than 9 windows. Here's how:

Include the following in your .vimrc:

let i = 1
while i <= 9
    execute 'nnoremap <Leader>' . i . ' :' . i . 'wincmd w<CR>'
    let i = i + 1
endwhile

Now you can just press <Leader><number> and be taken to the window number you want. I wouldn't recommend going beyond 9, because IMO, the utility of having multiple viewports follows a Rayleigh distribution and quickly becomes useless with too many viewports in one window.

It will be helpful if you have the window number displayed in your statusline to aid you in quickly figuring out which window you're on and which window you want to go to. To do that, use this little function and add it accordingly in your statusline.

function! WindowNumber()
    let str=tabpagewinnr(tabpagenr())
    return str
endfunction

See it in action in your statusline:

set laststatus=2
set statusline=win:%{WindowNumber()}

Note that the above line will replace your statusline. It was just meant for illustration purposes, to show how to call the function. You should place it where ever you think is appropriate in your statusline. Here's what mine looks like:


Update

romainl asked for my status line in the comments, so here it is:

"statusline
hi StatusLine term=bold cterm=bold ctermfg=White ctermbg=235
hi StatusHostname term=bold cterm=bold ctermfg=107 ctermbg=235 guifg=#799d6a
hi StatusGitBranch term=bold cterm=bold ctermfg=215 ctermbg=235 guifg=#ffb964

function! MyGitBranchStyle()
    let branch = GitBranch()
    if branch == ''
        let branchStyle = ''
    else
        let branchStyle = 'git:' . branch
    end
    return branchStyle
endfunction

function! WindowNumber()
    let str=tabpagewinnr(tabpagenr())
    return str
endfunction

set laststatus=2
set statusline=%#StatusLine#%F%h%m%r\ %h%w%y\ col:%c\ lin:%l\,%L\ buf:%n\ win:%{WindowNumber()}\ reg:%{v:register}\ %#StatusGitBranch#%{MyGitBranchStyle()}\ \%=%#StatusLine#%{strftime(\"%d/%m/%Y-%H:%M\")}\ %#StatusHostname#%{hostname()}

The last line should be a single line (be careful if your setup automatically breaks it into multiple lines). I know there are ways to keep it organized with incremental string joins in each step, but I'm too lazy to change it. :) The GitBranch() function (with other git capabilities) is provided by the git.vim plugin. There's a bug in it as noted here and I use the fork with the bug fix. However, I'm leaving both links and the blog here to give credit to all.

Also, note that I use a dark background, so you might have to change the colours around a bit if you are using a light scheme (and also to suit your tastes).

这篇关于如何制作在Vim窗口之间移动的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆