tags:

views:

277

answers:

3

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?

+1  A: 

You can use Python to distinguish between stdout and stderr:

python import vim, subprocess
python b=vim.current.buffer
python line=vim.current.range.start
python p=subprocess.Popen(["command", "argument", ...], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
python returncode=p.poll()
python if not returncode: b.append(("STDOUT:\n"+p.stdout.read()+"\nSTDERR:\n"+p.stderr.read()).split("\n"), line)
ZyX
Of course! With every "real" scripting language it's not a problem at all. Didn't think about that. Although I would prefer vim script if possible because I don't want to have more dependencies on my script than necessary.
ahe
I know only one other way: use `system()`, redirect stderr to a temporary file (or `/dev/null`), save stdout to some variable and use `v:shell_error` to determine whether a command failed before overwriting buffer. Note, that `:!` filters replace «!» with previous command and «%» with the current filename while `system()` does not.
ZyX
+2  A: 

:!{cmd} Executes {cmd} with the shell and sets v:shell_error.

If you happen to set up mappings to call your filters, you could do something like the following:

function! UndoIfShellError()
    if v:shell_error
        undo
    endif
endfuntion

nmap <leader>filter :%!/path/to/filter<CR>:call UndoIfShellError()<CR>
Curt Nelson
Nice trick :). Actually I hoped for an error sensitive pendant for the :!{cmd} syntax that I just overlooked. But more and more I fear that no such alternative syntax exists.
ahe
A: 

An alternative would be to run the filter command such as it modifies the file on disk.

For example, for gofmt (www.golang.org) I have these mappings in place:

map <f9> :w<CR>:silent !gofmt -w=true %<CR>:e<CR>
imap <f9> <ESC>:w<CR>:silent !gofmt -w=true %<CR>:e<CR>

Explanation: :w - save file :silent - avoid pressing enter at the end % - passes the file to gofmt -w=true - tells gofmt to write back to the file :e - tells vim to reload modified file

sergeant