Vimscript:如何获取当前文件类型作为变量 [英] Vimscript: how to get current filetype as a variable

查看:36
本文介绍了Vimscript:如何获取当前文件类型作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 vimscript 中获取当前文件类型作为变量.

I'd like to get the current filetype as a variable in vimscript.

我正在制作一个获取当前文件类型并编辑相应文件类型的另一个文件的函数.

I'm making a function that grabs the current filetype and edits another file of corresponding filetype.

例如:

  • 正在编辑foo/bar.txt",想打开tmp/other.txt"
  • 编辑foo/bar.cpp",想打开tmp/other.cpp"

等等.我知道 :set ft? 在 vi​​m 中显示文件类型,但我不确定如何捕获它,然后使用它作为新文件字符串的一部分打开另一个文件.

etc. I know that :set ft? displays the filetype in vim, but I'm not sure how to capture it and then open another file using it as part of the new file string.

推荐答案

您可以通过在设置前加上与号来访问设置的值.要将文件类型分配给变量,以下是等效的:

You can access the value of a setting by prefixing it with an ampersand. To assign the filetype to a variable, the following are equivalent:

let my_filetype = &filetype
let my_filetype = &ft

因此,对于您的示例,假设已设置当前缓冲区的文件类型,您可以执行类似的操作

So for your example, assuming the filetype of the current buffer has been set, you could do something like

execute 'edit tmp/other.' . &filetype

请注意,您需要执行表达式,以便在连接字符串之前扩展变量.

Note that you need to execute the expression so that the variable is expanded before the strings are concatenated.

这篇关于Vimscript:如何获取当前文件类型作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆