14

How do I disable vim from producing backup files? Like file~ do not generate. Should I do that by modifying /etc/vimrc?

I added set nobackup to /etc/vimrc, but when I looked at the distributed file system log, the backup file was still generated but it was then deleted.

15

Try adding:

set nobackup nowritebackup

to your vimrc.

UPDATE with regards to Jason Lefler's answer

If you want to prevent your backup/undo/swap files to be created next to your edited file consider setting directories for them:

let &directory = expand('~/.vimdata/swap//')

set backup
let &backupdir = expand('~/.vimdata/backup//')

set undofile
let &undodir = expand('~/.vimdata/undo//')

if !isdirectory(&undodir) | call mkdir(&undodir, "p") | endif
if !isdirectory(&backupdir) | call mkdir(&backupdir, "p") | endif
if !isdirectory(&directory) | call mkdir(&directory, "p") | endif

You set :h directory, :h backupdir, :h undodir to whatever you prefer. Note, there is double slash // at the end (~/.vimdata/swap//) needed to prevent collisions when you edit the same file names in different paths, e.g., there will be files like C%%Users%%username%%docs%%test.adoc~ and C%%Users%%username%%docs%%awesome%%test.adoc~ created in backupdir.

Then you create those directories if they don't exist. .vimdata dir is what I use to have all of them in one place, you can change it of course.

| improve this answer | |
13

Maxim's answer works for disabling backups entirely, but you can also relocate the backup directory, if you just want to stop cluttering your working directory but want the backup functionality.

set backupdir=~/.vim/backup

I also do this for temporary files, as well.

set directory=~/.vim/tmp

(These directories may need to already exist.)

| improve this answer | |
  • 1
    Welcome to Vi and Vim! Nice first answer—I prefer to preserve these backups too. For my undodir, I actually do an if !isdirectory(expand(&undodir)) | call mkdir(expand(&undodir), "p") | endif in my vimrc (so you can do this programatically) – D. Ben Knoble Oct 30 '19 at 15:15
  • 1
    Oo, undodir is new to me. Gonna look into that one. More lines for my already-monolithic vimrc file! – user25466 Oct 30 '19 at 15:21
  • I do the same :) – Maxim Kim Oct 30 '19 at 16:04
  • 1
    Does this solution cause problems if you're editing files in different directories with the same name -- say, pom.xml files in various project directories? – jeffB Oct 30 '19 at 16:58
  • 2
    Depends on path of your backupdir. If it ends with double slash then no issues. See corresponding help chapters @jeffB – Maxim Kim Oct 30 '19 at 17:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.