如何在Vim中切换分割窗口

时间:2019-06-20 15:49:28

标签: vim taskwarrior

我已安装TaskWarrior插件。为了显示任务,您必须输入:TW命令。问题是,TaskWarrior显示在我用于编辑的同一缓冲区中。为了避免这种情况,我必须创建一个新的拆分窗口,切换到拆分窗口,然后输入:TW

我想要一个以“开关/切换”样式执行此操作的命令。如果已存在拆分窗口,则该命令将不会创建新的拆分窗口。例如,我有nt击键命令,它每次都会创建/删除拆分窗口。

关于从何处开始以及任务的难度有何建议?

2 个答案:

答案 0 :(得分:1)

我认为您可以通过使用编译到vim副本中的一种语言定义自定义命令来实现此目的。我假设使用Python。看看是否可行::py3 print('hello world')。如果是这样,那么documentation将专门讨论如何操作窗口:

5. Window objects                                       python-window

Window objects represent vim windows.  You can obtain them in a number of ways:
        - via vim.current.window (python-current)
        - from indexing vim.windows (python-windows)
        - from indexing "windows" attribute of a tab page (python-tabpage)
        - from the "window" attribute of a tab page (python-tabpage)

You can manipulate window objects only through their attributes.  They have no
methods, and no sequence or other interface.

如果将其与here中的示例结合在一起:

For anything nontrivial, you'll want to put your Python code in a separate 
file, say (for simplicity) x. To get that code into a Vim session, type

 :source x

from within that session. That file will actually be considered to be 
Vimscript code, but with Python embedded.

Extending the above obligatory "hello wordl" example, place

    :map hw :py3 print("hello world")

in x. From then on, whenever you type 'hw' in noninsert mode, you'll 
see the greeting appear in the status line.

For more elaborate code, The format of the file x is typically this:
python << endpy
import vim
lines of Python code
endpy

鞭打看起来完全符合您的期望的东西似乎并不费力,类似于:

py3 << __EOF__
import vim
def do_window_stuff(foo):
    if vim.current.window == foo:
        <do some stuff>
__EOF__
command WindowThing :py3 do_window_stuff(foo)
" OR
noremap .windowthing :py3 do_window_stuff(foo)

这是将Python嵌入Vimscript中。最后几行会将自定义命令映射到python函数do_window_stuff()。

使用command将使您在命令模式提示符下以:WindowThing的形式调用它(命令需要以大写字母开头)。

从非插入模式输入时,使用noremap将重新映射键序列.windowthing

答案 1 :(得分:1)

我修改了一些在线代码,目前为止,它做得很好。按下组合nt时,将切换TaskWarrior拆分选项卡。

" Toggle TaskWarrior
nnoremap nt :call ToggleTaskWarriorMode()<CR>
vnoremap nt :call ToggleTaskWarriorMode()<CR>gv

let g:TaskWarriorMode = 0

function! ToggleTaskWarriorMode()
    let g:TaskWarriorMode = 1 - g:TaskWarriorMode
    if (g:TaskWarriorMode == 0)
       :close
    else
       :split task
       :TW
    endif
endfunction