如何在 Vim 中切换拆分 window

How to toggle a split window in Vim

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

我想要一个以 "switch/toggle" 风格执行此操作的命令。如果拆分 window 已经存在,该命令将不会创建新的拆分 window。例如,我有命令 nt 击键,它每次 creates/removes 拆分 window。

关于从哪里开始以及任务的难度有什么建议吗?

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

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)

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

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

使用 noremap 将在从 non-insert 模式输入时重新映射键序列 .windowthing

我已经修改了一些在线代码,目前效果还不错。这将在按下组合 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