1

I have the TaskWarrior Plugin installed. In order to show the tasks, you have to enter the :TW command. The problem is, TaskWarrior is displayed in the same buffer that I'm using for editing. In order to avoid that, I have to create a new Split window, switch to the split window and then enter :TW.

I want to have a command that does that in a "switch/toggle" style. The command will not create a new split window if a split window already exists. For example, I have the command to nt keystrokes and it creates/removes the split window each time.

Any suggestions on where to start and how difficult of a task that is?

Omar Abid
  • 14,746
  • 24
  • 72
  • 105

2 Answers2

1

I think you can do this by defining a custom command using one of the languages that was compiled into your copy of vim. I'll assume Python. See if this works::py3 print('hello world'). If so, then the documentation specifically discusses manipulating 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.

If you combine that with the example from 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

It doesn't seem like a big stretch to whip something up that does exactly what you're looking for, similar to this:

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)

This is embedding the python inside the Vimscript. The last few lines will map the custom command to the python function do_window_stuff().

Using command will let you invoke it at the command mode prompt as :WindowThing (the command needs to begin with an uppercase letter).

Using noremap will remap the key sequence .windowthing when entered from a non-insert mode.

Z4-tier
  • 4,716
  • 1
  • 19
  • 32
  • I do have Python but I'll prefer if the solution doesn't depend on it. Is it really that complicated to get it working only by Vim syntax? – Omar Abid Jun 21 '19 at 09:38
  • It's definitely do-able using pure Vim script. Only reason I suggest Python is to avoid spending the brain-CPU-cycles on a domain language. – Z4-tier Jun 21 '19 at 18:09
1

I have adapted some online code and it is doing the job pretty well for now. This will toggle a TaskWarrior split tab when the combination nt is pressed.

" 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
Omar Abid
  • 14,746
  • 24
  • 72
  • 105
  • good idea to use `noremap`, it will ignore other mappings when it runs (`nore` = 'no recursive'), preventing the cascading that regular `map` might cause. – Z4-tier Jun 21 '19 at 18:22