mwcz
mwcz

Reputation: 9331

Is it possible to access vim's command-line arguments in vimscript?

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?

Upvotes: 21

Views: 4970

Answers (2)

Jordan Running
Jordan Running

Reputation: 106147

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.

Upvotes: 10

Herbert Sitz
Herbert Sitz

Reputation: 22266

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

Upvotes: 13

Related Questions