search

Home  >  Q&A  >  body text

linux - 删除当前目录下所有VIM临时文件的正确命令是什么?

在使用VIM的过程中,会产生大量以波浪线(~)结尾的VIM临时文件,请问有没有一条删除所有VIM临时文件的命令?

rm *.*~

好像可以,不知道会产生不良后果吗?总感觉不是很安全

PHP中文网PHP中文网2810 days ago1097

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:36:43

      set nobackup
      set undodir=~/.undodir
    

    I use git or icloud dropbox for cloud backup. I really don’t see any need for vim backup files. That file will also affect grep, which is too inconvenient.

    reply
    0
  • 阿神

    阿神2017-04-17 11:36:43

    I have a special script to handle Vim backup files. Depends on Python 3 and mlocate. Backup files will be deleted in two situations: the original file has not been modified for a long time; the original file no longer exists.

    It is not recommended to disable the backup function. Although persistence is eliminated now, multiple backups of data are always safer.

    Let’s post a copy of the code here:

    #!/usr/bin/env python3
    # fileencoding=utf-8
    
    '''从列表中读取备份文件(*~)列表,并检测原文件是否存在;若不存在则删之'''
    
    import sys, os
    import subprocess
    import datetime
    
    if len(sys.argv) == 1:
      db = None
    elif len(sys.argv) == 2:
      db = sys.argv[1]
    else:
      sys.exit('argument error')
    
    if db:
      f = subprocess.getoutput("locate -d '%s' -e -b '*~' 2> /dev/null" % db).split('\n')
    else:
      f = subprocess.getoutput("locate -e -b '*~' 2> /dev/null").split('\n')
    
    def filter(i):
      if not os.path.isfile(i[:-1]):
        return True
      try:
        atime = datetime.datetime.fromtimestamp(os.stat(i).st_atime)
      except FileNotFoundError:
        return False
      now = datetime.datetime.today()
      interval = datetime.timedelta(days=30)
      if now - atime > interval:
        return True
      return False
    
    for i in f:
      if i.endswith('~') and os.access(os.path.split(i)[0], os.W_OK):
        if filter(i):
          try:
            print("删除", i)
            try:
                os.unlink(i)
            except FileNotFoundError:
                print(i, '在被删除前消失了:-(')
            # print(i)
          except:
            print(i+': error'+repr(sys.exc_info()), file=sys.stderr)
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:36:43

    $vim ~/.vimrc
    

    Join

    set nobackup
    

    Try it and see if it works

    reply
    0
  • 阿神

    阿神2017-04-17 11:36:43

    You can’t avoid deleting it this way:

    1. The file name starts with - (can be mistaken for parameters) (this is a classic pitfall)
    2. The file name does not contain .

    The correct command is rm -f -- *~.

    The above only answers the superficial part of this question (Question Y). Regarding the essential goal of dealing with vim’s temporary files (X problem), please also ask more vim experts on this site represented by @evian to answer

    reply
    0
  • Cancelreply