Edit a file in-place in vim

前端 未结 3 484
情话喂你
情话喂你 2020-12-19 09:26

Normally, you don\'t actually edit a file in vim. If you run vim foo, edit, write and quit, vim unlinks foo and creates a new file and a new link

相关标签:
3条回答
  • 2020-12-19 09:39

    Quick google shows this link - Editing a hard link to a file. The command that controls that is

    set backupcopy=auto,breakhardlink
    
    0 讨论(0)
  • 2020-12-19 09:54

    Check out the help for the backupcopy option and the notes on crontab in particular. The short answer is

    :set backupcopy=yes
    
    0 讨论(0)
  • 2020-12-19 10:01

    The trick is to tell Vim to use a backupfile to allow the original file to be safely overwritten, otherwise Vim would write to a new file and then rename it to replace the original.

    And then tell Vim to write the backupfile nowhere. This way the regular write command will fail, and write must be forced.

    :set noswapfile
    :set backupcopy=yes
    :set backupdir=''
    :e big
    (hit Crtl-C to stopo line count and syntax highlight)
    (edit the file)
    :w!
    

    To really see what is going on at the file system level, I used inotify tools:

    inotifywait -m -r /path/to/my_test_dir/
    

    But be careful, this does not work well for very big files, for example, I was trying to edit a 10G file in place with this method and ended up with a truncated file of 1.5GB, and the rest of the data was lost. And Vim ended up using almost 2GB of RAM...

    Other option would be to use MMap functions, eg. in a Python command line.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题