Question

I use the following mapping to run the python interpreter when I press F5:

autocmd Filetype python nnoremap <buffer> <F5> <C-o>:update<Bar>execute '!python '.shellescape(@%, 1)<CR>

Occasionally though the current file variable is expanded to something like "NERD_tree_1..." which of course causes this error:

powershell -command "python NERD_tree_1p"
C:\Python27\python.exe: can't open file 'NERD_tree_1p': [Errno 2] No such file o
r directory
shell returned 1
Hit any key to close this window...

Not sure how nerd tree is causing this problem or how to fix it.

Was it helpful?

Solution

The register % contains the current buffer name. Somehow, you seem to be in the scratch buffer that NERDTree uses to display its directory tree when the error occurs.

You should have noticed that the cursor is in the "wrong" (i.e. non-Python) buffer. If this happens in a Python buffer, I cannot explain that.

You can make your mapping more robust to include a check for Python filetype, like this:

... if &filetype ==# 'python'<Bar>update<Bar>execute '!python '.shellescape(@%, 1)<Bar>endif<CR>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top