Disable swap file for large files?

174 views
Skip to first unread message

skeept

unread,
Aug 20, 2013, 5:27:51 PM8/20/13
to vim...@googlegroups.com
Is there a way of enabling swap files for files say less than 10Mb but disabling swap files for files or larger sizes?

Thanks,

Jorge

Ben Fritz

unread,
Aug 20, 2013, 5:32:28 PM8/20/13
to vim...@googlegroups.com

Start with them globally disabled. On a BufRead autocmd, set it to enabled (locally), if getfsize() on the file name is larger than your threshold value.

John Little

unread,
Aug 20, 2013, 6:36:00 PM8/20/13
to vim...@googlegroups.com
Use the LargeFile plugin, it does exactly that. One can set a variable in your.vimrc to set the threshold; it defaults to 20 MB.


Regards, John Little

Ben Fritz

unread,
Aug 21, 2013, 10:09:55 AM8/21/13
to vim...@googlegroups.com

Oops, I guess You wanted to DISABLE for files larger than a certain size, not ENABLE them. You could do the same thing in reverse, or potentially enable by default and then disable locally in a BufReadPre. I'm not sure whether that works or not.

Ben Fritz

unread,
Aug 21, 2013, 10:11:56 AM8/21/13
to vim...@googlegroups.com
On Tuesday, August 20, 2013 5:36:00 PM UTC-5, John Little wrote:
> Use the LargeFile plugin, it does exactly that. One can set a variable in your.vimrc to set the threshold; it defaults to 20 MB.
>
>


I'm actually considering uninstalling the LargeFile plugin myself. I have file associations set on my system to launch files in existing Vim with --remote-tab-silent when I double-click them, and when I view a large log file, suddenly all my undo history is lost for all my code files! I really wish 'undolevels' were not a global option, but it is, so LargeFile messing with it is not very good when using the same Vim for everything.

Charles Campbell

unread,
Aug 21, 2013, 1:29:03 PM8/21/13
to vim...@googlegroups.com
Hmm, I don't typically use the undo functions, so please help me with this:

I think I can get a copy of the undo situation with undotree(). Is
there a way to restore it? I'm thinking of doing a save-undo ... work
with large file ... restore undo scenario.

Regards,
Chip Campbell

Jorge Rodrigues

unread,
Aug 21, 2013, 2:57:16 PM8/21/13
to vim...@googlegroups.com

Oops, I guess You wanted to DISABLE for files larger than a certain size, not ENABLE them. You could do the same thing in reverse, or potentially enable by default and then disable locally in a BufReadPre. I'm not sure whether that works or not.
 
 
Thanks for the suggestions Ben and John, I went with the following:
  "try to disable swapfiles for large files                                                                            
  augroup disable_swap_large_files                                                                                     
    autocmd!                                                                                                           
    "5 megs                                                                                                            
    autocmd BufRead * if getfsize(expand('%:p')) > 5242880 |                                                           
          \ setlocal noswapfile noundofile | endif                                                                     
  augroup END             
 
It seems that the backup option is a global one so I didn't change that one.
 
Regards,
Jorge
 
 
 

Christian Brabandt

unread,
Aug 22, 2013, 2:25:29 AM8/22/13
to vim...@googlegroups.com
On Wed, August 21, 2013 19:29, Charles Campbell wrote:
>> On Tuesday, August 20, 2013 5:36:00 PM UTC-5, John Little wrote:
>> I really wish 'undolevels' were not a global option, but it is,
>> so LargeFile messing with it is not very good when using the same Vim

There is a patch pending, making 'undolevels' buffer-local. Unfortunately,
Bram hasn't included it yet.

> I think I can get a copy of the undo situation with undotree(). Is
> there a way to restore it? I'm thinking of doing a save-undo ... work
> with large file ... restore undo scenario.

No. There are only two ways handling this. If you don't really change
the buffer, you can disable the undo and later :rundo the
existing undofiles. Or you can work with a copy of the buffer and only
write the changed copy back. The NrrwRgn plugin might be helpful with
this. Both ways are usually not really workable in practice.

regards,
Christian

Ben Fritz

unread,
Aug 22, 2013, 10:37:31 AM8/22/13
to vim...@googlegroups.com
On Thursday, August 22, 2013 1:25:29 AM UTC-5, Christian Brabandt wrote:
>
> No. There are only two ways handling this. If you don't really change
>
> the buffer, you can disable the undo and later :rundo the
>
> existing undofiles.

Woah, woah, woah!

Could one theoretically add to the autocmd that detects a large file, to (before setting undolevels) do something like:

bufdo let names[bufnr('%')] = tempname() | exe 'wundo' names[bufnr('%')]

And then when closing the file/undoing the largefile settings,

bufdo exe 'rundo' names[bufnr('%')]

(with error handling and all that added of course).

Probably would want to set 'nomodifiable' on all the buffers until the largefile settings were out of effect or something to prevent creating new changes not in the undo file.

Christian Brabandt

unread,
Aug 22, 2013, 11:59:44 AM8/22/13
to vim...@googlegroups.com
Might work. Not sure, one wants to use several tempname() calls at all,
since tempname() is only guaranteed to give distinct names for 26 calls
and some plugins make use of tempname() themselves (so it could happen,
that plugins overwrite the undofile).

regards,
Christian

Charles Campbell

unread,
Aug 22, 2013, 12:20:14 PM8/22/13
to vim...@googlegroups.com
I've been trying it out, but I'm going to have to put this mod down for
now. I've got BufLeave events firing when they shouldn't.

Regards,
Chip Campbell

Christian Brabandt

unread,
Aug 22, 2013, 2:49:00 PM8/22/13
to vim...@googlegroups.com
Hi Charles!
Well, we should convince Bram then to include the undolevels local patch
then.

regards,
Christian
--
Der Tourist am Seeufer: Er sieht einen Tafel: "M�VEN ZU VERKAUFEN".
Was kostet eine M�ve?
DEM 2,60 das St�ck.
Dann nehme ich gleich f�nf.
Sehen sie sie, die M�ven dort links oben, sie geh�ren nun ihnen.

Abhishek

unread,
Aug 23, 2013, 6:33:41 AM8/23/13
to vim...@googlegroups.com

Hi,

Here's something I use:

let g:BufSizeThresholdSmall = 500000 " No swap threshold (Set to 500 KB)
let g:BufSizeThresholdBig = 1000000000 " Minimal mode threshold (Set to 1 GB)
" For small files, switch all options on
autocmd BufEnter * if getfsize(expand("<afile>")) < g:BufSizeThresholdSmall | set incsearch | setlocal statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P | endif
" Don't use a swap file for big files
autocmd BufReadPre * if getfsize(expand("<afile>")) >= g:BufSizeThresholdSmall | setlocal noswapfile | setlocal statusline=%<%f\ %h%m%r\ No\ Swap\ File%=%-14.(%l,%c%V%)\ %P | endif
" Switch off other options for still bigger files
autocmd BufEnter * if getfsize(expand("<afile>")) >= g:BufSizeThresholdBig | set noincsearch | setlocal noswapfile | setlocal statusline=%<%f\ %h%m%r\ No\ Swap\ File.\ No\ Incsearch%=%-14.(%l,%c%V%)\ %P | endif

It allows two thresholds and disables more options for very large files. Moreover, the status line is updated to reflect the current state of options so that I know what to expect for the currently open file.

PS: I don't remember if I copied it from somewhere or built it over time. In either case, corrections and suggestions for improvement are welcome. Will help me learn something new. :)

Best Regards,
Abhishek

Ben Fritz

unread,
Aug 23, 2013, 11:36:57 AM8/23/13
to vim...@googlegroups.com
It would be nice, if somebody took some of the suggestions in this thread and incorporated them into:

http://vim.wikia.com/wiki/Faster_loading_of_large_files

Reply all
Reply to author
Forward
0 new messages