DavisDude
DavisDude

Reputation: 952

How to set a portion of the status line a certain color only if an element is evaluated- Vim

In Vim I'm trying to make my status-line to work how I want it to. Here's what I'm going for: When the file I'm editing has been saved, I want it to look like this when saved: Ideal-Saved

And this when unsaved: Actual-Saved

The former I see, but I get this when saved: Ideal-Unsaved

(If those are too small to see, you can get the links here, here, and here)

Here's what I have so far:

set statusline=%1*\ %n:\ %f\ %y\ %(%#mid#%m%)%=%1*%6v%6l\%6L\ %*

Any suggestions?

Ideally I would like to be able to see the top when unsaved and the second one when saved.

Edit

After some searching I've come up with this, which seems like as good a start as any:

highlight User1 guibg=Black guifg=DarkYellow
highlight mid guibg=DarkRed guifg=Yellow
set statusline=%1*\ %n:\ %f\ %y\ %=%6v%6l\%6L\ %*
autocmd InsertChange * if (&modified) | set statusline=%1*\ %n:\ %f\ %y\ %#mid#%m%=%1*%6v%6l\%6L\ %* | endif
autocmd BufWrite * set statusline=%1*\ %n:\ %f\ %y\ %=%6v%6l\%6L\ %*

However, I can't seem to figure out when InsertChanged is actually called.

Upvotes: 1

Views: 352

Answers (2)

DavisDude
DavisDude

Reputation: 952

After much searching I found a solution that works for all my purposes:

set laststatus=2
highlight User1 guibg=Black guifg=DarkYellow
highlight mid guibg=DarkRed guifg=Yellow

let s:default = '%1* %n: %f %y %=%6v%6l%6L %*'
let s:changed =  '%1* %n: %f %y %#mid#%m%=%1*%6v%6l%6L %*'

fun IsModified()
    if &modified
        return s:changed
    endif
    return s:default
endfun
autocmd BufEnter * set statusline=%!IsModified()
autocmd BufLeave * let &l:statusline = IsModified()

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172520

You're right that you can use :autocmds to update the statusline colors. However, InsertChange does not trigger when you think it is; it is merely for switching between normal inserting and overriding. Unfortunately, there's no exact event for "buffer changed"; you can trigger on InsertEnter and CursorHold, and then check the &modified flag.

My StatusLineHighlight plugin indicates the state of the buffer (modified, readonly, unmodifiable, special non-file "scratch") / window (is preview window) by changing the highlighting of the window's status line. You can either use it directly, or at least find useful implementation hints for your own one.

Upvotes: 0

Related Questions