Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active April 17, 2024 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Konfekt/8e484af2955a0c7bfe82114df683ce0f to your computer and use it in GitHub Desktop.
Save Konfekt/8e484af2955a0c7bfe82114df683ce0f to your computer and use it in GitHub Desktop.
Open GUI application / file from Vim in background
" To start the GUI app, use :Silent! app
" Useful, say, to look up a keyword by :set keywordprg=:Silent!\ zeal
" To open file, use :Open! file
" Useful, say, to view a compiled HTML of a markdown file, by :Open! %:r.html
if executable('cmd.exe') " Win32 or WSL
command! -complete=shellcmd -nargs=1 -bang Silent
\ execute ':silent !' .
\ (&shell =~? '\v(^|\\)cmd\.exe$' ? '' : 'cmd.exe /c ') .
\ (<bang>0 ? 'start /b ' : '') . <q-args> | execute ':redraw!'
elseif has('unix')
command! -complete=shellcmd -nargs=1 -bang Silent execute ':silent !' .
\ (<bang>0 ? 'nohup ' . <q-args> . ' </dev/null >/dev/null 2>&1 &' : <q-args>) | execute ':redraw!'
endif
command! -complete=shellcmd -nargs=1 -bang Sil Silent<bang> <args>
" Windows / Git Bash / WSL
if executable('cmd.exe')
let s:cmd = "explorer.exe"
" Linux/BSD
elseif executable("xdg-open")
let s:cmd = "xdg-open"
" MacOS
elseif executable("open")
let s:cmd = "open"
endif
exe 'command! -complete=file -nargs=1 -bang Open exe "Silent<bang> ' . s:cmd . ' " . shellescape(<q-args>)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment