Select vimenter autocmd's to run based on args

1

1

I'm trying to reduce the number of things I need to do when I open VIM. I have the three following scenarios that I'd like to account for:

1. Open vim with a directory specified

vim .

For this case, I would like to open NERDTree, with this in my ~/.vimrc:

autocmd vimenter * NERDTree


2. Open vim with a directory specified, on a large monitor

vim . --large

Here I'd like to open NERDTree as well as do some screen splits:

autocmd vimenter * NERDTree
autocmd vimenter * wincmd w
autocmd vimenter * wincmd v
autocmd vimenter * wincmd v


3. Open vim with a specific filename

vim ~/vimrc

For this case, I would like to not run any of the autocmds that I mentioned above.


Edit - Final implementation

Thanks to FDinoff's answer.

if argc() == 1 && arv(0) == '.' " `vim .` called
  autocmd vimenter * NERDTree   " Start up NERDTree
  autocmd vimenter * wincmd w   " Jump to split that file is open in

  if !empty($L)
    autocmd vimenter * wincmd v
    autocmd vimenter * wincmd v
  endif
endif

and run it with L=t vim ., vim ., or vim specific_file

RyanB

Posted 2013-06-16T14:29:11.940

Reputation: 113

Answers

2

You can use argc() and argv() to find the arguments to vim in your vimrc and set the autocmds accordingly. So for the first one you could do something like this to only open NERDTree if the first argument is .

if argc() == 1 && argv(0) == '.'
    autocmd vimenter * NERDTree
endif

For the large monitor I think using the shell variable would be the correct way to do this because vim thinks that --large in vim --large is an argument to vim not to your script. I do not know if there is a way around this.

You could do something like vim -- . --large but then vim opens a buffer for a file --large which is probably not what you want.

FDinoff

Posted 2013-06-16T14:29:11.940

Reputation: 1 623

Thanks! I'm updating the question with my final implementation. – RyanB – 2013-06-16T21:51:25.800

@RyanB You can also just define L in your bashrc so you don't have to type it every time. – FDinoff – 2013-06-16T22:00:06.450