Loading ...
Sorry, an error occurred while loading the content.

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

Expand Messages
  • Christian Brabandt
    ... 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
    Message 1 of 15 , Feb 1, 2011
    • 0 Attachment
      On Tue, February 1, 2011 10:23 am, Steve Laurie 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.
      >
      > 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?

      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

      --
      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
    • Karthick Gururaj
      On Tue, Feb 1, 2011 at 3:30 PM, Christian Brabandt wrote: [snip] ... Ah, I mis-read the post. Clearer now :) -- You received this message
      Message 2 of 15 , Feb 1, 2011
      • 0 Attachment
        On Tue, Feb 1, 2011 at 3:30 PM, Christian Brabandt <cblists@...> 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 :)

        --
        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
        ... 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,
        Message 3 of 15 , Feb 1, 2011
        • 0 Attachment
          On 02/01/11 21:07, Karthick Gururaj wrote:
          > On Tue, Feb 1, 2011 at 3:30 PM, Christian Brabandt<cblists@...> 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 :)
          >
          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


          --
          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
        • Ben Schmidt
          ... 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,
          Message 4 of 15 , Feb 1, 2011
          • 0 Attachment
            >>> 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.



            --
            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
          • Marvin Renich
            ... 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
            Message 5 of 15 , Feb 1, 2011
            • 0 Attachment
              * Steve Laurie <mr.steven.laurie@...> [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

              --
              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
            • Ivan Krasilnikov
              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
              Message 6 of 15 , Feb 1, 2011
              • 0 Attachment
                Check if $DISPLAY is not empty.

                On Tue, Feb 1, 2011 at 12:23, Steve Laurie <mr.steven.laurie@...> 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.
                >
                > 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
                >
                >
                > --
                > 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 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
                ... 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
                Message 7 of 15 , Feb 1, 2011
                • 0 Attachment
                  On 02/02/11 01:51, Marvin Renich wrote:
                  > * Steve Laurie<mr.steven.laurie@...> [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
                  >
                  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"

                  --
                  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
                • Marvin Renich
                  ... 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
                  Message 8 of 15 , Feb 1, 2011
                  • 0 Attachment
                    * Steve Laurie <mr.steven.laurie@...> [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

                    --
                    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
                    ... 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
                    Message 9 of 15 , Feb 1, 2011
                    • 0 Attachment
                      On 02/02/11 08:24, Marvin Renich wrote:
                      > * Steve Laurie<mr.steven.laurie@...> [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
                      >
                      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

                      --
                      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
                    • Benjamin R. Haskell
                      ... 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 )
                      Message 10 of 15 , Feb 1, 2011
                      • 0 Attachment
                        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

                        --
                        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
                        ... Thanks Ben, the $DISPLAY version doesn t work (I get red screen and flashing text in text mode) however, both $WINDOWID and ( $SHLVL ) && $SHLVL
                        Message 11 of 15 , Feb 1, 2011
                        • 0 Attachment
                          On 02/02/11 09:25, Benjamin R. Haskell wrote:
                          > 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
                          >
                          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?




                          --
                          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
                        • Ben Schmidt
                          ... Maybe just whack something in .bashrc or .profile like if [ $TERM = xterm ] ; then export TERM=xterm-256color fi ? ... Yep. If you re in bash and start
                          Message 12 of 15 , Feb 1, 2011
                          • 0 Attachment
                            >>> 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.



                            --
                            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
                          • mat
                            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
                            Message 13 of 15 , Feb 2, 2011
                            • 0 Attachment
                              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 ?

                              On Feb 1, 4:23 am, Steve Laurie <mr.steven.lau...@...> 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.
                              >
                              > 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

                              --
                              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
                            Your message has been successfully submitted and would be delivered to recipients shortly.