什么是 vimrc 函数来确定缓冲区是否已被修改? [英] What is a vimrc function to determine if a buffer has been modified?
问题描述
我有一个从某处窃取/修改的 tabline 函数,但我希望文件名前有一个星号,如果它自上次写入磁盘以来已被修改(即,如果 :up 将执行行动).
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 was written to disk (ie if :up would perform an action).
例如,这是我打开 vim -p file*.txt 时的标签行
For example this is my tabline when I open vim -p file*.txt
file1.txt file2.txt file3.txt
然后在我更改file1.txt之后不保存:
Then after I change file1.txt and don't save it:
*file1.txt file2.txt file3.txt
我的标签功能:
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
推荐答案
我刚找了一样,发现 %m
和 %M
不太适合,因为它告诉您当前打开的缓冲区是否被修改.所以你无法看到其他缓冲区是否被修改(特别是对于标签,这很重要).
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).
解决方案是函数getbufvar
.大致来自帮助:
The solution is the function getbufvar
. Roughly from the help:
let s .= (getbufvar(buflist[winnr - 1], "&mod")?'*':'').file
代替
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).
这篇关于什么是 vimrc 函数来确定缓冲区是否已被修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!