Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim filters and stdout/stderr

Tags:

vim

When I use :%! to run the contents of a file through a filter and the filter fails (it returns another code than 0) and prints an error message to stderr I get my file replaced with this error message. Is there a way to tell vim to skip the filtering if the filter returns an status code that indicates an error and/or ignore output the filter program writes to stderr?

There are cases where you want your file to replaced with the output of the filter but most often this behavior is wrong. Of course I can just undo the filtering with one keypress but it isn't optimal.

Also I have a similar problem when writing a custom vim script to do the filtering. I have a script that calls a filter program with system() and replaces the file in the buffer with its output but there doesn't seem to be a way to detect if the lines returned by system() where written to stdout or to stderr. Is there a way to tell them apart in vim script?

like image 280
ahe Avatar asked Apr 04 '10 19:04

ahe


2 Answers

To Vim 7 were added new autocommand events: ShellCmdPost and ShellFilterPost

augroup FILTER_ERROR
  au!
  autocmd ShellFilterPost * if v:shell_error | undo | endif
augroup END
like image 98
Jorengarenar Avatar answered Sep 22 '22 05:09

Jorengarenar


This is what I ended up doing:

function MakeItAFunction(line1, line2, args)
  let l:results=system() " call filter via system or systemlist
  if v:shell_error
    "no changes were ever actually made!
    echom "Error with etc etc"
    echom results
  endif
  "process results if anything needed?

  " delete lines but don't put in register:
  execute a:line1.",".a:line2." normal \"_dd"
  call append(a:line1-1, l:result)  " add lines
  call cursor(a:line1, 1)  " back to starting place
  " echom any messages
endfunction
command -range <command keys> MakeItAFunction(<line1>,<line2>,<q-args>) 
"                                         or <f-args>, etc.

You can see my full code at http://vim.wikia.com/wiki/Perl_compatible_regular_expressions

It's complicated, but it works and when it's used, it's fairly transparent and graceful. Hope that helps in any way!

like image 24
Peter Kay Avatar answered Sep 20 '22 05:09

Peter Kay