What is a vimrc function to determine if a buffer has been modified?

前端 未结 2 1937
栀梦
栀梦 2020-12-18 02:41

I have a tabline function that I stole/modified from somewhere, but I would like the filename to have an asterisk before it if it has been modified since the last time it wa

相关标签:
2条回答
  • 2020-12-18 02:46

    I was just looking for the same and found that %m and %M is not well suited, as it tells you whether the currently open buffer is modified. So you cannot see if other buffers are modified (especially for tabs, this is important).

    The solution is the function getbufvar. Roughly from the help:

    let s .= (getbufvar(buflist[winnr - 1], "&mod")?'*':'').file
    

    instead of

    let s .= file
    

    should do the trick. This can be used nicely to show all buffers open in one tab (in case of multiple splits).

    0 讨论(0)
  • 2020-12-18 03:04

    tabline uses similar flags as statusline (see :h statusline). So %m is what you need and modifying the lines just before the endwhile as

    let s .= file
    let s .= (i == t ? '%m' : '')
    let i = i + 1
    

    will automatically place the default [+] after the file name in the current tab if there are unsaved changes.

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