21

I found the answer to this question while writing it, so I've broadened it a little. I wanted to access the --servername argument, in order to create dynamic settings in my .vimrc file.

Through vim's help, I found the v:servername variable, and my script is working. However, now I'm curious if it's possible to access any arbitrary command-line argument. For example, if I wanted to know if vim was in Lisp mode (-l) or Debugging mode (-D), how would I do it? There seems to be no corresponding v: variable for them.

Here are the variables I found by autocompleting :help v:<Tab> enter image description here

Is there a general way to access command-line arguments from vimscript?

2 Answers 2

13

Strangely, I think the answer may be "No, there is no direct way to access startup options specified on the command line".

The :args command and argv() can be used to access the filename(s) specified on startup, but that's not what you want.

I see on Vim's forums that someone offered this solution to get the startup command line on Linux:

:exe '!tr "\0" " " </proc/' . getpid() . '/cmdline' 

I assume there's analogous command on Windows. . . .

You can look over that forum thread here:

http://groups.google.com/group/vim_use/browse_thread/thread/43773f27cdc10265/ad17ae8180c0fb6e?show_docid=ad17ae8180c0fb6e

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The solution is equivalent to the other answer, marking correct because of the citation.
10

My googling indicates that this feature has been proposed but never implemented. However I did come up with a bit of a kludge that nevertheless works:

:echo split( system( "ps -o command= -p " . getpid() ) )
# => [ 'vim', ... arguments ... ]

(Tested on OS X Lion.)

The getpid() function gets Vim's PID, then we call ps externally with options to return nothing but the "command" value for process, then use split() to split the command into a list.

1 Comment

Set a variable such as s:result with this, then combine with count(s:result, '-q') to find out if a particular option (such as -q, for example) is used. This will let you know whether a command line option was used. In my example using -q, you can tell if you are in quickfix mode. Alas, checking &buftype does not always work, especially during initialization in .vimrc.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.