How to make vim indicate the file has changed since last save?

How to make vim indicate the file has changed since last save?



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.






This might be helpful.

– diyoda_
Oct 27 '13 at 4:41






@Diode: How are marks useful here?

– Johnsyweb
Oct 27 '13 at 5:55






As another alternative, it helps to show the tab line.

– user202729
Sep 15 '18 at 15:57




4 Answers
4



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).


m


'statusline'


[+]


'laststatus'



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.


'titlestring'


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



This will display:


[


%n


]


<Space>


%<


%f


%h


%m



For more information see:


:help status-line






The statusline is for sure the way to go here. I just recently found the 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 0: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 5 '14 at 20:11


[%getbufvar(bufnr('%'),'&mod')?'modified':'saved']


:set statusline+= [%getbufvar(bufnr('%'),'&mod')?'modified':'saved']



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


:ls


+



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


:f



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.


:help ^g



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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)