Vimscript: how to get current filetype as a variable

前端 未结 2 1794
一整个雨季
一整个雨季 2021-02-07 05:33

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 filety

相关标签:
2条回答
  • 2021-02-07 06:00

    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.

    0 讨论(0)
  • 2021-02-07 06:00

    File type and file suffix are not necessarily the same thing. For example, if you are editing a .txt file, let zft = &ft sets zft to "text". let zfs = fnamemodify(bufname("%"), ":e") sets zfs to "txt".

    0 讨论(0)
提交回复
热议问题