Autocmd bufnewfile 在相对路径上导致 "Trailing characters" 错误
Autocmd bufnewfile causing "Trailing characters" error on relative paths
我的 .vimrc 文件有问题。我已经完成了所有 python 和 sh 文件的自动命令。我在下面都包括了。使用直接路径时,一切都按预期工作,即:
gvim test.py
如果我使用相对于cwd的路径,例如:
gvim ../test.py
我收到以下错误:
Error detected while processing BufNewFile Auto commands for "*.{py,sh}"
E488: Trailing characters
关于如何解决这个问题有什么想法吗?
autocmd bufnewfile *.{py,sh}
\ let path = expand("~/bin/Templates/")|
\ let extension = expand("%:e")|
\ let template = path . extension|
\ let name = "John Doe" |
\ if filereadable(template)|
\ execute "silent! 0r" . template|
\ execute "1," . 10 . "g/# File Name:.*/s//# File Name: " .expand("%")|
\ execute "1," . 10 . "g/# Creation Date:.*/s//# Creation Date: " .strftime("%b-%d-%Y")|
\ execute "1," . 10 . "g/Created By:.*/s//Created By: " . name|
\ execute "normal Gdd/CURSOR\<CR>dw"|
\ endif|
\ startinsert!
autocmd bufwritepre,filewritepre *.{py,sh}
\ execute "normal ma"|
\ execute "1," . 10 . "g/# Last Modified:.*/s/# Last Modified:.*/# Last Modified: "
\ .strftime("%b-%d-%Y")
autocmd bufwritepost,filewritepost *.{py,sh}
\ execute "normal 'a"
python个文件的模板如下:
#!/usr/bin/python
# File Name: <filename>
# Creation Date: <date>
# Last Modified: <N/A>
# Created By: <Name>
# Description: CURSOR
首先我们来看一下:help 10.2
:
The general form of the `:substitute` command is as follows:
:[range]substitute/from/to/[flags]
请牢记/[flags]
。现在当你在命令行中输入gvim test.py
时,在Vim中执行如下命令:
:s//# File Name: test.py
但是当你输入gvim ../test.py
时Vim执行:
:s//# File Name: ../test.py
所以 Vim 使用 test.py
作为 :substitute
的标志,这不是我们想要的行为。
您需要的是将expand("%")
替换为expand("%:t")
以仅获取文件名。有关详细信息,请参阅 :help expand()
。
我的 .vimrc 文件有问题。我已经完成了所有 python 和 sh 文件的自动命令。我在下面都包括了。使用直接路径时,一切都按预期工作,即:
gvim test.py
如果我使用相对于cwd的路径,例如:
gvim ../test.py
我收到以下错误:
Error detected while processing BufNewFile Auto commands for "*.{py,sh}"
E488: Trailing characters
关于如何解决这个问题有什么想法吗?
autocmd bufnewfile *.{py,sh}
\ let path = expand("~/bin/Templates/")|
\ let extension = expand("%:e")|
\ let template = path . extension|
\ let name = "John Doe" |
\ if filereadable(template)|
\ execute "silent! 0r" . template|
\ execute "1," . 10 . "g/# File Name:.*/s//# File Name: " .expand("%")|
\ execute "1," . 10 . "g/# Creation Date:.*/s//# Creation Date: " .strftime("%b-%d-%Y")|
\ execute "1," . 10 . "g/Created By:.*/s//Created By: " . name|
\ execute "normal Gdd/CURSOR\<CR>dw"|
\ endif|
\ startinsert!
autocmd bufwritepre,filewritepre *.{py,sh}
\ execute "normal ma"|
\ execute "1," . 10 . "g/# Last Modified:.*/s/# Last Modified:.*/# Last Modified: "
\ .strftime("%b-%d-%Y")
autocmd bufwritepost,filewritepost *.{py,sh}
\ execute "normal 'a"
python个文件的模板如下:
#!/usr/bin/python
# File Name: <filename>
# Creation Date: <date>
# Last Modified: <N/A>
# Created By: <Name>
# Description: CURSOR
首先我们来看一下:help 10.2
:
The general form of the `:substitute` command is as follows:
:[range]substitute/from/to/[flags]
请牢记/[flags]
。现在当你在命令行中输入gvim test.py
时,在Vim中执行如下命令:
:s//# File Name: test.py
但是当你输入gvim ../test.py
时Vim执行:
:s//# File Name: ../test.py
所以 Vim 使用 test.py
作为 :substitute
的标志,这不是我们想要的行为。
您需要的是将expand("%")
替换为expand("%:t")
以仅获取文件名。有关详细信息,请参阅 :help expand()
。