linux - 删除当前目录下所有VIM临时文件的正确命令是什么?
PHP中文网
PHP中文网 2017-04-17 11:34:43
0
4
1077

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

rm *.*~

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
大家讲道理
  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.

阿神

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)
左手右手慢动作
$vim ~/.vimrc

Join

set nobackup

Try it and see if it works

阿神

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!