0

When running vim with no command line arguments, it opens a splash screen describing Vim.

I would like to control what vim does "by default", so I can make it do something more useful like opening the current directory inside netrw, running :browse old so I can grab a recently visited file, or running :grep and waiting for me to do a search.

Is there a way to control what Vim does "by default" when it is not given any files to open?

5
  • May be would you be interested to this question Commented Oct 18, 2023 at 5:22
  • 2
    Does this answer your question? How can I change Vim's start or intro screen? Commented Oct 18, 2023 at 5:23
  • Do you still have something open in your question? How can we help you further? Otherwise may be could uou accept one of the solutions using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Nov 7, 2023 at 4:03
  • @VivianDeSmedt I still don't know how to get browse oldfiles to run by default (I think the oldfiles list is populated after your vimrc runs), but that probably belongs in another question. Your answer is very good though and I've accepted it. Commented Nov 7, 2023 at 5:54
  • Thanks for the feedback :-) Feel free to ask another question about browse old we will have a look. Commented Nov 7, 2023 at 7:24

2 Answers 2

2

Here is the test used by Startify:

fun! Start()
    " Don't run if: we have commandline arguments, we don't have an empty
    " buffer, if we've not invoked as vim or gvim, or if we'e start in insert mode
    if argc() || line2byte('$') != -1 || v:progname !~? '^[-gmnq]\=vim\=x\=\%[\.exe]$' || &insertmode
        return
    endif

    " Do the action you want  
endfun

" Run after "doing all the startup stuff"
autocmd VimEnter * call Start()

Extracted from this answer

1
  • 1
    The test is subtle, thanks for pointing that out!
    – D. Ben Knoble
    Commented Oct 18, 2023 at 12:28
0

Here's a ~/.vimrc that opens up the current directory when no files are given as arguments.

if argc() ==# 0
  silent execute 'edit .'
endif

vim has two functions argv and argc which mimic the behavior of the parameters conventionally called argv and argc in C, except both are functions.

argc returns the number of positional parameters in the argument list.

vim will helpfully remove all the command line arguments, so with the above ~/.vimrc, for example, vim -y will still open the current working directory.


The other example that I gave earlier today, browse oldfiles is more complex.

if argc() ==# 0
  silent execute 'browse oldfiles'
endif

The above .vimrc opens an empty list.

If we remove the silent, giving us

if argc() ==# 0
  execute 'edit .'
endif

then we see the following error message.

No old files.
Press ENTER or type command to continue

This makes me think that my ~/.vimrc is executing too early, before the oldfiles are actually parsed.

I don't have any idea how to delay the evaluation of my ~/.vimrc though.

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.