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

I have a tab function that I stole / modified somewhere, but I would like the file name to have an asterisk in front of me if it has been changed since the last time it was written to disk (i.e. if: up would take action )

For example, this is my tab when I open the vim -p * .txt file

file1.txt file2.txt file3.txt 

Then after modifying file1.txt and do not save it:

 *file1.txt file2.txt file3.txt 

My tab function:

 if exists("+showtabline") function MyTabLine() let s = '' let t = tabpagenr() let i = 1 while i <= tabpagenr('$') let buflist = tabpagebuflist(i) let winnr = tabpagewinnr(i) let s .= ' %*' let s .= (i == t ? '%#TabLineSel#' : '%#TabLine#') let file = bufname(buflist[winnr - 1]) let file = fnamemodify(file, ':p:t') if file == '' let file = '[No Name]' endif let s .= file let i = i + 1 endwhile let s .= '%T%#TabLineFill#%=' let s .= (tabpagenr('$') > 1 ? '%999XX' : 'X') return s endfunction set stal=2 set tabline=%!MyTabLine() endif 
+6
source share
2 answers

I just looked for the same thing and found that %m and %m not very suitable, as it tells you if the current open buffer has changed. Thus, you cannot see if other buffers have changed (especially for tabs, this is important).

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

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

instead

 let s .= file 

gotta do the trick. This can be used to show all buffers opened on one tab (in the case of multiple splits).

+13
source

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

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

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

+1
source

Source: https://habr.com/ru/post/891748/


All Articles