Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vimscript: how to get current filetype as a variable

Tags:

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.

For example:

  • editing "foo/bar.txt", want to open "tmp/other.txt"
  • editing "foo/bar.cpp", want to open "tmp/other.cpp"

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.

like image 641
aweg awef Avatar asked Feb 05 '13 21:02

aweg awef


1 Answers

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.

like image 171
Prince Goulash Avatar answered Oct 04 '22 15:10

Prince Goulash