esamatti
esamatti

Reputation: 18973

Vim: Warn if file is not tracked in Git

Gitgutter is great tool for keeping me informed about uncommitted changes but too often I forget full files from commits.

I'd like to have a warning in the Vim statusbar if I'm editing a file that's in a git repository directory but completely untracked.

Any ideas how to implement this?

I'm currently using Airline and Fugitive so it would be nice if it could implemented with those.

Upvotes: 4

Views: 328

Answers (3)

benjifisher
benjifisher

Reputation: 5122

First, define a function something like this:

fun! GitTracks(...)
  let file = a:0 ? a:1 : expand('%')
  let message = system('git ls-files -- ' . file)
  let is_tracked = (message =~ '^fatal:') ? 0 : strlen(message)
  if is_tracked
    hi StatusLine guibg=blue
  else
    hi StatusLine guibg=black
  endif
  return is_tracked
endfun

This will return 0 if you are not in a git repository or if your file is not tracked. Who knows, some day you may want to call it with an argument. Just for kicks, I change the color of the status line. (This takes a while to kick in, depending on your setting for 'updatetime'. You could remove it from the function and use a BufEnter autocommand that checks the value of GitTracks().)

Next, decide how you want to indicate this in your status line. For example,

:set stl+=%{GitTracks()?'tracked':'NOT\ tracked'}

Upvotes: 1

Ken
Ken

Reputation: 8897

If you use the git fugitive plugin it's easy to setup on the statusline. Just add the following to your .vimrc file.

set statusline=%f
set statusline+=%{fugitive#statusline()}

Of course you can get as fancy as wish. http://learnvimscriptthehardway.stevelosh.com/chapters/17.html

Upvotes: 0

sanmiguel
sanmiguel

Reputation: 4888

You can almost certainly do this with powerline. It's a hugely configurable utility that can be used in vim to give you a really powerful statusline, as well as in bash/zsh for powerful prompt display.

Here's what mine looks like (stock configuration), showing the following info:

{normal mode active} | BR: {current git branch} | {filename} vim-powerline-example

I'm certain there'll be a way to get status for the current buffer's file in there...

Upvotes: 0

Related Questions