I'm trying to find the name of the file I'm editing inside of Vim. So I can use it to map F5 to compile this file. Would of course be great if I could recognize the file format, and choose compiler accordingly, but really not necessary. If I find the name of the file, I could do that myself. But I really can't find any way to get the name of the file I'm editing.
我正在尝试在Vim中找到我正在编辑的文件的名称。所以我可以用它来映射F5来编译这个文件。如果我能识别文件格式并选择相应的编译器,那当然会很棒,但实际上并不是必需的。如果我找到文件的名称,我可以自己做。但我真的找不到任何方法来获取我正在编辑的文件的名称。
I know of the :make
command, and have already mapped that, but for small scripts/testing programs, I really don't want to first have to write a simple makefile.
我知道:make命令,并且已经映射过,但对于小脚本/测试程序,我真的不想先写一个简单的makefile。
6 个解决方案
#1
I'm trying to find the name of the file I'm editing inside of vim.
我正在尝试在vim中找到我正在编辑的文件的名称。
For the current buffer this will give you name of the file,
对于当前缓冲区,这将为您提供文件的名称,
echo "You're editing " bufname("%")
(just put it in some file.vim, and source it ":so %". But I don't think this is what you need.
(只需将它放在一些file.vim中,然后将其命名为“:so%”。但我认为这不是你所需要的。
So I can use it to map F5 to compile this file, for testing purposes. Would of course be great if I could recognize the file format, and choose compiler accordingly, but really not necessary. If I find the name of the file, I could do that myself. But I really can't find any way to get the name of the file I'm editing.
所以我可以用它来映射F5来编译这个文件,用于测试目的。如果我能识别文件格式并选择相应的编译器,那当然会很棒,但实际上并不是必需的。如果我找到文件的名称,我可以自己做。但我真的找不到任何方法来获取我正在编辑的文件的名称。
You could do several things. If vim recognizes the filetype you're editing, you could map F5 to your compiler command and put that command in its specific ftplugin directory, so it will be valid only for that filetype. For example, you put
你可以做几件事。如果vim识别出您正在编辑的文件类型,则可以将F5映射到编译器命令并将该命令放在其特定的ftplugin目录中,因此它仅对该文件类型有效。例如,你把
nmap <f5> :!compilername %<cr>
in cpp.vim, fortran.vim, python.vim, etc. The %
gives the name of the current file you're editing. This will execute the compiler just like if you called compilername file.cpp
in the command prompt.
在cpp.vim,fortran.vim,python.vim等中。%给出了您正在编辑的当前文件的名称。这将执行编译器,就像在命令提示符中调用compilername file.cpp一样。
Of course, compilername
will be different for each filetype; you're not going to compile fortran with cpp compiler, you put fortrancompiler %
in that case.
当然,每个文件类型的编译器名称都不同;你不打算用cpp编译器编译fortran,在这种情况下你把fortrancompiler%。
That is one option. You could also try according to extension set autocmd commands in your .vimrc
so it recognizes the filetype according to extension of the file. This is all standard usage of vim, nothing uncommon. Now, I'm not sure how you'd like to go about this, so I'll just point you to Vim Wiki where you can find all kinds of articles that cover this (and also a few tips for some compilers).
这是一种选择。您还可以根据.vimrc中的扩展集autocmd命令进行尝试,以便根据文件的扩展名识别文件类型。这是vim的所有标准用法,没有什么不常见的。现在,我不确定你是怎么想这样做的,所以我只想指出Vim Wiki,在那里你可以找到涵盖这个的各种文章(以及一些编译器的一些技巧)。
I know of the
:make
command, and have already mapped that, but for small scripts/testing programs, I really don't want to first have to write a simple makefile.我知道:make命令,并且已经映射过,但对于小脚本/测试程序,我真的不想先写一个简单的makefile。
Yes, that seems like overkill. Personally (when I'm on Windows), I find simple batch file much easier to write for most things except big projects. If the program is in one or two files, I just compile it using some oneliner mapping.
是的,这似乎有点矫枉过正。就个人而言(当我在Windows上时),我发现简单的批处理文件比大项目更容易编写。如果程序在一个或两个文件中,我只是使用一些oneliner映射来编译它。
#2
You can use the % character in vim commands to get the current filename with the extension. For example,
您可以使用vim命令中的%字符来获取扩展名的当前文件名。例如,
:!javac %
to run javac on the current file.
在当前文件上运行javac。
You can find out more with
你可以找到更多
:help filename-modifiers
#3
The other points are very useful. I would add that you probably want to use :p, to ensure that the file name is passed as a fully qualified path. Without it, Vim may pass it as a path that is relative to Vim's current path, which may or may not be the same path as the compiler uses. So one solution is:
其他要点非常有用。我想补充一点,你可能想使用:p,以确保文件名作为完全限定的路径传递。没有它,Vim可以将其作为相对于Vim当前路径的路径传递,该路径可能与编译器使用的路径相同,也可能不同。所以一个解决方案是:
nnoremap <silent> <f5> :!javac %:p<cr>
If you want to detect the file type automatically you could use AutoCommand
如果要自动检测文件类型,可以使用AutoCommand
autocmd FileType java nnoremap <buffer> <silent> <f5> :!javac %:p<cr>
autocmd FileType cpp nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>
#4
As others have already mentioned, %
is expanded to the current file. If you want to get that string in vim script, use expand("%")
.
正如其他人已经提到的那样,%被扩展到当前文件。如果要在vim脚本中获取该字符串,请使用expand(“%”)。
But you may want to simply set makeprg to something like "compiler-command\ %"
and run :make
- you gain quick-fix support that way. (open errors/warnings window with :copen
)
但您可能只想将makeprg设置为类似“compiler-command \%”的内容并运行:make - 您可以通过这种方式获得快速修复支持。 (打开错误/警告窗口:copen)
#5
Well, it's % as a substitution code inside things like :w. So :w blah% writes a copy of your file with 'blah' prepended. Or !!echo % replaces the current line with it. Dunno if that addresses your needs or not.
好吧,它是%作为替换代码,如:w。所以:w blah%写了一个文件的副本,前面加上'blah'。或者!! echo%用它替换当前行。 Dunno,如果它满足您的需求。
#6
Use %
as a substitute for your filename. But personally, I really think you can push :make
to do exactly what you want.
使用%替换文件名。但就个人而言,我真的认为你可以推动:做到你想做的事情。
I used to do exactly what you want to avoid: just hack out a two-line Makefile for every single folder I was in. I enjoyed :make
's quickfix support so much that whipping out a two-line Makefile just seemed like a small overhead in the big picture.
我曾经做过你想要避免的事情:只为我所在的每一个文件夹中的两行Makefile。我很喜欢:make的quickfix支持非常多,以至于甩掉两行Makefile似乎只是一个很小的开销。大局。
... I've since learned better. You can use an autocmd or ftplugin to set the makeprg
option, configuring :make
to use your command of choice based on the current filetype. In itself, that saves you a bit of work.
......我从那时起就学得更好了。您可以使用autocmd或ftplugin设置makeprg选项,配置:make根据当前文件类型使用您选择的命令。就其本身而言,这可以为您节省一些工作。
The problem starts when we work a tool whose error messages don't look just like GCC's. To get around this, vim also provides the errorformat
option. errorformat
is very sophisticated. If you really want to learn about it, start with :h error-format
. In the long term, the sophistication is really great. It means that, once you learn how to use it, you can parse virtually any compiler output and format it for viewing with quickfix (:cope
or :cl
).
当我们使用一个错误消息看起来不像GCC的工具时,问题就出现了。为了解决这个问题,vim还提供了errorformat选项。 errorformat非常复杂。如果您真的想了解它,请从:h error-format开始。从长远来看,复杂性非常好。这意味着,一旦您学会了如何使用它,您几乎可以解析任何编译器输出并对其进行格式化以便使用quickfix(:cope或:cl)进行查看。
In the short term, it can be a bit of a bad thing, because it means that the first couple of times you hack an error format together yourself, you're going to be spending a fair bit of time puzzling over the vimdocs and re-loading and building files that will trigger the various error messages your compiler can produce. That's definitely not what you want if you're only briefly working outside your comfort zone, or your boss is over your shoulder urging you to fix this bug now.
在短期内,它可能有点不好,因为这意味着你自己一起破解错误格式的前几次,你将花费相当多的时间来困扰vimdocs和re - 加载和构建将触发编译器可以生成的各种错误消息的文件。如果你只是在你的舒适区之外短暂工作,或者你的老板在你的肩膀上敦促你现在解决这个问题,那绝对不是你想要的。
Hopefully (and likely), someone has already gone through this pain for you, and either configured errorformat
appropriately for your default ftplugin
, or released it as part of a vim script that they're just waiting for the community to install and adore.
希望(有可能),有人已经为您解决了这个问题,并为您的默认ftplugin配置了errorformat,或者将其作为vim脚本的一部分发布,他们只是等待社区安装和崇拜。
As a quick demonstration, here's a couple of lines I hacked together for my .vim/ftplugin/haskell.vim
file:
作为一个快速演示,这里有几行我一起攻击我的.vim / ftplugin / haskell.vim文件:
setl makeprg=ghc\ --make\ %
setl errorformat=%E%f:%l:%c:,%C\ %.%m,%Z,%f:%l:%c:%m
I use setl
here so that these options are only configured for haskell source files; if I edit a different kind of source file simultaneously, it can use a different configuration instead.
我在这里使用setl,以便只为haskell源文件配置这些选项;如果我同时编辑不同类型的源文件,它可以使用不同的配置。
If you end up using :make
, I also recommend configuring your switchbuf
setting. I found it a bit annoying having to press ctrl-O after a build if quickfix gets excited and jumps you to the wrong file. I use:
如果您最终使用:make,我还建议您配置您的switchbuf设置。我发现在构建之后按ctrl-O有点烦人如果quickfix被激发并跳转到错误的文件。我用:
set switchbuf=useopen,usetab,newtab
#1
I'm trying to find the name of the file I'm editing inside of vim.
我正在尝试在vim中找到我正在编辑的文件的名称。
For the current buffer this will give you name of the file,
对于当前缓冲区,这将为您提供文件的名称,
echo "You're editing " bufname("%")
(just put it in some file.vim, and source it ":so %". But I don't think this is what you need.
(只需将它放在一些file.vim中,然后将其命名为“:so%”。但我认为这不是你所需要的。
So I can use it to map F5 to compile this file, for testing purposes. Would of course be great if I could recognize the file format, and choose compiler accordingly, but really not necessary. If I find the name of the file, I could do that myself. But I really can't find any way to get the name of the file I'm editing.
所以我可以用它来映射F5来编译这个文件,用于测试目的。如果我能识别文件格式并选择相应的编译器,那当然会很棒,但实际上并不是必需的。如果我找到文件的名称,我可以自己做。但我真的找不到任何方法来获取我正在编辑的文件的名称。
You could do several things. If vim recognizes the filetype you're editing, you could map F5 to your compiler command and put that command in its specific ftplugin directory, so it will be valid only for that filetype. For example, you put
你可以做几件事。如果vim识别出您正在编辑的文件类型,则可以将F5映射到编译器命令并将该命令放在其特定的ftplugin目录中,因此它仅对该文件类型有效。例如,你把
nmap <f5> :!compilername %<cr>
in cpp.vim, fortran.vim, python.vim, etc. The %
gives the name of the current file you're editing. This will execute the compiler just like if you called compilername file.cpp
in the command prompt.
在cpp.vim,fortran.vim,python.vim等中。%给出了您正在编辑的当前文件的名称。这将执行编译器,就像在命令提示符中调用compilername file.cpp一样。
Of course, compilername
will be different for each filetype; you're not going to compile fortran with cpp compiler, you put fortrancompiler %
in that case.
当然,每个文件类型的编译器名称都不同;你不打算用cpp编译器编译fortran,在这种情况下你把fortrancompiler%。
That is one option. You could also try according to extension set autocmd commands in your .vimrc
so it recognizes the filetype according to extension of the file. This is all standard usage of vim, nothing uncommon. Now, I'm not sure how you'd like to go about this, so I'll just point you to Vim Wiki where you can find all kinds of articles that cover this (and also a few tips for some compilers).
这是一种选择。您还可以根据.vimrc中的扩展集autocmd命令进行尝试,以便根据文件的扩展名识别文件类型。这是vim的所有标准用法,没有什么不常见的。现在,我不确定你是怎么想这样做的,所以我只想指出Vim Wiki,在那里你可以找到涵盖这个的各种文章(以及一些编译器的一些技巧)。
I know of the
:make
command, and have already mapped that, but for small scripts/testing programs, I really don't want to first have to write a simple makefile.我知道:make命令,并且已经映射过,但对于小脚本/测试程序,我真的不想先写一个简单的makefile。
Yes, that seems like overkill. Personally (when I'm on Windows), I find simple batch file much easier to write for most things except big projects. If the program is in one or two files, I just compile it using some oneliner mapping.
是的,这似乎有点矫枉过正。就个人而言(当我在Windows上时),我发现简单的批处理文件比大项目更容易编写。如果程序在一个或两个文件中,我只是使用一些oneliner映射来编译它。
#2
You can use the % character in vim commands to get the current filename with the extension. For example,
您可以使用vim命令中的%字符来获取扩展名的当前文件名。例如,
:!javac %
to run javac on the current file.
在当前文件上运行javac。
You can find out more with
你可以找到更多
:help filename-modifiers
#3
The other points are very useful. I would add that you probably want to use :p, to ensure that the file name is passed as a fully qualified path. Without it, Vim may pass it as a path that is relative to Vim's current path, which may or may not be the same path as the compiler uses. So one solution is:
其他要点非常有用。我想补充一点,你可能想使用:p,以确保文件名作为完全限定的路径传递。没有它,Vim可以将其作为相对于Vim当前路径的路径传递,该路径可能与编译器使用的路径相同,也可能不同。所以一个解决方案是:
nnoremap <silent> <f5> :!javac %:p<cr>
If you want to detect the file type automatically you could use AutoCommand
如果要自动检测文件类型,可以使用AutoCommand
autocmd FileType java nnoremap <buffer> <silent> <f5> :!javac %:p<cr>
autocmd FileType cpp nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>
#4
As others have already mentioned, %
is expanded to the current file. If you want to get that string in vim script, use expand("%")
.
正如其他人已经提到的那样,%被扩展到当前文件。如果要在vim脚本中获取该字符串,请使用expand(“%”)。
But you may want to simply set makeprg to something like "compiler-command\ %"
and run :make
- you gain quick-fix support that way. (open errors/warnings window with :copen
)
但您可能只想将makeprg设置为类似“compiler-command \%”的内容并运行:make - 您可以通过这种方式获得快速修复支持。 (打开错误/警告窗口:copen)
#5
Well, it's % as a substitution code inside things like :w. So :w blah% writes a copy of your file with 'blah' prepended. Or !!echo % replaces the current line with it. Dunno if that addresses your needs or not.
好吧,它是%作为替换代码,如:w。所以:w blah%写了一个文件的副本,前面加上'blah'。或者!! echo%用它替换当前行。 Dunno,如果它满足您的需求。
#6
Use %
as a substitute for your filename. But personally, I really think you can push :make
to do exactly what you want.
使用%替换文件名。但就个人而言,我真的认为你可以推动:做到你想做的事情。
I used to do exactly what you want to avoid: just hack out a two-line Makefile for every single folder I was in. I enjoyed :make
's quickfix support so much that whipping out a two-line Makefile just seemed like a small overhead in the big picture.
我曾经做过你想要避免的事情:只为我所在的每一个文件夹中的两行Makefile。我很喜欢:make的quickfix支持非常多,以至于甩掉两行Makefile似乎只是一个很小的开销。大局。
... I've since learned better. You can use an autocmd or ftplugin to set the makeprg
option, configuring :make
to use your command of choice based on the current filetype. In itself, that saves you a bit of work.
......我从那时起就学得更好了。您可以使用autocmd或ftplugin设置makeprg选项,配置:make根据当前文件类型使用您选择的命令。就其本身而言,这可以为您节省一些工作。
The problem starts when we work a tool whose error messages don't look just like GCC's. To get around this, vim also provides the errorformat
option. errorformat
is very sophisticated. If you really want to learn about it, start with :h error-format
. In the long term, the sophistication is really great. It means that, once you learn how to use it, you can parse virtually any compiler output and format it for viewing with quickfix (:cope
or :cl
).
当我们使用一个错误消息看起来不像GCC的工具时,问题就出现了。为了解决这个问题,vim还提供了errorformat选项。 errorformat非常复杂。如果您真的想了解它,请从:h error-format开始。从长远来看,复杂性非常好。这意味着,一旦您学会了如何使用它,您几乎可以解析任何编译器输出并对其进行格式化以便使用quickfix(:cope或:cl)进行查看。
In the short term, it can be a bit of a bad thing, because it means that the first couple of times you hack an error format together yourself, you're going to be spending a fair bit of time puzzling over the vimdocs and re-loading and building files that will trigger the various error messages your compiler can produce. That's definitely not what you want if you're only briefly working outside your comfort zone, or your boss is over your shoulder urging you to fix this bug now.
在短期内,它可能有点不好,因为这意味着你自己一起破解错误格式的前几次,你将花费相当多的时间来困扰vimdocs和re - 加载和构建将触发编译器可以生成的各种错误消息的文件。如果你只是在你的舒适区之外短暂工作,或者你的老板在你的肩膀上敦促你现在解决这个问题,那绝对不是你想要的。
Hopefully (and likely), someone has already gone through this pain for you, and either configured errorformat
appropriately for your default ftplugin
, or released it as part of a vim script that they're just waiting for the community to install and adore.
希望(有可能),有人已经为您解决了这个问题,并为您的默认ftplugin配置了errorformat,或者将其作为vim脚本的一部分发布,他们只是等待社区安装和崇拜。
As a quick demonstration, here's a couple of lines I hacked together for my .vim/ftplugin/haskell.vim
file:
作为一个快速演示,这里有几行我一起攻击我的.vim / ftplugin / haskell.vim文件:
setl makeprg=ghc\ --make\ %
setl errorformat=%E%f:%l:%c:,%C\ %.%m,%Z,%f:%l:%c:%m
I use setl
here so that these options are only configured for haskell source files; if I edit a different kind of source file simultaneously, it can use a different configuration instead.
我在这里使用setl,以便只为haskell源文件配置这些选项;如果我同时编辑不同类型的源文件,它可以使用不同的配置。
If you end up using :make
, I also recommend configuring your switchbuf
setting. I found it a bit annoying having to press ctrl-O after a build if quickfix gets excited and jumps you to the wrong file. I use:
如果您最终使用:make,我还建议您配置您的switchbuf设置。我发现在构建之后按ctrl-O有点烦人如果quickfix被激发并跳转到错误的文件。我用:
set switchbuf=useopen,usetab,newtab