Reputation: 2793
How can one detect stdin input in .vimrc
?
I have the following command in my ~/.vimrc
:
autocmd BufWinEnter * silent loadview
to open a file with a cursor at the last line position. However, if I use vim on stdin, I get the following error message:
Error detected while processing BufWinEnter Autocommands for "*":
E32: No file name
Press ENTER or type command to continue
How can one detect that vim is used on stdin in .vimrc
to suppress execution of the command in such cases?
Thank you for your help!
Upvotes: 1
Views: 404
Reputation: 196556
v:argv
is a list that begins with the complete path of the program, and contains each argument, if any.
If you call Vim like this:
$ vim
:echo v:argv
outputs something like:
['/path/to/vim']
If you call Vim like this:
$ vim foo.txt bar.json
:echo v:argv
outputs something like:
['/path/to/vim', 'foo.txt', 'bar.json']
If you call Vim like this:
$ echo 'foo bar baz' | vim -
:echo v:argv
outputs something like:
['/path/to/vim', '-']
Therefore, you can condition the execution of :loadview
to the content of v:argv[1]
. Note that we use get()
because the index 1
may not exist:
autocmd BufWinEnter * if get(v:argv, 1, '') != '-' | silent loadview | endif
Reference:
:help :get()
:help v:argv
Upvotes: 7