0

Is there a way to tell Vim from command line to run a specific command after running the vimrc, but before loading a file?

E. g.

  • vim --cmd <cmd> <file> runs <cmd> before loading vimrc,
  • vim -c <cmd> <file> runs <cmd> after loading vimrc and after opening the file.

Is there a middle-ground?


In case of an XY problem, this is what I'm trying to achieve.

I'm trying to use Vim to open and edit a huge file (~50GB). I have enough RAM to load the file, but not enough disk space and bandwidth for any sort of extra temporary files. My vimrc enables backups, swap files and undo files by default, and I'd like to find a way to disable all three for a specific Vim invocation. This needs to be done before the file is opened (otherwise a swap file will be created), but after vimrc is loaded (otherwise vimrc is going to override the settings).

3
  • Could you differentiate by file type? You can disable swapping, backups, etc. in this case.
    – Friedrich
    Commented Jul 26, 2023 at 15:52
  • 2
    Check the LargeFile plugin I think it uses a BufReadPre auto-command Commented Jul 26, 2023 at 15:53
  • Possibly combine the cmd flag with VimEnter autocommand? Can’t recall exactly when that runs.
    – D. Ben Knoble
    Commented Jul 26, 2023 at 17:10

1 Answer 1

3

Here is a low-tech approach.

In your vimrc, put all the relevant options behind a guard. Something like:

if !exists('$LARGE_FILE')
    set backup
    " etc.
endif

If the environment variable exists (its value doesn't matter) don't set those options.

If the environment variable doesn't exist, set those options.

Now, in your shell, you can call Vim like this:

$ LARGE_FILE=1 vim 50gbfile.json
4
  • That's exactly what I did :) I just wanted to know if there's a better way.
    – intelfx
    Commented Jul 27, 2023 at 2:42
  • Define "better". If you want an automagic solution, the plugin mentioned in the comments is certainly "better". IMO, the problem and the solution are too trivial to require a third-party plugin.
    – romainl
    Commented Jul 27, 2023 at 5:07
  • So there is no way to pre-gather the size of a file for such logic ... using vimscript? Commented Sep 3 at 18:35
  • "Pre-gather the size of a file" is trivially easy, as should be obvious after a quick glance at :help file-functions. But it is not the problem OP was facing, which was to run that logic at a specific moment… which I suggest to bypass entirely in a declarative way.
    – romainl
    Commented Sep 3 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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