Bash in :shell vs :terminal

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Bash in :shell vs :terminal

Jason Franklin
I would like for bash to be able to discern whether it is being run with :shell or with :term.  I would assume this would need to be done with an environment variable.  Does this feature to do something like this already exist?

-- Jason

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Chr. von Stuckrad
On Wed, 30 May 2018, Jason Franklin wrote:

> I would like for bash to be able to discern whether it is being run with :shell or with :term.  I would assume this would need to be done with an environment variable.  Does this feature to do something like this already exist?

The method I know to use in scripts
is the expression (mostly in 'if':
... [ -t 0 -a -t 1 ] ...
Meanig '-t N' (asks for STDIN 0 and STDOUT 1):
'is filedescriptor N connected to a terminal ?'

So e.g. you can write in all (non C shell)shells:
-------------------------------------------------
if [ -t 0 -a -t 1 ]; then
        do_terminal
else
        non_terminal
fi
-------------------------------------------------
alternatively:
-------------------------------------------------
[ -t 0 -a -t 1 ] && do_terminal || non_terminal
-------------------------------------------------

AND on modern shells you better use the shell-internal
test with [[ and ]], instead of [ and ], which was
using the real program 'test'.
BUT 'or' and 'and' are different then:
-------------------------------------------------
     positively             negatively
 [ -t 0 -a -t 1 ]   !=   [ ! -t 0 -o ! -t 1 ]
[[ -t 0 && -t 1 ]]  !=  [[ ! -t 0 || ! -t 1 ]]
-------------------------------------------------

May be this is all you need?

I end lots of my scripts with code for
"if called in terminal, list on pager",
because I always forget "... | less" (:-)
or "if input not a terminal, do not ask".

--
Christoph von Stuckrad    * * | also XMPP = |Mail <[hidden email]> \
Freie Universitaet Berlin |/_*| 'jabber' via|Tel(Mo.,Mi.):+49 30 838-75 459|
IT Mathematik & Informatik|\ *|stucki@jabber|  (Di,Do,Fr):+49 30 77 39 6600|
Takustr. 9 / 14195 Berlin * * |.fu-berlin.de|Fax(home):   +49 30 77 39 6601/
                                           

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
I am aware of "test -t", but I don't see how it helps here.

I want the prompt to adjust based on whether I'm using :term or :sh.  So,
the method needs to work in interactive shell sessions.

-- JF

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Marcin Szamotulski-3
Hi Jason,

You can set `set shell=/bin/bash\ --rcfile=$HOME/.vim_bashrc` and set the environment variable there (and source your ~/.bashrc file as well).

Best regards,
Marcin Szamotulski


​Sent with ProtonMail Secure Email.​

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On May 30, 2018 4:06 PM, Jason Franklin <[hidden email]> wrote:

> I am aware of "test -t", but I don't see how it helps here.
>
> I want the prompt to adjust based on whether I'm using :term or :sh. So,
>
> the method needs to work in interactive shell sessions.
>
> -- JF
>
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> --
>
> You received this message from the "vim_use" maillist.
>
> Do not top-post! Type your reply below the text you are replying to.
>
> For more information, visit http://www.vim.org/maillist.php
>
>
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> You received this message because you are subscribed to the Google Groups "vim_use" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
>
> For more options, visit https://groups.google.com/d/optout.


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
Hey Martin,

This doesn't actually help me.  The 'shell' setting is used for both types of shell sessions spawned from Vim.

I'm looking to be able to distinguish between bash sessions run in a Vim terminal window or in a normal xterm.

For example, I would like a different prompt if bash is started with :terminal (a Vim job) as opposed to being started with :shell (a forked process).

Thanks,
Jason

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
I'm actually beginning to think that this isn't possible.  Even if I was able to distinguish at the level of the child shell, I don't think I could continue that for, say, a subshell of that shell.

In other words, knowing something is a job with an environment variable won't tell you anything beyond the immediate child.

The one solution I can think of would be to namespace the device files for Vim terminal windows. It's worth asking on vim_dev.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Christian Brabandt
In reply to this post by Jason Franklin

On Mi, 30 Mai 2018, Jason Franklin wrote:

> I would like for bash to be able to discern whether it is being run with :shell or with :term.  I would assume this would need to be done with an environment variable.  Does this feature to do something like this already exist?

Check for the $VIM_SERVERNAME environment variable


Best,
Christian
--
Auf welcher Gesetzestafel steht: Die heiligen Gefühle der Theisten
müssen respektiert werden, die heiligen Gefühle der A-Theisten aber
nicht?
                -- Ludwig Marcuse

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
Hey Christian,

Unfortunately, $VIM_SERVERNAME is not always available.  This happens when Vim is compiled without +clientserver.

Thanks,
Jason

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Tim Ferguson
In reply to this post by Jason Franklin

On 30/05/18 13:48, Jason Franklin wrote:
> I would like for bash to be able to discern whether it is being
> run with :shell or with :term.  I would assume this would need
> to be done with an environment variable.  Does this feature to
> do something like this already exist?



You could look at the heirarchy of the current session:


if [ -v $( ps | grep vim ) ]
then echo "I'm a terminal session"
else echo "I'm in a shell"
fi


I am not sure if this is purely a bash feature, though!

Tim F


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
Hey Tim,

This is not as useful because vim can be invoked by many names (e.g., a vi alias or link).

Also, I don't think this works the way you think.  In my vim terminal window, I don't have a listing for vim in ps output.

Something more robust is needed.

-- JF

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Christian Brabandt
In reply to this post by Jason Franklin

On Mo, 04 Jun 2018, Jason Franklin wrote:

> Unfortunately, $VIM_SERVERNAME is not always available.  This happens
> when Vim is compiled without +clientserver.

Good catch. I did not think of it and I have been using the
VIM_SERVERNAME environment variable for this purpose.

@Bram, can we have a environment variable indicating that we are in a
Vim Terminal? Possibly which also includes the Vim patchlevel?

That is also quite useful, when deciding whether to use the terminal
API.

Best,
Christian
--

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
Christian,

I don't think an environment variable is really going to solve this problem.

I created an issue on github for this idea, you can see my explanation there:

https://github.com/vim/vim/issues/2981

That may be the solution we're looking for, if it's possible. Also, note that
passing the patch level isn't really necessary since a user can define an
environment variable on their own for this purpose.

The real problem is that descendant processes need to know that their
controlling terminal is a Vim terminal window.

Thanks,
Jason

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Bram Moolenaar
In reply to this post by Christian Brabandt

Christian wrote:

> On Mo, 04 Jun 2018, Jason Franklin wrote:
>
> > Unfortunately, $VIM_SERVERNAME is not always available.  This happens
> > when Vim is compiled without +clientserver.
>
> Good catch. I did not think of it and I have been using the
> VIM_SERVERNAME environment variable for this purpose.
>
> @Bram, can we have a environment variable indicating that we are in a
> Vim Terminal? Possibly which also includes the Vim patchlevel?
>
> That is also quite useful, when deciding whether to use the terminal
> API.

I suppose we can add VIM_TERMINAL for that.


--
How To Keep A Healthy Level Of Insanity:
6. In the memo field of all your checks, write "for sexual favors".

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
> I suppose we can add VIM_TERMINAL for that.

Hey @Bram,

Please take a look at the following issue before simply using an environment variable:

https://github.com/vim/vim/issues/2981

I've given this some thought, and an environment variable wouldn't work in all cases.

I'm not sure, though, if my proposed solution is possible.

Thanks,
Jason

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Christian Brabandt
In reply to this post by Jason Franklin

On Mo, 04 Jun 2018, Jason Franklin wrote:

> That may be the solution we're looking for, if it's possible. Also, note that
> passing the patch level isn't really necessary since a user can define an
> environment variable on their own for this purpose.

Well the User can also set a environment variable BASH_VERSION and
overwrite the existing one. However that is not a good idea. Having a
variable which returns e.g. v:version + patchlevel allows scripts to
handle differences in Vim patchlevel, like the prompt buffer that is
about to be developed now.

>
> The real problem is that descendant processes need to know that their
> controlling terminal is a Vim terminal window.

That does not work on Windows and I am not even sure, that it is Vims
responsibility to assign a device name. So no, I don't consider this to
be a stable solution.


Best,
Christian
--
Frage sich doch jeder, mit welchem Organ er allenfalls in seine
Zeit einwirken kann und wird.
                -- Goethe, Maximen und Reflektionen, Nr. 554

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Jason Franklin
Thanks, Christian.

I wasn't sure it would work, but I figured it was worth asking.

As I mentioned in a comment on the issue, I'm fine with a VIM_TERMINAL variable so long as it allows me to distinguish when a terminal window is in use.

Thanks, again!

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Chr. von Stuckrad
On Mon, 04 Jun 2018, Jason Franklin wrote:

> ... I'm fine with a VIM_TERMINAL variable so long as it allows me to distinguish when a terminal window is in use.

I think, it boils down to one kind of typical situation,
and an otherwise never reliably solvable problem.

If you only need to know, which way the 'currently starting'
program runs under 'vim', you can use any 'switch', which
is inherited by the child program.

BUT if the programs running 'under vim' itself forks off
decendants, you sooner or later will be lost, if/because
the 'switch' is passed on to the children, and they can
somehow loose/change/reset their association to the
terminal (like starting a terminalprogram themselves),
while the inherited 'switch' still stays the same.

Namespacing may work, because you fix the atribute to
the pty, and any decendant then can find out, whether
it is on such a terminal, but I fear it only works
on linux, and I don't now whether you'd need root.

Stucki

--
Christoph von Stuckrad    * * | also XMPP = |Mail <[hidden email]> \
Freie Universitaet Berlin |/_*| 'jabber' via|Tel(Mo.,Mi.):+49 30 838-75 459|
IT Mathematik & Informatik|\ *|stucki@jabber|  (Di,Do,Fr):+49 30 77 39 6600|
Takustr. 9 / 14195 Berlin * * |.fu-berlin.de|Fax(home):   +49 30 77 39 6601/
                                           

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Bash in :shell vs :terminal

Christian Brabandt
In reply to this post by Bram Moolenaar

On Mo, 04 Jun 2018, Bram Moolenaar wrote:
> I suppose we can add VIM_TERMINAL for that.
I made a patch:
https://github.com/chrisbra/vim-win32-installer/blob/master/patch/vim_terminal.patch

Best,
Christian
--
Wir wissen zwar nicht wo wir hinwollen, wollen aber als Erste da sein.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.