greduan
greduan

Reputation: 4938

Checking if %Y exists in current file in VimScript

Like the title says.

I would love to know how, with VimScript, I can do an if statement, that checks if %Y (for statusline) exists in the current file. This is what I have so far:

if expand('%:Y') != ""
    let filetype=extend('%:Y')
    let filetype+= ",\ "
else
    let filetype=""
endif

This is, of course, for my statusline, for which I don't want to show the filetype comma and space if the filetype doesn't exist.

Thanks for all your help!

Upvotes: 2

Views: 161

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172600

You cannot use expand() or similar to resolve options in 'statusline'; they are only interpreted inside the setting.

If you don't like the formatting returned by the option or want to combine options to a custom indicator, you have to emulate the option in a %{...} Vimscript expression. Most options can be completely or mostly emulated. For your %Y, this would be included in 'statusline' instead:

let &statusline .= '%{empty(&filetype) ? "" : "," . toupper(&filetype) . ", "}'

Instead of putting a large expression inline, you can also move it to a separate :function.

Upvotes: 4

Related Questions