1

If I start with nvim -u NONE , I want to get "-u NONE".

I tried to get pid using bash and python, but it seems to return the pid of the process that is created (either python or bash).

So how should I do it?

3
  • It might be in argv or something similar. These are the command arguments, not the current command line of (n)vim.
    – D. Ben Knoble
    Commented Aug 30, 2019 at 22:35
  • These are the files in the argument list. Not what I need.
    – eyal karni
    Commented Aug 30, 2019 at 23:01
  • Just to give context, I want to load the default workspace only if neovim started with no arguments. echo "xxx" | nvim - consist of an argument, and loading the default workspace ruins this option.
    – eyal karni
    Commented Aug 30, 2019 at 23:10

2 Answers 2

2

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.

0

Sadly, this is what I came up with.

Find the current process, the process parent, and use ps ax to obtain the path. Meant to work in mac. in Linux, it is easier with `/proc/XXX/cmdline'.

PY import vim
PY import os
PY pid=os.getpid()
PY kk=os.popen('ps -o ppid= -p ' + str(pid)).read()
PY kk=kk.replace('\n','')
PY tt=("let cmdline=system('ps ax | grep \""+kk + "\" | grep nvim | grep -v grep | /usr/bin/sed \"s/^.*\///\"')")
PY vim.command(tt)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.