I set up this function in my .vimrc file. It duplicates the current window, moves it to a new tab, read
s the output of yapf (a code formatter) run on the current file, sets up a diff between the original file and the formatted file, and selects the original file window. Then I proceed to merge the format changes I like by using ]c
and [c
to navigate the changes and do
/dp
to accept the formatted change or keep the original.
Here's the function I use:
function! Yapf()
exe "normal \<C-w>s\<C-w>T"
vnew
setlocal buftype=nofile
read !yapf #
windo diffthis
wincmd p
endfunction
(I came up with this with help from answers to a previous question)
But I tend to run this on scripts that start with a shebang line, and it always seemed to suggest a blank line at the start of the file (which would break the shebang behaviour). E.g. I created a new file with the following content:
#!/usr/bin/python2
print "hi"
Then calling call Yapf()
in vim, the resulting diff shows a blank line at the top of the output of the yapf call.
Calling yapf from the command line directly on the file produces an output with no extra initial line.
Is this extra line coming from how I am using read
? How can I fix my function to avoid this issue?