3

:Rename (from vim-rename) fails with Python files in my current set up with the following error:

line   46:
E486: Pattern not found: \s\+$

if the file does not contain trailing whitespaces in at least one line, leading to the file being duplicated (with the new name and the old one).

Using the following file helloworld.py (note there are no trailing whitespaces):

print("Hello, world!")

If I attempt to use :Rename new_name.py it will spew out the previously-mentioned error, and I'll end up with two files (helloworld.py and new_name.py). If, however, I add whitespaces at the end of the line, the renaming will succeed and the trailing whitespaces are automatically removed.

This seems to be due to some sort of conflict between vim-rename and python-mode, since it only happens with Python files.

After debugging what happens on a write with :debug write, I can see that pymode#trim_whitespaces() is invoked by default by python-mode. If I disable it by let g:pymode_trim_whitespaces=0, the problem with vim-rename goes away. However, automatic trimming of trailing whitespaces is a feature I don't want to lose.

My questions, in order of importance:

  1. How can I fix this so that I can rename files and keep the automatic trimming of whitespaces?
  2. Why is the non-existence of whitespaces causing rename to fail?

I would prefer to keep vim-rename, although I'm willing to switch it for a more robust plugin if necessary. Fixing the code in vim-rename manually would also be acceptable.

If it is of any use, this is my complete vimrc file.

  • 1
    As you said the problem comes from somewhere in your vimrc which makes your question pretty much a duplicate of How do I debug my vimrc – statox Sep 20 '17 at 8:19
  • @statox Thanks for the suggestion. I have debugged my vimrc and confirmed my suspicions, but knowing what the problem is doesn't mean I know how to fix it, so I don't think it's a duplicate. I'll update the answer, though. – user2891462 Sep 20 '17 at 9:18
  • If you found which line of configuration or which plugin creates the error message then you can indeed edit your question so that we can help you with this particular line/plugin, otherwise your question is still about debugging your vimrc. – statox Sep 20 '17 at 9:26
  • 1
    As a workaround you can try github.com/tpope/vim-eunuch plugin, which has Rename as well (but please see README for difference between Rename and Move) – grodzik Sep 20 '17 at 9:45
  • 2
    I'm the author of Rename.vim (although there are forks of it out there). As @Rich mentions in their answer, this is an issue with the BufWritePre autocmd and function that the python-mode plugin defines, and the suggestion to add an e flag to the substitute command will fix it. – Heptite Sep 20 '17 at 16:37
4

I'm not clear on why the problem occurs — the line in pymode#trim_whitespaces() that is triggering the error includes a silent! so the error should be ignored. You could maybe try appending an e flag to ignore the errors more specifically:

silent! %s/\s\+$//e

If that works, you should probably report it to the author of python-mode.

However, I'm not too motivated to dig any deeper into what's going wrong, because there's a very simple workaround, which is to write a new command which turns off "trim whitespaces" before running the rename and then restores it afterwards.

function! MyRename(new_name, bang)
  if exists("g:pymode_trim_whitespaces")
    let orig_trim_whitespaces = g:pymode_trim_whitespaces
  endif
  let g:pymode_trim_whitespaces = 0

  execute "Rename" . a:bang . " " . a:new_name

  if exists("orig_trim_whitespaces")
    let g:pymode_trim_whitespaces = orig_trim_whitespaces
  else
    unlet g:pymode_trim_whitespaces
  endif
endfunction

command! -nargs=* -complete=file -bang MyRename call MyRename(<q-args>, '<bang>')

You've also indicated that you're happy to edit the code of vim-rename, so an even quicker (but even more hacky) fix would be simply to add the lines that set and reset g:pymode_trim_whitespaces at appropriate places in the Rename() function itself. (i.e. just before and after the line that performs the rename (silent! exe 'saveas' . a:bang . ' ' . l:name).

  • 2
    I have opened an issue on python-mode with your suggested fix. – Heptite Sep 20 '17 at 17:03
  • Thank you to both :) I have switched to tpope/vim-eunuch, which works fine, but this answer gives a solution that keeps the same plugin and explains what goes wrong, so accepted! – user2891462 Sep 21 '17 at 7:44
  • 1
    For future reference, the issue is github.com/python-mode/python-mode/issues/797 and it has been resolved :D – user2891462 Dec 15 '17 at 7:13

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.