Quantcast

Changing Cursor in Terminal Vim...Mode Recognition?

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Changing Cursor in Terminal Vim...Mode Recognition?

John Magolske
In migrating from gvim over to terminal vim, one thing I miss
is a cursor that changes to a vertical bar while in insert-mode.

Xterm doesn't have the capability of changing the text-cursor's
shape, but its color can be changed with escape sequences.
For example, this changes the cursor to blue, then green:

:!echo -ne "\033]12;RoyalBlue2\007"
:!echo -ne "\033]12;green\007"

Is there a way to tie this to something in the .vimrc that would
recognize whether vim is in insert-mode or normal-mode and
automatically change the cursor color accordingly?

Thanks,

John
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Bram Moolenaar

John Magolske wrote:

> In migrating from gvim over to terminal vim, one thing I miss
> is a cursor that changes to a vertical bar while in insert-mode.
>
> Xterm doesn't have the capability of changing the text-cursor's
> shape, but its color can be changed with escape sequences.
> For example, this changes the cursor to blue, then green:
>
> :!echo -ne "\033]12;RoyalBlue2\007"
> :!echo -ne "\033]12;green\007"
>
> Is there a way to tie this to something in the .vimrc that would
> recognize whether vim is in insert-mode or normal-mode and
> automatically change the cursor color accordingly?

See ":help termcap-cursor-shape".

--
hundred-and-one symptoms of being an internet addict:
146. You experience ACTUAL physical withdrawal symptoms when away
     from your 'puter and the net.

 /// Bram Moolenaar -- [hidden email] -- 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://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

John Magolske
>  > Is there a way to tie this to something in the .vimrc that would
>>  recognize whether vim is in insert-mode or normal-mode and
>>  automatically change the cursor color accordingly?
>
>See ":help termcap-cursor-shape".

This gets "Sorry, no help for termcap-cursor-shape"
Does it have to do with not having the +cursorshape feature?
Is Vim7 required? (I'm using 6.4)

Searching around, I found:
http://www.polarhome.com/cvs/CVSROOT/vim7/runtime/doc/term.txt
Which references needing the +cursorshape feature, describing the scheme:

if &term =~ "xterm"
   let &t_SI = "\033]12;purple\007"
   let &t_EI = "\033]12;blue\007"
endif

With this in my vimrc, upon launching vim:
Error detected... E355: Unknown option: t_SI

I can't seem to figure out how to compile vim with +cursorshape...
by adjusting something in the makefile? Or maybe in src/feature.h?

Regards, John

:version
VIM - Vi IMproved 6.4 (2005 Oct 15, compiled Jan  1 2006 23:21:58)
Compiled by [hidden email]
Huge version without GUI.
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent
+clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
+cryptv +cscope +dialog_con +diff +digraphs -dnd -ebcdic +emacs_tags
+eval +ex_extra
  +extra_search +farsi +file_in_path +find_in_path +folding -footer
+fork() -gettext
-hangul_input +iconv +insert_expand +jumplist +keymap +langmap
+libcall +linebreak
+lispindent +listcmds +localmap +menu +mksession +modify_fname +mouse
-mouseshape
+mouse_dec -mouse_gpm -mouse_jsbterm +mouse_netterm +mouse_xterm +multi_byte
+multi_lang -netbeans_intg -osfiletype +path_extra -perl +postscript +printer
-python +quickfix +rightleft -ruby +scrollbind +signs +smartindent -sniff
+statusline -sun_workshop +syntax +tag_binary +tag_old_static
-tag_any_white -tcl
+terminfo +termresponse +textobjects +title -toolbar +user_commands +vertsplit
+virtualedit +visual +visualextra +viminfo +vreplace +wildignore
+wildmenu +windows
+writebackup +X11 +xfontset -xim +xsmp_interact +xterm_clipboard -xterm_save
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

A.J.Mechelynck
John Magolske wrote:
>>  > Is there a way to tie this to something in the .vimrc that would
>>>  recognize whether vim is in insert-mode or normal-mode and
>>>  automatically change the cursor color accordingly?
>>
>> See ":help termcap-cursor-shape".
>
> This gets "Sorry, no help for termcap-cursor-shape"
> Does it have to do with not having the +cursorshape feature?
> Is Vim7 required? (I'm using 6.4)

The +cursorshape feature is required. For some reason, none of my Vim
builds for W32 mention either +cursorshape or -cursorshape in their
":version" listings, but if I ask ":echo has('cursorshape')" the answer
is 1 in version 7 builds and 0 in version 6.4 builds: so it seems that
the answer to your other question is also yes. Here is what I have in v7
$VIMRUNTIME/doc/term.txt under ":help term-cursor-shape" which is also
":help termcap-cursor-color":

                                *termcap-cursor-shape* *termcap-cursor-color*
When Vim enters Insert mode the 't_SI' escape sequence is sent.  When
leaving Insert mode 't_EI' is used.  But only if both are defined.  This
can be used to change the shape or color of the cursor in Insert mode.
These are not standard termcap/terminfo entries, you need to set them
yourself.
Example for an xterm, this changes the color of the cursor: >
    if &term =~ "xterm"
        let &t_SI = "\<Esc>]12;purple\x7"
        let &t_EI = "\<Esc>]12;blue\x7"
    endif
NOTE: When Vim exits the shape for Normal mode will remain.  The shape
from before Vim started will not be restored.
{not available when compiled without the +cursorshape feature}




Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Changing Cursor in Terminal Vim...Mode Recognition?

Zdenek Sekera
In reply to this post by John Magolske
{...}

> These are not standard termcap/terminfo entries, you need to set them
> yourself.
> Example for an xterm, this changes the color of the cursor: >
>     if &term =~ "xterm"
> let &t_SI = "\<Esc>]12;purple\x7"
> let &t_EI = "\<Esc>]12;blue\x7"
>     endif
> NOTE: When Vim exits the shape for Normal mode will remain.  The shape
> from before Vim started will not be restored.
> {not available when compiled without the +cursorshape feature}

And is there a way to have it restored?

Regards,

---Zdenek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

A.J.Mechelynck
Zdenek Sekera wrote:

> {...}
>> These are not standard termcap/terminfo entries, you need to set them
>> yourself.
>> Example for an xterm, this changes the color of the cursor: >
>>     if &term =~ "xterm"
>> let &t_SI = "\<Esc>]12;purple\x7"
>> let &t_EI = "\<Esc>]12;blue\x7"
>>     endif
>> NOTE: When Vim exits the shape for Normal mode will remain.  The shape
>> from before Vim started will not be restored.
>> {not available when compiled without the +cursorshape feature}
>
> And is there a way to have it restored?
>
> Regards,
>
> ---Zdenek
>
>
>

I don't know. My guess (but it's only a guess) is: if you know what
codes to send to set that default, try using these codes in an ":echo"
statement at the VimLeave autocomand event.


Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Bram Moolenaar
In reply to this post by John Magolske

John Magolske wrote:

> >  > Is there a way to tie this to something in the .vimrc that would
> >>  recognize whether vim is in insert-mode or normal-mode and
> >>  automatically change the cursor color accordingly?
> >
> >See ":help termcap-cursor-shape".
>
> This gets "Sorry, no help for termcap-cursor-shape"
> Does it have to do with not having the +cursorshape feature?
> Is Vim7 required? (I'm using 6.4)

This is indeed new in Vim 7.  Sorry, I forgot about that.

I don't think there is a way to do this in Vim 6 (without mapping all
the keys that start and stop Insert mode).

--
hundred-and-one symptoms of being an internet addict:
163. You go outside for the fresh air (at -30 degrees) but open the
     window first to hear new mail arrive.

 /// Bram Moolenaar -- [hidden email] -- 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://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Changing Cursor in Terminal Vim...Mode Recognition?

Zdenek Sekera
In reply to this post by John Magolske
> >> Example for an xterm, this changes the color of the cursor: >
> >>     if &term =~ "xterm"
> >> let &t_SI = "\<Esc>]12;purple\x7"
> >> let &t_EI = "\<Esc>]12;blue\x7"
> >>     endif
> >> NOTE: When Vim exits the shape for Normal mode will
> remain.  The shape
> >> from before Vim started will not be restored.
> >> {not available when compiled without the +cursorshape feature}
> >
> > And is there a way to have it restored?
...
> I don't know. My guess (but it's only a guess) is: if you know what
> codes to send to set that default, try using these codes in an ":echo"
> statement at the VimLeave autocomand event.

I don't know the codes, unfortunately, but the idea of
using VimLeave is interesting. And why it's not done automatically in
vim is perhaps because there is no way
to guarantee it can be done generically for any terminal
emulator. Bram?

Does anyone know what codes need to be sent to at
least xterm to have the cursor shape/color restored on vim exit?

Cheers,

---Zdenek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Changing Cursor in Terminal Vim...Mode Recognition?

Bram Moolenaar

Zdenek Sekera wrote:

> > >> Example for an xterm, this changes the color of the cursor: >
> > >>     if &term =~ "xterm"
> > >> let &t_SI = "\<Esc>]12;purple\x7"
> > >> let &t_EI = "\<Esc>]12;blue\x7"
> > >>     endif
> > >> NOTE: When Vim exits the shape for Normal mode will
> > remain.  The shape
> > >> from before Vim started will not be restored.
> > >> {not available when compiled without the +cursorshape feature}
> > >
> > > And is there a way to have it restored?
> ...
> > I don't know. My guess (but it's only a guess) is: if you know what
> > codes to send to set that default, try using these codes in an ":echo"
> > statement at the VimLeave autocomand event.
>
> I don't know the codes, unfortunately, but the idea of using VimLeave
> is interesting. And why it's not done automatically in vim is perhaps
> because there is no way to guarantee it can be done generically for
> any terminal emulator. Bram?

The reason it can't be restored is that Vim doesn't know what the
original color was.

> Does anyone know what codes need to be sent to at
> least xterm to have the cursor shape/color restored on vim exit?

As far as I know it's impossible, there doesn't appear a way to obtain
the cursor color that is in use when Vim is started.

--
hundred-and-one symptoms of being an internet addict:
165. You have a web page burned into your glasses

 /// Bram Moolenaar -- [hidden email] -- 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://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Changing Cursor in Terminal Vim...Mode Recognition?

Zdenek Sekera
In reply to this post by John Magolske
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> Sent: 26 January 2006 11:43
...
> > Does anyone know what codes need to be sent to at
> > least xterm to have the cursor shape/color restored on vim exit?
>
> As far as I know it's impossible, there doesn't appear a way to obtain
> the cursor color that is in use when Vim is started.

That solves the problem :-)! Too bad.
Thanks.

---Zdenek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

mzyzik
All,

I want to raise another problem.
If I run an xterm with cursor blinking (which I do) and set
selection=inclusive (which I do), the selection that is shown is short
of one character. Specifically the character under the mouse cursor is
not getting highlighted, even though it should. The highlighting is like
when selection=exclusive, yet the behavior of the commands is inclusive.

--Matt

P. S. Does anyone know the escape codes to change cursor shapes?

On Thu, Jan 26, 2006 at 02:07:19PM +0100, Zdenek Sekera wrote:

> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> > Sent: 26 January 2006 11:43
> ...
> > > Does anyone know what codes need to be sent to at
> > > least xterm to have the cursor shape/color restored on vim exit?
> >
> > As far as I know it's impossible, there doesn't appear a way to obtain
> > the cursor color that is in use when Vim is started.
>
> That solves the problem :-)! Too bad.
> Thanks.
>
> ---Zdenek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

John Magolske
In reply to this post by A.J.Mechelynck
Thank's for the replies...looks like it's time to move on to #7.

John

>The +cursorshape feature is required. For some reason, none of my Vim
>builds for W32 mention either +cursorshape or -cursorshape in their
>":version" listings, but if I ask ":echo has('cursorshape')" the answer
>is 1 in version 7 builds and 0 in version 6.4 builds: so it seems that
>the answer to your other question is also yes.

>This is indeed new in Vim 7.  Sorry, I forgot about that.
>
>I don't think there is a way to do this in Vim 6 (without mapping all
>the keys that start and stop Insert mode).
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Bram Moolenaar
In reply to this post by mzyzik

Matt Mzyzik wrote:

> I want to raise another problem.
> If I run an xterm with cursor blinking (which I do) and set
> selection=inclusive (which I do), the selection that is shown is short
> of one character. Specifically the character under the mouse cursor is
> not getting highlighted, even though it should. The highlighting is like
> when selection=exclusive, yet the behavior of the commands is inclusive.

Vim doesn't highlight the character under the cursor, because there are
several terminals where this causes trouble (esp. that the cursor
disappears when inverting twice).  You will just have to remember that
the character under the cursor is included if 'selection' is
"inclusive".

--
If Microsoft would build a car...
... Occasionally your car would die on the freeway for no
reason. You would have to pull over to the side of the road,
close all of the car windows, shut it off, restart it, and
reopen the windows before you could continue. For some reason
you would simply accept this.

 /// Bram Moolenaar -- [hidden email] -- 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://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Gary Johnson
In reply to this post by Bram Moolenaar
On 2006-01-26, Bram Moolenaar <[hidden email]> wrote:

> Zdenek Sekera wrote:
>
> > > >> Example for an xterm, this changes the color of the cursor: >
> > > >>     if &term =~ "xterm"
> > > >> let &t_SI = "\<Esc>]12;purple\x7"
> > > >> let &t_EI = "\<Esc>]12;blue\x7"
> > > >>     endif
> > > >> NOTE: When Vim exits the shape for Normal mode will
> > > remain.  The shape
> > > >> from before Vim started will not be restored.
> > > >> {not available when compiled without the +cursorshape feature}
> > > >
> > > > And is there a way to have it restored?
> > ...
> > > I don't know. My guess (but it's only a guess) is: if you know what
> > > codes to send to set that default, try using these codes in an ":echo"
> > > statement at the VimLeave autocomand event.
> >
> > I don't know the codes, unfortunately, but the idea of using VimLeave
> > is interesting. And why it's not done automatically in vim is perhaps
> > because there is no way to guarantee it can be done generically for
> > any terminal emulator. Bram?
>
> The reason it can't be restored is that Vim doesn't know what the
> original color was.
>
> > Does anyone know what codes need to be sent to at
> > least xterm to have the cursor shape/color restored on vim exit?
>
> As far as I know it's impossible, there doesn't appear a way to obtain
> the cursor color that is in use when Vim is started.

(I'm catching up on some old unread list e-mail.)

According to the ctlseqs.ms document that came with xterm-165:


    Operating System Controls
    OSC Ps ; Pt ST
    OSC Ps ; Pt BEL
           Set Text Parameters.  For colors and font, if Pt is a
           "?", the control sequence elicits a response which con-
           sists of the control sequence which would set the
           corresponding value. ....
           ...
             Ps = 1 2  -> Change colors starting with text cursor to
           Pt


I executed this command at a Linux shell prompt,

    echo -e '\033]12;?\007' ; read x

and got the following response:

    ^[]12;rgb:ffff/ffff/ffff^G

After setting the cursor to red and repeating the command, I got
this:

    ^[]12;rgb:ffff/0000/0000^G

So, xterm does have the mechanism to obtain the cursor color when
vim is started.  I just don't know how to obtain the terminal's
response from within vim and don't have the time to experiment
further at the moment.  I did discover, though, that you can echo
back the color in that same format, so no parsing of the terminal
response would be necessary.

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Bram Moolenaar

Gary Johnson wrote:

> > > Does anyone know what codes need to be sent to at
> > > least xterm to have the cursor shape/color restored on vim exit?
> >
> > As far as I know it's impossible, there doesn't appear a way to obtain
> > the cursor color that is in use when Vim is started.
>
> (I'm catching up on some old unread list e-mail.)
>
> According to the ctlseqs.ms document that came with xterm-165:
>
>
>     Operating System Controls
>     OSC Ps ; Pt ST
>     OSC Ps ; Pt BEL
>            Set Text Parameters.  For colors and font, if Pt is a
>            "?", the control sequence elicits a response which con-
>            sists of the control sequence which would set the
>            corresponding value. ....
>            ...
>              Ps = 1 2  -> Change colors starting with text cursor to
>            Pt
>
>
> I executed this command at a Linux shell prompt,
>
>     echo -e '\033]12;?\007' ; read x
>
> and got the following response:
>
>     ^[]12;rgb:ffff/ffff/ffff^G
>
> After setting the cursor to red and repeating the command, I got
> this:
>
>     ^[]12;rgb:ffff/0000/0000^G
>
> So, xterm does have the mechanism to obtain the cursor color when
> vim is started.  I just don't know how to obtain the terminal's
> response from within vim and don't have the time to experiment
> further at the moment.  I did discover, though, that you can echo
> back the color in that same format, so no parsing of the terminal
> response would be necessary.

Interesting, this must have been added recently.

I wonder what triggers the "rgb:" response.  It doesn't state somewhere
why this happens instead of the "ordinary" color numbers.  Perhaps
because xterm was compiled with more than 16 colors?

--
hundred-and-one symptoms of being an internet addict:
42. Your virtual girlfriend finds a new net sweetheart with a larger bandwidth.

 /// Bram Moolenaar -- [hidden email] -- 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://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing Cursor in Terminal Vim...Mode Recognition?

Gary Johnson
On 2006-04-12, Bram Moolenaar <[hidden email]> wrote:

> Gary Johnson wrote:
>
> > > > Does anyone know what codes need to be sent to at
> > > > least xterm to have the cursor shape/color restored on vim exit?
> > >
> > > As far as I know it's impossible, there doesn't appear a way to obtain
> > > the cursor color that is in use when Vim is started.
> >
> > (I'm catching up on some old unread list e-mail.)
> >
> > According to the ctlseqs.ms document that came with xterm-165:
> >
> >
> >     Operating System Controls
> >     OSC Ps ; Pt ST
> >     OSC Ps ; Pt BEL
> >            Set Text Parameters.  For colors and font, if Pt is a
> >            "?", the control sequence elicits a response which con-
> >            sists of the control sequence which would set the
> >            corresponding value. ....
> >            ...
> >              Ps = 1 2  -> Change colors starting with text cursor to
> >            Pt
> >
> >
> > I executed this command at a Linux shell prompt,
> >
> >     echo -e '\033]12;?\007' ; read x
> >
> > and got the following response:
> >
> >     ^[]12;rgb:ffff/ffff/ffff^G
> >
> > After setting the cursor to red and repeating the command, I got
> > this:
> >
> >     ^[]12;rgb:ffff/0000/0000^G
> >
> > So, xterm does have the mechanism to obtain the cursor color when
> > vim is started.  I just don't know how to obtain the terminal's
> > response from within vim and don't have the time to experiment
> > further at the moment.  I did discover, though, that you can echo
> > back the color in that same format, so no parsing of the terminal
> > response would be necessary.
>
> Interesting, this must have been added recently.

This is from xterm-165, which appears to be from January 2002.

>
> I wonder what triggers the "rgb:" response.  It doesn't state somewhere
> why this happens instead of the "ordinary" color numbers.  Perhaps
> because xterm was compiled with more than 16 colors?

I don't know.  How do you tell how many colors the xterm binary
supports?  The xterm I built for SunOS behaves the same as the Linux
one I tested above and I didn't specify anything about colors when I
configured the build, so I think it has only 16 colors.

If I set "rgb:ff00/7f00/fe00", then query the value, the response is
"rgb:ffff/7f7f/fefe".  So something limits the resolution to 256
values per color component.  Some limited experimentation shows that
the actual resolution is enough to get more than 16 cursor colors.
So maybe a default build of xterm does support 256 colors.

Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Loading...