12

I used to work with netbeans and it always put an asterisk and changed the tab color when the file had changed since last save. Is there any way to make vim do something similar, that is, remind me that I haven't saved the file?

I know that there is a way to have it save automatically once in a while, but I don't want to do that.

user202729
  • 3,358
  • 3
  • 25
  • 36
user798275
  • 441
  • 6
  • 14

4 Answers4

9

You can use the m flag in the 'statusline' option to add a [+] if file is modified. Note that in order to see the statusline, you'll need to set 'laststatus' to be greater than 0 (1-Only shows status line if there are two or more windows, 2-Always).

If you're using a GUI-version, such as MacVim, you may prefer to set 'titlestring', which uses the same syntax but will alter the name of the window in your window-manager.

Example:

:set laststatus=2
:set statusline=[%n]\ %<%f%h%m

This will display:

  • [: literal
  • %n: buffer number
  • ]: literal
  • \<Space>: a space
  • %<: Truncate the field at the beginning if too long
  • %f: Path to the file in the buffer, as typed or relative to current directory.
  • %h: Help buffer flag, text is "[help]".
  • %m: Modified flag, text is "[+]"; "[-]" if 'modifiable' is off.

For more information see:

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    The statusline is for sure the way to go here. I just recently found the [vim-airline](https://github.com/bling/vim-airline) plugin which is provides an excellent set of defaults for what it displays: mode, modified, git branch, filename, encoding, cursor position, etc. – Edward Oct 28 '13 at 00:44
  • If you wanted something a little bit more fancy without immediately having to resort to plugins like vim-airline, you could use an expression in your statusline like this `[%{getbufvar(bufnr('%'),'&mod')?'modified':'saved'}]`. To add it to your existing statusline use something like `:set statusline+=\ [%{getbufvar(bufnr('%'),'&mod')?'modified':'saved'}]` – Matijs Jul 05 '14 at 20:11
7

Call :ls and you will see a + before unsaved buffers

Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
1

If the terminal displays its title somewhere, it's possible to use

:set title

to display whether the file is modified: a + is displayed after the file name if it's modified.

However, a file can have + at the end of its file name. For most files this should work fine.


Source: https://stackoverflow.com/a/13244715/5267751

user202729
  • 3,358
  • 3
  • 25
  • 36
0

Pressing Ctrl+g (or equivalently :f) in normal mode will show the file status, which indicates whether the file is modified.


The status looks like this

"file_name" 100 lines --20%--

if the file is not modified, or

"file_name" [Modified] 100 lines --20%--

if the file is modified.


For more info see :help ^g.

user202729
  • 3,358
  • 3
  • 25
  • 36