如何在VIM中为文件设置正确的路径? [英] How to set the correct path for a file in VIM?
问题描述
每当我在vim中按下:pwd
时,即使我在桌面上的Python文件中,命令也始终返回路径C:\Windows\system32
.因此,每当我运行:!python %
时,命令都会返回
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.
但是,如果我使用命令:cd %:p:h
设置路径,然后运行相同的python命令,则Python文件将正确执行.所以基本上我想知道如何让vim正确设置打开的每个文件的路径.
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.
(即,如果我位于桌面:pwd
的文件中,则返回~\Desktop\
,或者如果我位于主目录:pwd
的文件中,则返回C:\Users\MyName\
)
(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\
).
推荐答案
您可以设置autochdir
:
:set autochdir
通过此设置,当前工作目录将跟随您正在编辑的文件.
With this setting the current working directory will follow the file that you're editing.
请参见:help 'autochdir'
,尤其是此注释:
See :help 'autochdir'
, and particularly this note:
注意:启用此选项后,某些插件可能无法正常工作.
Note: When this option is on some plugins may not work.
这篇关于如何在VIM中为文件设置正确的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!