Whenever I hit :pwd in vim the command always returns the path C:\Windows\system32, even if I'm in a Python file from the desktop. So whenever I run :!python % the command returns
python: can't open file '\Users\myname': [Errno 2] No such file or directory.
But if I set the path with the command :cd %:p:h and then run the same python command the Python file executes correctly. So basically I'm wondering how do I get vim to correctly set the path for every file that I open.
(i.e. if I'm in a file located at the desktop :pwd returns ~\Desktop\ or if I'm in a file in the home directory :pwd returns C:\Users\MyName\).
解决方案
You can set autochdir:
:set autochdir
With this setting the current working directory will follow the file that you're editing.
See :help 'autochdir', and particularly this note:
Note: When this option is on some plugins may not work.