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?

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Greg Nisbet
  • 1,839
  • 14
  • 27

2 Answers2

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

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
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.

Greg Nisbet
  • 1,839
  • 14
  • 27