Hide Vim backups (*~) in Windows Explorer
Asked Answered
P

3

5

On Windows, I normally work with Total Commander, which can easily be configured to ignore these *.*~ and *~ completely. But occasionally when I switch to Windows Explorer, I get little bit confused with all that "unknown" files/.

Can I set up Vim so that for every backup it creates it will also set "hidden" attribute?

Or set up some nice workaround?

I know I can set up Vim to put these in other directory, but I'd like to avoid that, since IIUC, it could suffer from naming conflicts.

Protractile answered 27/3, 2012 at 8:20 Comment(0)
M
6

If the backup option is set, vim updates the backup file every time we write the file with :w. And every time, it creates a file which is not hidden even though you had forcibly hidden it previously! So we need to do something everytime we write the buffer into file.

You can do this on windows. In your _vimrc file ( generally found at C:\Program Files (x86)\Vim ), add this line

autocmd BufWritePost,FileWritePost * silent ! attrib +h <afile>~

Where,

attrib=Windows File attribute changinf command
<afile>= Name of the file being sourced
silent= Prevent an annoying command window from popping up and asking user to press a key

This will make sure that the backup file gets hidden with every write to file from buffer. Why every time? Cos vim creates a non-hidden file at every write!

But you have to live with a flashing black window ( command window where we are running attrib command ) every time you save your file, but worth the pain :)

On linux/unix systems you can add this in your .vimrc

autocmd BufWritePost,FileWritePost * silent ! mv <afile>~ .<afile>

Hope this helps all those trying to find how to hide vim backup files.

Memberg answered 28/3, 2012 at 18:58 Comment(5)
It would be trivial to write a replacement for the attrib command that didn't pop up a command window.Dogoodism
The problem with bothering console window is solved here on SU.Protractile
OT, For the record: It seems that no matter how I try to hide the window, it does not work. It looks like if actually Vim opened the window before executing my command. That's another question, though... Thanks for great and complete answer!Protractile
Glad it helped you :) Yea the Quiet executable also doesn't seem to work here. I'll do post back if I find something interesting.Memberg
but still there is another a.txt.un~ which does not hide? how can i hide that too. @PavanManjunathRettarettig
E
3

I wrote a plugin for this a while back called autohide. It works by setting the "hidden" attribute after write as suggested in Pavan's answer. By default it only does this for swap files, viminfo, and persistent undo files; you can make it only hide backup files by configuring let g:autohide_types='b' in your .vimrc, or add it to the default list instead with 'suvb' instead of just 'b'.

Benefits over the manual method in Pavan's answer include handling of additional file types, arbitrary file patterns (like dotfiles), and some error handling (especially related to slow network shares that don't allow setting attributes right away after creating a file).

Erdei answered 27/4, 2017 at 16:40 Comment(2)
I used your plugin. It works well. But How can I hide .type~ files it did not hid file name a.txt~. @ErdeiRettarettig
It's a bug. Sorry about that, I'll try to put out a new version later. For now in the plugin/autohide.vim script, find the "call Autohide_DoHide(fnamemodify(a:file,':r').&backupext)" line and change the ':r' to ':n'. Then adding 'b' to the g:autohide_types variable should do what you want.Erdei
G
1

I have this in my _gvimrc:

set nobackup

No backup files are generated in the first place. However, the swap file (.*.swp) is still generated during editing (and deleted when you close Vim). So if your computer crashes, you can still recover your changes.

Gobbet answered 28/3, 2012 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.