vim:应用autocmd时,新文件的目录与当前工作目录不匹配

时间:2022-09-07 16:59:11

I'm starting using vim to edit my python code and I want vim to save codes under different folders according to different suffixes like '.py'.

我开始使用vim来编辑我的python代码,我希望vim根据不同的后缀(如'.py')将代码保存在不同的文件夹下。

So, I added this into my gvimrc file (I'm using MacVim indeed, but the same problem appears on vim)

所以,我把它添加到我的gvimrc文件中(我确实使用的是MacVim,但vim上出现了同样的问题)

au BufWritePre *.py :cd /Users/username/Documents/folder

I think this changes the working directory into /Users/username/Documents/folder every time before I save my code ending in '.py'.

我认为每次保存以“.py”结尾的代码之前,这会将工作目录更改为/ Users / username / Documents /文件夹。

While I entered MacVim, opened a new file, typed something like

当我进入MacVim时,打开一个新文件,输入类似的内容

print("Hello")

and saved it by

并保存

:w hello_world.py

A file named hello_world.py appeared in /Users/username/Documents/folder, as I wished. The ":pwd" command in vim also returned "/Users/username/Documents/folder". But when I check the filename of hello_world.py in vim by

正如我所希望的那样,一个名为hello_world.py的文件出现在/ Users / username / Documents /文件夹中。 vim中的“:pwd”命令也返回“/ Users / username / Documents / folder”。但是当我在vim中检查hello_world.py的文件名时

:!echo %

it returns

它返回

/Users/username/hello_world.py

instead of the expected filename

而不是预期的文件名

/Users/username/Documents/folder/hello_world.py # expected filename

Further more, When I tried to upgrade the code like

此外,当我尝试升级代码时

print("Hello")
print("World")

and saved it by

并保存

:w

The content in hello_world.py remained unchanged under /Users/username/Documents/folder. But a new file with the same hello_world.py name appeared in the /Users/username, which is consistent with the filename in vim.

hello_world.py中的内容在/ Users / username / Documents /文件夹下保持不变。但是/ Users / username中出现了一个具有相同hello_world.py名称的新文件,这与vim中的文件名一致。

I guess my autocmd script in gvimrc did changed the working directory before saving '.py' files, but I'm trying to figure out why the filename is not consistent with the working directory and how can I save my '.py' file into a specific folder.

我想我在gvimrc中的autocmd脚本在保存'.py'文件之前确实改变了工作目录,但是我想弄清楚为什么文件名与工作目录不一致以及如何将'.py'文件保存到特定文件夹。

1 个解决方案

#1


0  

For me (using Vim 7.4.52 on Ubuntu 14.04), the file consistently get created in the CWD, so the autocmd has no effect to file placement, but changes the directory. Internally, the file's eventual directory is determined when the buffer is created (as evidenced by :ls), not during the first write or naming of the buffer.

对我来说(在Ubuntu 14.04上使用Vim 7.4.52),文件始终在CWD中创建,因此autocmd对文件放置没有影响,但会更改目录。在内部,文件的最终目录在创建缓冲区时确定(由ls证明),而不是在缓冲区的第一次写入或命名期间。

I only managed to get closer to what you want via

我只是设法靠近你想要的东西

:au BufWritePost *.py file /Users/username/Documents/folder/%:t

which still creates the initial file in the CWD, though.

但仍然会在CWD中创建初始文件。

If you think Vim should handle this (better), you can try raising the issue on the vim_dev mailing list.

如果您认为Vim应该处理(更好),您可以尝试在vim_dev邮件列表上提出问题。

#1


0  

For me (using Vim 7.4.52 on Ubuntu 14.04), the file consistently get created in the CWD, so the autocmd has no effect to file placement, but changes the directory. Internally, the file's eventual directory is determined when the buffer is created (as evidenced by :ls), not during the first write or naming of the buffer.

对我来说(在Ubuntu 14.04上使用Vim 7.4.52),文件始终在CWD中创建,因此autocmd对文件放置没有影响,但会更改目录。在内部,文件的最终目录在创建缓冲区时确定(由ls证明),而不是在缓冲区的第一次写入或命名期间。

I only managed to get closer to what you want via

我只是设法靠近你想要的东西

:au BufWritePost *.py file /Users/username/Documents/folder/%:t

which still creates the initial file in the CWD, though.

但仍然会在CWD中创建初始文件。

If you think Vim should handle this (better), you can try raising the issue on the vim_dev mailing list.

如果您认为Vim应该处理(更好),您可以尝试在vim_dev邮件列表上提出问题。