I would put something like this in my ~/.vimrc or a file with the .vim extension in ~/.vim/plugin/:
command! FW call FilterToNewWindow('myscript')
function! FilterToNewWindow(script)
let TempFile = tempname()
let SaveModified = &modified
exe 'w ' . TempFile
let &modified = SaveModified
exe 'split ' . TempFile
exe '%! ' . a:script
endfunction
Then you can just do ":FW
".
It might be a good idea to change the command definition to require a filter script name argument to pass to the function instead of "hard coding" it.
For commands I use often, I generally set up a function
in my .bashrc to make them paginate if longer than a screen.
Like your example: (ps -A)
function ps { command ps "$@" |& less -F; }
This replaces ps
with a function, named ps
, which calls the original ps
command with whatever arguments given on the command line, then pipes the output (stdout and stderr, using the |&
pipe) into less -F
, which pauses if there's more than a screen-full, but exits immediately if it's less than a screen-full.
VERY handy, doesn't interfere with anything I've worked with so far, and is just cool!
You can even add oft-used options into the command/functions too:
function nm { command nm --demangle "$@" |& less -F; }
This makes nm
always demangle C++ symbols. AND paginates the output. Yay!
I'm running Debian, so I use the apt-cache
command quite often, search and show mostly. This function causes those particular options to paginate, search output is sorted, and everything paginates:
function apt-cache { case "$1" in "search") command apt-cache "$@" | sort | less -F;; *) command apt-cache "$@" | less -F;; esac; }
If the command is 'search', sort the output, then paginate with less -F
, but if command is anything else, just paginate, without sorting.
Occasionally I forget I've got the functions, and I'll do something like:
apt-cache search gcc | less
The function doesn't interfere, everything works as expected, no harm either way.
Another little tweak, I use the same .bashrc
on all my systems, so sometimes a utility might not be installed, so there's no need for the function. I make them conditional like this:
which apt-cache &>/dev/null && function apt-cache { case "$1" in "search") command apt-cache "$@" |& sort | less -F;; *) command apt-cache "$@" |& less -F;; esac; }
This just uses the which
command to determine if a program is available, if it isn't, it quietly fails and skips installing the function. Taa Daa!
Best Answer
If you are using vim 8 or neovim, maybe you would like to try :
http://github.com/skywind3000/asyncrun.vim ,
Nice replacement to vim-dispatch with much better user experience.
Easy to use, just start your background command by :AsyncRun (just like old "!" cmd).
Command is done in the background, no need to wait for the entire process to finish.
Output are displayed in the quickfix window, errors are matched with errorformat.
You can explore the error output immediately or keep working in vim while executing.