Determining if Vim is running in text-console mode or X Windows

2,841 views
Skip to first unread message

Steve Laurie

unread,
Feb 1, 2011, 4:23:57 AM2/1/11
to v...@vim.org
Hi,

I've searched Vim help and Google as well as done lots of
experimentation with no luck.

Basically, what I'm trying to do is put something in my .vimrc file that
can determine if I'm starting Vim in text-console mode or in gnome-terminal.

The reason I need to do this is because I log into FreeBSD in
text-console. If I go into Xorg, I use startx and I use gnome-terminal
in Window Maker.

I need to determine if I'm in text-console mode so I can set the term
setting in my .vimrc file to cons25 and if I startx, I need to set
t_Co=256, term to xterm-256color and set the theme to a nice 256 color
theme.
If I try to use the color settings in console mode, the screen turns red
and the text flashes.


I've tried the following:

if has("x11")
set term=xterm-256color
set t_Co=256
colorscheme calmar256-dark
else
set term=cons25
colorscheme myvim
endif

This didn't work. I've also tried:
if has("gui_running")
if has("gui_gtk2")

nothing works. I've even tried the reverse ways like "if !has", but
still, no luck.


Can someone help me out on this?

TIA,
Steve Laurie


Karthick Gururaj

unread,
Feb 1, 2011, 4:58:42 AM2/1/11
to vim...@googlegroups.com, v...@vim.org
On Tue, Feb 1, 2011 at 2:53 PM, Steve Laurie <mr.steve...@gmail.com> wrote:
> Hi,
>
> I've searched Vim help and Google as well as done lots of experimentation
> with no luck.
>
> Basically, what I'm trying to do is put something in my .vimrc file that can
> determine if I'm starting Vim in text-console mode or in gnome-terminal.

> The reason I need to do this is because I log into FreeBSD in text-console.
> If I go into Xorg, I use startx and I use gnome-terminal in Window Maker.

Why not keep GUI specific settings .gvimrc? Let .vimrc have only the
settings that are common to both GUI/text versions..

>
> I need to determine if I'm in text-console mode so I can set the term
> setting in my .vimrc file to cons25 and if I startx, I need to set t_Co=256,
> term to xterm-256color and set the theme to a nice 256 color theme.
> If I try to use the color settings in console mode, the screen turns red and
> the text flashes.
>
>
> I've tried the following:
>
> if has("x11")
>    set term=xterm-256color
>    set t_Co=256
>    colorscheme calmar256-dark
> else
>    set term=cons25
>    colorscheme myvim
> endif
>
> This didn't work. I've also tried:
> if has("gui_running")
> if has("gui_gtk2")
>
> nothing works. I've even tried the reverse ways like "if !has", but still,
> no luck.

if has("gui") ?

Christian Brabandt

unread,
Feb 1, 2011, 5:00:15 AM2/1/11
to vim...@googlegroups.com

So you basically distinguish it by inspecting your $TERM variable. First
determine in both situations what your $TERM is set to, then put something
like this in your .vimrc

if &term=~'linux'
" This is the console on linux. I don't know for FreeBSD
" set your console settings here.
elseif &term=~'xterm'
" Put your settings for X11 mode here
endif

regards,
Christian

Karthick Gururaj

unread,
Feb 1, 2011, 5:07:43 AM2/1/11
to vim...@googlegroups.com
On Tue, Feb 1, 2011 at 3:30 PM, Christian Brabandt <cbl...@256bit.org> wrote:
[snip]

> So you basically distinguish it by inspecting your $TERM variable. First
> determine in both situations what your $TERM is set to, then put something
> like this in your .vimrc

Ah, I mis-read the post. Clearer now :)

Steve Laurie

unread,
Feb 1, 2011, 7:57:38 AM2/1/11
to vim...@googlegroups.com
Thanks for your help. unfortunately, none of these suggestions work.

If I had some way of changing $TERM from cons25 to xterm-256color when
Xorg starts up, that would work.
I tried putting export TERM="xterm-256color" in my .xinitrc file but it
doesn't change.
I also tried exporting it from Window Maker's
~/GNUstep/Library/WindowMaker/autostart file but that didn't work either.

I can't get it to change from cons25


Ben Schmidt

unread,
Feb 1, 2011, 8:24:10 AM2/1/11
to vim...@googlegroups.com, Steve Laurie
>>> So you basically distinguish it by inspecting your $TERM variable. First
>>> determine in both situations what your $TERM is set to, then put something
>>> like this in your .vimrc
>> Ah, I mis-read the post. Clearer now :)
>>
> Thanks for your help. unfortunately, none of these suggestions work.
>
> If I had some way of changing $TERM from cons25 to xterm-256color when Xorg starts
> up, that would work.
> I tried putting export TERM="xterm-256color" in my .xinitrc file but it doesn't
> change.
> I also tried exporting it from Window Maker's
> ~/GNUstep/Library/WindowMaker/autostart file but that didn't work either.
>
> I can't get it to change from cons25

This seems very strange. $TERM is usually set appropriately by your
terminal program in X.

Are you sure something else isn't changing it, e.g. ~/.profile,
~/.bashrc, /etc/profile (or other rc files for your shell)? Or is there
an option in the GUI for your X terminal that has been incorrectly set
to make $TERM something it shouldn't be?

You are using Vim in a terminal, either in the console or in an X
terminal, right? Not Gvim.

Ben.

Marvin Renich

unread,
Feb 1, 2011, 9:51:12 AM2/1/11
to vim...@googlegroups.com
* Steve Laurie <mr.steve...@gmail.com> [110201 06:57]:

> Thanks for your help. unfortunately, none of these suggestions work.

Hmm. You say in your original message that you have tried
has("gui_running"). This works for me (and has for a long time).

As a test, I put

let my_has_gui_running = has("gui_running")

early in my .vimrc. If I start vim from the command line and type

:echo my_has_gui_running

I get 0. If I do the same with gvim (or vim -g) I get 1. Using

if has("gui_running")
echo "has gui_running"
else
echo "does not have gui_running"
endif

in .vimrc prints (to the terminal before setting up the vim screen) the
correct string for both vim and gvim.

I am using vim 7.2.445 from Debian squeeze.

...Marvin

Ivan Krasilnikov

unread,
Feb 1, 2011, 10:05:09 AM2/1/11
to vim...@googlegroups.com
Check if $DISPLAY is not empty.

> --
> 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
>

Steve Laurie

unread,
Feb 1, 2011, 10:17:40 AM2/1/11
to vim...@googlegroups.com
Hi Marvin,

On my system, "gui_running" only distinguishes between Vim and gvim.

I put let my_has_gui_running = has("gui_running") early in my ~/.vimrc
file and did :echo my_has_gui_running

In Vim run from the black and white tty text console, I get 0;
In Vim run insde of gnome-terminal, I also get 0;
In gvim, I get a 1

That's why it's not working. I'm trying to distinguish between tty text
mode and X mode.
Not between Vim and gvim.

Regards,
Steve


the problem with this is if I use "gui_running"

Marvin Renich

unread,
Feb 1, 2011, 4:24:46 PM2/1/11
to vim...@googlegroups.com
* Steve Laurie <mr.steve...@gmail.com> [110201 09:17]:

> Hi Marvin,
>
> On my system, "gui_running" only distinguishes between Vim and gvim.
>
> I put let my_has_gui_running = has("gui_running") early in my
> ~/.vimrc file and did :echo my_has_gui_running
>
> In Vim run from the black and white tty text console, I get 0;
> In Vim run insde of gnome-terminal, I also get 0;
> In gvim, I get a 1
>
> That's why it's not working. I'm trying to distinguish between tty
> text mode and X mode.
> Not between Vim and gvim.
>
> Regards,
> Steve

Ah, you want to distinguish between running in a terminal on a text
console and running in a terminal emulator under X. Does this do what
you want?

if $DISPLAY == ""
let is_x_terminal = 0
else
let is_x_terminal = 1
endif

...Marvin

Steve Laurie

unread,
Feb 1, 2011, 4:45:38 PM2/1/11
to vim...@googlegroups.com
Thanks for your help Marvin.

Both in text console and in gnome-terminal, :echo $DISPLAY returns 0
(int, not a string) - still no difference between tty text mode and
terminal emulator.

I have had some success using:

if &term=~'cons25'
colorscheme myvim
elseif &term=~'xterm'
set t_Co=256
colorscheme calmar256-dark
endif

but the strange thing is, if $TERM is set to xterm and not
xterm-256color, gkrellm locks up... something to do with the email part
of it.

If I could just find where xterm is being set and change it to
xterm-256color without altering tty mode's TERM settings (i.e. cons25)
I'd be laughing.

Thx,
Steve

Benjamin R. Haskell

unread,
Feb 1, 2011, 5:25:31 PM2/1/11
to vim...@googlegroups.com
On Wed, 2 Feb 2011, Steve Laurie wrote:

> [...]


>
> Both in text console and in gnome-terminal, :echo $DISPLAY returns 0
> (int, not a string) - still no difference between tty text mode and
> terminal emulator.

Maybe I'm misunderstanding something in your response, but you shouldn't
test the return value of echo. Test the $DISPLAY env var:

if exists('$DISPLAY')
" running under X11
else
" running on console
endif


> I have had some success using:
>
> if &term=~'cons25'
> colorscheme myvim
> elseif &term=~'xterm'
> set t_Co=256
> colorscheme calmar256-dark
> endif
>
> but the strange thing is, if $TERM is set to xterm and not xterm-256color,
> gkrellm locks up... something to do with the email part of it.
>
> If I could just find where xterm is being set and change it to xterm-256color
> without altering tty mode's TERM settings (i.e. cons25) I'd be laughing.

For me, here are some environment variables that are different between a
shell running on a virtual console and a shell session running under
rxvt-unicode under X11:

X11: TERM=rxvt-unicode256 console: TERM=linux
X11: SHLVL=4 console: SHLVL=1

These are not present at all on a vt:

COLORTERM=rxvt
DISPLAY=:0.0
WINDOWID=92274697
WINDOWPATH=7
XAUTHLOCALHOSTNAME=bhaskell-pc
XAUTHORITY=/home/bhaskell/.Xauthority

You shouldn't have a DISPLAY variable set under a console session, as
it's meaningless. (Doesn't mean you won't get one -- might be
erroneously set up by default.)

You're also right to question a TERM=cons25 when running under X11; that
sounds as broken as $DISPLAY. Do you have something in a .profile,
.bash_profile, or .bashrc?

Actually, on a FreeBSD box that I have access to, the .profile file from
/etc/skel/ has:

TERM=cons25; export TERM

Maybe that's the problem.

Otherwise, I would think $SHLVL and $WINDOWID would be your best
chances.

e.g.

if exists('$SHLVL') && $SHLVL < 2
" probably running on console
else
" running under X11
endif

or:

if exists('$WINDOWID')
" under X11
else
" on console
endif

--
Best,
Ben

Steve Laurie

unread,
Feb 1, 2011, 5:52:00 PM2/1/11
to vim...@googlegroups.com
Thanks Ben,

the $DISPLAY version doesn't work (I get red screen and flashing text in
text mode)

however, both $WINDOWID and ('$SHLVL') && $SHLVL < 2 do work.

I think I like the $WINDOWID version best. It feels ... ??cleaner??. I
don't know why because, without looking it up, I don't even know what
$SHLVL is. I suspect it's shell level and if we're running in the first
level of the shell, then it's likely to be text console given that
that's what I first log into. Am I right?


Ben Schmidt

unread,
Feb 2, 2011, 12:27:49 AM2/2/11
to vim...@googlegroups.com, Steve Laurie
>>> but the strange thing is, if $TERM is set to xterm and not xterm-256color,
>>> gkrellm locks up... something to do with the email part of it.
>>>
>>> If I could just find where xterm is being set and change it to xterm-256color
>>> without altering tty mode's TERM settings (i.e. cons25) I'd be laughing.

Maybe just whack something in .bashrc or .profile like

if [ "$TERM" = xterm ] ; then
export TERM=xterm-256color
fi

?

> I think I like the $WINDOWID version best. It feels ... ??cleaner??. I don't know
> why because, without looking it up, I don't even know what $SHLVL is. I suspect
> it's shell level and if we're running in the first level of the shell, then it's
> likely to be text console given that that's what I first log into. Am I right?

Yep. If you're in bash and start another bash and then echo $SHLVL,
it'll have increased. Maybe also if you source scripts, or start
scripts as executables using shebangs. I can't remember the details. But
you're basically right. It's certainly not all that reliable a thing to
use for this purpose!

Ben.

mat

unread,
Feb 2, 2011, 9:07:55 AM2/2/11
to vim_use
has() is good, problem is If your in a terminal (gnome-terminal), your
not running any "gui" vim.
I personally would try to check outside vim, maybe env $TERM ?
Reply all
Reply to author
Forward
0 new messages