2

I'm trying to do the following:

autocmd BufWritePost *.py :!isort % | :edit

The isort command in this case is writing to the buffer and so I'd like to reload the buffer to see the changes to the file I'm currently viewing.

But i find that because I've triggered a shell command ! the pipe is interpreted as part of the shell command and not as part of a chained vim Ex command.

Is there a way to do this?

I tried grouping the shell command so it was separate from the pipe/Ex command (In my head I imagined some magic making this work sort of like a subprocess):

autocmd BufWritePost *.py :(!isort %) | :edit

But that was invalid.

Thanks.

4

The help for :! says:

A '|' in {cmd} is passed to the shell, you cannot use
it to append a Vim command.  See |:bar|.

The usual workaround is to use :execute, which can be followed by |:

:execute '!isort %' | edit

One last note, though: it is possible to use the :! form as a filter (see :help :range!), meaning I would use the following code instead:

:%!isort

This runs isort with stdin as the contents of the buffer. It replaces the buffer with the output of the command.

| improve this answer | |
  • 1
    isort only sorts Python import statements (according to specific rules) and not sort the whole contents of the file... So built-in sort is not really a replacement here. – filbranden Jan 27 at 15:31

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.