Skip to main content
Add a more complete solution for the general case, including spaces/newlines in the arguments.
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

If you're going the Python + ps route, then this should be better and more efficient:

:pyx import os, subprocess
:let vim_cmdline = pyxeval("subprocess.check_output(['ps', '-p', str(os.getpid()), '-o', 'cmd='])")

Note that you're checking for the current PID (os.getpid()), since the Python interpreter is running inside the current Vim process.

One (definitely Linux-only!) method that doesn't involve Python or ps is to read from the /proc filesystem directly. It turns out /proc/self has information about the current process, and the cmdline file has the command line (separated with NUL bytes.)

You can get the current command-line in a Vim list with:

let vim_cmdline = split(readfile('/proc/self/cmdline')[0], '\n', 1)

It's not 100% perfect. If you have arguments with a newline in them, then this expression wil discard them (they'd be indices [1], [2], etc. in the list), but it's probably good enough for most purposes, since arguments with newlines are typically quite rare.


UPDATE: This does a better job at it, taking into account newline characters, etc.:

function! VimCmdLine()
  let c = map(readfile('/proc/self/cmdline'), {_, v -> split(v, '\n', 1)})
  let r = c[0]
  for i in c[1:]
    let r[-1] = r[-1]."\n".i[0]
    let r = r + i[1:]
  endfor
  return r[:-2]
endfunction

Also returns a list, with one element per command-line argument.

If you're going the Python + ps route, then this should be better and more efficient:

:pyx import os, subprocess
:let vim_cmdline = pyxeval("subprocess.check_output(['ps', '-p', str(os.getpid()), '-o', 'cmd='])")

Note that you're checking for the current PID (os.getpid()), since the Python interpreter is running inside the current Vim process.

One (definitely Linux-only!) method that doesn't involve Python or ps is to read from the /proc filesystem directly. It turns out /proc/self has information about the current process, and the cmdline file has the command line (separated with NUL bytes.)

You can get the current command-line in a Vim list with:

let vim_cmdline = split(readfile('/proc/self/cmdline')[0], '\n', 1)

It's not 100% perfect. If you have arguments with a newline in them, then this expression wil discard them (they'd be indices [1], [2], etc. in the list), but it's probably good enough for most purposes, since arguments with newlines are typically quite rare.

If you're going the Python + ps route, then this should be better and more efficient:

:pyx import os, subprocess
:let vim_cmdline = pyxeval("subprocess.check_output(['ps', '-p', str(os.getpid()), '-o', 'cmd='])")

Note that you're checking for the current PID (os.getpid()), since the Python interpreter is running inside the current Vim process.

One (definitely Linux-only!) method that doesn't involve Python or ps is to read from the /proc filesystem directly. It turns out /proc/self has information about the current process, and the cmdline file has the command line (separated with NUL bytes.)

You can get the current command-line in a Vim list with:

let vim_cmdline = split(readfile('/proc/self/cmdline')[0], '\n', 1)

It's not 100% perfect. If you have arguments with a newline in them, then this expression wil discard them (they'd be indices [1], [2], etc. in the list), but it's probably good enough for most purposes, since arguments with newlines are typically quite rare.


UPDATE: This does a better job at it, taking into account newline characters, etc.:

function! VimCmdLine()
  let c = map(readfile('/proc/self/cmdline'), {_, v -> split(v, '\n', 1)})
  let r = c[0]
  for i in c[1:]
    let r[-1] = r[-1]."\n".i[0]
    let r = r + i[1:]
  endfor
  return r[:-2]
endfunction

Also returns a list, with one element per command-line argument.

Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

If you're going the Python + ps route, then this should be better and more efficient:

:pyx import os, subprocess
:let vim_cmdline = pyxeval("subprocess.check_output(['ps', '-p', str(os.getpid()), '-o', 'cmd='])")

Note that you're checking for the current PID (os.getpid()), since the Python interpreter is running inside the current Vim process.

One (definitely Linux-only!) method that doesn't involve Python or ps is to read from the /proc filesystem directly. It turns out /proc/self has information about the current process, and the cmdline file has the command line (separated with NUL bytes.)

You can get the current command-line in a Vim list with:

let vim_cmdline = split(readfile('/proc/self/cmdline')[0], '\n', 1)

It's not 100% perfect. If you have arguments with a newline in them, then this expression wil discard them (they'd be indices [1], [2], etc. in the list), but it's probably good enough for most purposes, since arguments with newlines are typically quite rare.