Get PID inside vim

1,374 views
Skip to first unread message

Zdenek Sekera

unread,
Feb 20, 2008, 4:07:57 AM2/20/08
to vim...@googlegroups.com
I need to get the PID of the process that runs vim,
something is easy in a shell: 'echo $$'.
However, when inside vim the '$$' gives an error.
Would anyone know the trick of getting it or do I need
to just store it in an environmental variable:

export MYPID=$(echo $$)
vim
and then inside vim ':echo $MYPID' ??

Can be done but it's not awfully practical.
Better ideas?

---Zdenek

Milan Vancura

unread,
Feb 20, 2008, 4:34:31 AM2/20/08
to vim...@googlegroups.com

As variable $PPID is not set in vim I don't see any direct possibility.
So it depends on what do you need and what can you do at the destination
machine/system. Do you have an access to the shell before running vim? If yes,
your solution seems the easiest way how to do it. If not, try to parse output
of 'ps -l' where PPID is in the fifth column. For example, for me (at Linux)
the following works:

:!ps --no-heading -l $PPID | ( read a a a a ppid a ; echo $ppid )

$PPID is PID of vim (parent of the shell running :! command is vim itself
$ppid is PID parent of parent of that shell - the pid of the process running
vim, as you need


Milan Vancura
--
Milan Vancura, Prague, Czech Republic, Europe

Patrick Texier

unread,
Feb 20, 2008, 5:10:49 AM2/20/08
to vim...@googlegroups.com
On Wed, 20 Feb 2008 10:34:31 +0100, Milan Vancura wrote:

> As variable $PPID is not set in vim I don't see any direct possibility.

Using an interface?

======= getpid.vim ============
if !has("perl")
finish
endif
function! GetPID()
perl VIM::DoCommand('let s:pid =' . $$)
return s:pid
endfunction
=================================

:so getpid.vim
:echo GetPid()
293153
--
Patrick Texier

vim:syntax=mail:ai:ts=4:et:tw=72

Bram Moolenaar

unread,
Feb 20, 2008, 5:28:25 AM2/20/08
to Zdenek Sekera, vim...@googlegroups.com

Zdenek Sekera wrote:

Adding a getpid() function has been in the todo list for ages. It's
really easy to implement, since we already have the mch_get_pid()
function. I'll do that now.

--
hundred-and-one symptoms of being an internet addict:
35. Your husband tells you he's had the beard for 2 months.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Zdenek Sekera

unread,
Feb 20, 2008, 7:39:11 AM2/20/08
to Br...@moolenaar.net, vim...@googlegroups.com

> -----Original Message-----
> From: Br...@moolenaar.net [mailto:Br...@moolenaar.net]
> Sent: 20 February 2008 11:28
> To: Zdenek Sekera
> Cc: vim...@googlegroups.com
> Subject: Re: Get PID inside vim
>
>
> Zdenek Sekera wrote:
>
> > I need to get the PID of the process that runs vim,
> > something is easy in a shell: 'echo $$'.
> > However, when inside vim the '$$' gives an error.
> > Would anyone know the trick of getting it or do I need
> > to just store it in an environmental variable:
> >
> > export MYPID=$(echo $$)
> > vim
> > and then inside vim ':echo $MYPID' ??
> >
> > Can be done but it's not awfully practical.
> > Better ideas?
>
> Adding a getpid() function has been in the todo list for ages. It's
> really easy to implement, since we already have the mch_get_pid()
> function. I'll do that now.

Excellent, thanks! Will wait...

---Zdenek

Reply all
Reply to author
Forward
0 new messages