0

I have :SyntasticCheck mapped to F8 in my .vimrc.

map <F8> :SyntasticCheck<CR>

This open the Syntastic window on a split.

I would like to close it with the same mapping. Closing is done with :SyntasticReset, but it only works if the Synastic window is open.

How can I create a mapping that only open Synastic windows if it's not visible and close it if it exists ?

1

You could use something like this:

function! ToggleSyntastic()
    for i in range(1, winnr('$'))
        let bnum = winbufnr(i)
        if getbufvar(bnum, '&buftype') == 'quickfix'
            lclose
            return
        endif
    endfor
    SyntasticCheck
endfunction

nnoremap <F8> :call ToggleSyntastic()<CR>

As Syntastic populates the location list, the function iterates through the different open windows. If it finds the quickfix window which contains the location list, then it closes it. If no quickfix window was found, then it runs SyntasticCheck

Then you can simply call F8 to call the function.

  • 1
    Works flawlessly ! – stdcall Jul 30 '18 at 14:26
  • One liner if that is your bag: nnoremap <expr> <f8> len(filter(range(1, winnr('$')), 'getbufvar(winbufnr(v:val), "&buftype") == "quickfix"')) ? ":\<c-u>lclose\<cr>" : ":\<c-u>SyntasticCheck\<cr>" – Peter Rincker Jul 30 '18 at 15:31
  • @PeterRincker Personally I'd rather put the function in the autoload directory and have a readable code but one-liners can be nice too. Is there a significant performance gain when using filter rather than a full loop? – statox Jul 30 '18 at 15:46
  • 1
    From what I understand, simple filter() & map() calls typically beat loops in Vimscript. However, I have not personally done any benchmarking. That being said, I think you are right maintainability is more important. That comes down to deciding if a more functional approach with filter() is more readable than a few lines in a loop. I know when I see filter()/map() that a loop is occurring under the covers, but I feel like this is very much a personal preference and whatever the project maintainer feels is the most appropriate. Performance-wise: the number of windows should be small – Peter Rincker Jul 30 '18 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.