Presuming that yapf doesn't require the whole buffer in order to be able to reformat lines 30-40, an easier way to achieve your goal is to use Vim'sVim provides a filter
feature aimed at this sort of problem. This allows you to pass content from Vim into an external tool, and then replace thethat content you passed with the tool's output.
The documentation can be found at :help filter
, but your goal might* be as simple to achievereplace the entire contents of your file with the output of your yapf
command you can use the command:
:%!yapf -l 30-40
For commands that don't require the entire content of the buffer as input, you can use a range with a filter (See :help :range!
). e.g.:
:30,40!yapf
* I say "might" because I don't have yapf installed so haven't tested this precise command. You might need to tweak the command slightly if yapf requires further arguments. See :h :range!
for the specific form I'm using above.