Skip to content
Browse files
patch 8.0.1685: can't set ANSI colors of a terminal window
Problem:    Can't set ANSI colors of a terminal window.
Solution:   Add term_setansicolors(), term_getansicolors() and
            g:term_ansi_colors. (Andy Massimino, closes #2747)
  • Loading branch information
brammool committed Apr 10, 2018
1 parent 07b46af commit f59c6e8cee092433d325ba21a107654a8d84f776
Showing with 321 additions and 6 deletions.
  1. +31 −0 runtime/doc/eval.txt
  2. +10 −0 runtime/doc/terminal.txt
  3. +44 −0 src/channel.c
  4. +6 −0 src/evalfunc.c
  5. +2 −0 src/proto/
  6. +4 −1 src/structs.h
  7. +152 −5 src/terminal.c
  8. +70 −0 src/testdir/test_terminal.vim
  9. +2 −0 src/version.c
@@ -2423,6 +2423,7 @@ term_dumpload({filename} [, {options}])
term_dumpwrite({buf}, {filename} [, {options}])
none dump terminal window contents
term_getaltscreen({buf}) Number get the alternate screen flag
term_getansicolors({buf}) List get ANSI palette in GUI color mode
term_getattr({attr}, {what}) Number get the value of attribute {what}
term_getcursor({buf}) List get the cursor position of a terminal
term_getjob({buf}) Job get the job associated with a terminal
@@ -2435,6 +2436,8 @@ term_gettty({buf}, [{input}]) String get the tty name of a terminal
term_list() List get the list of terminal buffers
term_scrape({buf}, {row}) List get row of a terminal screen
term_sendkeys({buf}, {keys}) none send keystrokes to a terminal
term_setansicolors({buf}, {colors})
none set ANSI palette in GUI color mode
term_setkill({buf}, {how}) none set signal to stop job in terminal
term_setrestore({buf}, {command}) none set command to restore terminal
term_start({cmd}, {options}) Job open a terminal window and run a job
@@ -8248,6 +8251,18 @@ term_getaltscreen({buf}) *term_getaltscreen()*
{buf} is used as with |term_getsize()|.
{only available when compiled with the |+terminal| feature}

term_getansicolors({buf}) *term_getansicolors()*
Get the ANSI color palette in use by terminal {buf}.
Returns a List of length 16 where each element is a String
representing a color in hexadecimal "#rrggbb" format.
Also see |term_setansicolors()| and |g:terminal_ansi_colors|.
If neither was used returns the default colors.

{buf} is used as with |term_getsize()|. If the buffer does not
exist or is not a terminal window, an empty list is returned.
{only available when compiled with the |+terminal| feature and
with GUI enabled and/or the |+termguicolors| feature}

term_getattr({attr}, {what}) *term_getattr()*
Given {attr}, a value returned by term_scrape() in the "attr"
item, return whether {what} is on. {what} can be one of:
@@ -8379,6 +8394,19 @@ term_sendkeys({buf}, {keys}) *term_sendkeys()*
means the character CTRL-X.
{only available when compiled with the |+terminal| feature}

term_setansicolors({buf}, {colors}) *term_setansicolors()*
Set the ANSI color palette used by terminal {buf}.
{colors} must be a List of 16 valid color names or hexadecimal
color codes, like those accepted by |highlight-guifg|.
Also see |term_getansicolors()| and |g:terminal_ansi_colors|.

These colors are used in the GUI and in the terminal when
'termguicolors' is set. When not using GUI colors (GUI mode
or |termguicolors|), the terminal window always uses the 16
ANSI colors of the underlying terminal.
{only available when compiled with the |+terminal| feature and
with GUI enabled and/or the |+termguicolors| feature}

This comment has been minimized.

Copy link

chdiza Apr 11, 2018

It would be nice if somewhere it's documented which positions in the list correspond to which colors. E.g., if I want to change the blue, I have no way of knowing which of the 16 I should change.

Also, the two new functions need to appear under :h function-list; currently they don't.

This comment has been minimized.

Copy link

brammool via email Apr 11, 2018

Author Member
term_setkill({buf}, {how}) *term_setkill()*
When exiting Vim or trying to close the terminal window in
another way, {how} defines whether the job in the terminal can
@@ -8463,6 +8491,9 @@ term_start({cmd}, {options}) *term_start()*
CTRL-D is used on MS-Windows. For Python
use CTRL-Z or "exit()". For a shell use
"exit". A CR is always added.
"ansi_colors" A list of 16 color names or hex codes
defining the ANSI palette used in GUI
color modes. See |g:terminal_ansi_colors|.

{only available when compiled with the |+terminal| feature}

@@ -136,6 +136,16 @@ terminal window will start with a white or black background.
To use a different color the Terminal highlight group can be used, for
example: >
hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
In GUI mode or with |termguicolors|, the 16 ANSI colors used by default in new
terminal windows may be configured using the variable
`g:terminal_ansi_colors`, which should be a list of 16 color names or
hexadecimal color codes, similar to those accepted by |highlight-guifg|. When
not using GUI colors, the terminal window always uses the 16 ANSI colors of
the underlying terminal.
The |term_setansicolors()| function can be used to change the colors, and
|term_getansicolors()| to get the currently used colors.

Syntax ~
@@ -4802,6 +4802,50 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
opt->jo_set2 |= JO2_TERM_KILL;
opt->jo_term_kill = get_tv_string_chk(item);
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
int n = 0;
listitem_T *li;
long_u rgb[16];

if (!(supported2 & JO2_ANSI_COLORS))

if (item == NULL || item->v_type != VAR_LIST
|| item->vval.v_list == NULL)
EMSG2(_(e_invargval), "ansi_colors");
return FAIL;

li = item->vval.v_list->lv_first;
for (; li != NULL && n < 16; li = li->li_next, n++)
char_u *color_name;
guicolor_T guicolor;

color_name = get_tv_string_chk(&li->li_tv);
if (color_name == NULL)
return FAIL;

guicolor = GUI_GET_COLOR(color_name);
if (guicolor == INVALCOLOR)
return FAIL;

rgb[n] = GUI_MCH_GET_RGB(guicolor);

if (n != 16 || li != NULL)
EMSG2(_(e_invargval), "ansi_colors");
return FAIL;

opt->jo_set2 |= JO2_ANSI_COLORS;
memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
# endif
else if (STRCMP(hi->hi_key, "env") == 0)
@@ -856,6 +856,9 @@ static struct fst
{"term_dumpload", 1, 2, f_term_dumpload},
{"term_dumpwrite", 2, 3, f_term_dumpwrite},
{"term_getaltscreen", 1, 1, f_term_getaltscreen},
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
{"term_getansicolors", 1, 1, f_term_getansicolors},
# endif
{"term_getattr", 2, 2, f_term_getattr},
{"term_getcursor", 1, 1, f_term_getcursor},
{"term_getjob", 1, 1, f_term_getjob},
@@ -868,6 +871,9 @@ static struct fst
{"term_list", 0, 0, f_term_list},
{"term_scrape", 2, 2, f_term_scrape},
{"term_sendkeys", 2, 2, f_term_sendkeys},
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
{"term_setansicolors", 2, 2, f_term_setansicolors},
# endif
{"term_setkill", 2, 2, f_term_setkill},
{"term_setrestore", 2, 2, f_term_setrestore},
{"term_start", 1, 2, f_term_start},
@@ -32,6 +32,7 @@ int term_swap_diff(void);
void f_term_dumpdiff(typval_T *argvars, typval_T *rettv);
void f_term_dumpload(typval_T *argvars, typval_T *rettv);
void f_term_getaltscreen(typval_T *argvars, typval_T *rettv);
void f_term_getansicolors(typval_T *argvars, typval_T *rettv);
void f_term_getattr(typval_T *argvars, typval_T *rettv);
void f_term_getcursor(typval_T *argvars, typval_T *rettv);
void f_term_getjob(typval_T *argvars, typval_T *rettv);
@@ -44,6 +45,7 @@ void f_term_gettty(typval_T *argvars, typval_T *rettv);
void f_term_list(typval_T *argvars, typval_T *rettv);
void f_term_scrape(typval_T *argvars, typval_T *rettv);
void f_term_sendkeys(typval_T *argvars, typval_T *rettv);
void f_term_setansicolors(typval_T *argvars, typval_T *rettv);
void f_term_setrestore(typval_T *argvars, typval_T *rettv);
void f_term_setkill(typval_T *argvars, typval_T *rettv);
void f_term_start(typval_T *argvars, typval_T *rettv);
@@ -1708,7 +1708,7 @@ struct channel_S {
#define JO2_EOF_CHARS 0x1000 /* "eof_chars" */
#define JO2_NORESTORE 0x2000 /* "norestore" */
#define JO2_TERM_KILL 0x4000 /* "term_kill" */
#define JO2_ALL 0x7FFF
#define JO2_ANSI_COLORS 0x8000 /* "ansi_colors" */

#define JO_CB_ALL \
@@ -1777,6 +1777,9 @@ typedef struct
int jo_term_finish;
char_u *jo_eof_chars;
char_u *jo_term_kill;
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
long_u jo_ansi_colors[16];
# endif
} jobopt_T;

0 comments on commit f59c6e8

Please sign in to comment.