When I open vim inside of a TMUX pane, The pane is filled with codes I don't recognise. If I just run vim, I get this:
^[[38;2;165;42;42m 1
^[[38;2;0;0;255m~
If I open a file with vim, I get something like this (top pane):
Pretty new to both vim and TMUX. How can I solve this?
It seems that Vim is sending control sequences to your terminal which the latter doesn't understand.
More specifically, the sequences you mentioned in the OP:
^[[38;2;165;42;42m
^[[38;2;0;0;255m
look like they're encoding foreground true colors for the text.
You can find their syntax here:
CSI Pm m
Where CSI stands for “Control Sequence Introducer” and is produced by the keys ESC [, and Pm stands for:
A multiple numeric parameter composed of any number of single numeric parameters, separated by ; character(s).
If you scroll down the page, you should find a description of a more detailed syntax:
CSI Pm m Character Attributes (SGR).
...
This variation on ISO-8613-6 is supported for compatibility with KDE konsole:
Pm = 3 8 ; 2 ; Pr; Pg; Pb
Set foreground color to the closest match in xterm's palette for
the given RGB Pr/Pg/Pb.
Pm = 4 8 ; 2 ; Pr; Pg; Pb
Set background color to the closest match in xterm's palette for
the given RGB Pr/Pg/Pb.*
Applied to your first sequence, you can break it down like so:
┌ CSI
│ ┌ Pm
├─┐├────────────┐
^[[38;2;165;42;42m
├─┘ ├┘ ├┘
│ │ └ Pb = amount of blue
│ └ Pg = amount of green
└ Pr = amount of red
If the terminal doesn't understand this sequence, I can see 3 explanations:
the terminal doesn't support true colors
tmux is not properly configured to support true colors
Vim is not properly configured to support true colors
To test whether 1. is the issue, you can write this bash function in your ~/.bashrc:
truecolor() {
local i r g b
for ((i = 0; i <= 79; i++)); do
b=$((i*255/79))
g=$((2*b))
r=$((255-b))
if [[ $g -gt 255 ]]; then
g=$((2*255 - g))
fi
printf -- '\e[48;2;%d;%d;%dm \e[0m' "$r" "$g" "$b"
done
printf -- '\n'
}
Then execute $ truecolor in your shell, outside tmux. If you get some kind of rainbow, your terminal supports true colors (at least partially).
If you see some cells which are not colored, and others randomly colored, your terminal doesn't support true colors.
Alternatively, you can try the sequences manually:
$ printf '\e[38;2;%d;%d;%dm this text should be colored \e[0m' 165 42 42
$ printf '\e[38;2;%d;%d;%dm this text should be colored \e[0m' 0 0 255
If $ truecolor doesn't produce a rainbow, or if the $ printf commands don't change the foreground color (not the background color) of the text, you'll have to either:
disable 'termguicolors' in your ~/.vimrc; i.e. remove set termguicolors (or make it execute set notermguicolors)
try and upgrade your terminal
find another terminal which supports true colors
To test whether 2. is the issue, inside tmux, you can execute this shell command:
$ tmux info | grep Tc
If the output contains [missing]:
203: Tc: [missing]
^^^^^^^^^
it means that tmux is not configured to support true colors.
In this case, you have to include something like this in ~/.tmux.conf:
set -as terminal-overrides ',*-256color:Tc'
││ ├────────────────┘ ├────────┘ ├┘
││ │ │ └ tell tmux that the terminal suppors true colors
││ │ └ configure the option only if `$TERM` ends with the string `-256color`
││ └ the option to configure is `terminal-overrides` (see `$ man tmux`)
│└ the next option is a server option
└ append the value to the tmux option instead of overwriting it
Then restart tmux, and execute $ tmux info | grep Tc. This time the output should contain true:
203: Tc: (flag) true
^^^^
If it doesn't, have a look at the output of $TERM outside tmux:
$ echo $TERM
The output should be matched by whatever pattern you write before :Tc.
In the previous example, I used the pattern *-256color which will match any terminal whose $TERM ends with the string -256color. If it doesn't match your $TERM, you can try another pattern, or simply write * to describe any type of terminal:
set -as terminal-overrides ',*:Tc'
To test whether 3. is the issue, you can write these commands in your ~/.vimrc:
set termguicolors
let &t_8f = "\<Esc>[38:2:%lu:%lu:%lum"
let &t_8b = "\<Esc>[48:2:%lu:%lu:%lum"
Or:
set termguicolors
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
The only difference between the 2 versions is the delimiter between the parameters of the sequences. A colon in the first version, a semicolon in the second one. See :h xterm-true-color for more info.
You can check the current values of these 3 options by executing successively:
:echo &tgc
:echo &t_8f
:echo &t_8b
They should output:
1
^[[38:2:%lu:%lu:%lum
^[[48:2:%lu:%lu:%lum
Or:
1
^[[38;2;%lu;%lu;%lum
^[[48;2;%lu;%lu;%lum
Related
I am using ANSI escape codes in my bash script to change the color of echo commands
#/bin/bash
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo -e "$text"
}
cecho b "This is blue"
cecho r "This is red"
The issue is i am using a Terminal color scheme and it causes the cecho command to display wrong colors , for eg. If i try to output cecho command for purple color , it gets displayed in yellow color. I think my internal Terminal color sceheme is conflicting with these ANSI color codes. Is it somehow possible to override any internal color schemes and force display the colors defined in bash script ? Wrong colors being displayed in echo might be due to other reasons as well , conflict with internal color scheme is just my guess .
In my color scheme,there were some variables pre defined for different colors.So while using ANSI color codes ,it was getting overrided by these gloablly defined colors. Removing all those internally defined colors has apparently fixed this issue
My problem is, that any script / command in /etc/init.d seems to be overridden by something else or disregarded.
So I guess that this must be something to be configured in the kernel.
For example using /etc/init.d/some_script (This content is about what I want):
# Column number to place the status message
RES_COL=60
# Command to move out to the configured column number
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
# Command to set the color to SUCCESS (Green)
SETCOLOR_SUCCESS="echo -en \\033[1;36m"
# Command to set the color to FAILED (Red)
SETCOLOR_FAILURE="echo -en \\033[1;31m"
# Command to set the color back to normal
SETCOLOR_NORMAL="echo -en \\033[0;39m"
# Function to print the SUCCESS status
echo_success() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_SUCCESS
echo -n $" OK "
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 0
}
# Function to print the FAILED status message
echo_failure() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_FAILURE
echo -n $"FAILED"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
I've been grepping around inside the kernel- and other sources and inside the system but found nothing about that.
/drivers/tty/vt/vt.c seems to be some global config for the VT. That's not what I need.
I would be happy to receive a few tips.
Greetings
Jens
This OS uses systemd.
I just started my old computer with Mandriva 2009 and coloring worked out of the box. There is a file 'etc/sysconfig/init' in which I can set the color but here on Magaia 7.1 there is no such file. I only have /etc/init.d/functions which says:
"This file contains functions to be used by most or all shell scripts in the /etc/init.d directory."
There is a block in it showing the color configuration:
# Read in our configuration
if [ -z "${BOOTUP:-}" ]; then
if [ -f /etc/sysconfig/init ]; then
. /etc/sysconfig/init
else
# verbose ->> very (very!) old bootup look (prior to RHL-6.0?)
# color ->> default bootup look
# other ->> default bootup look without ANSI colors or positioning
BOOTUP=color
# Column to start "[ OK ]" label in:
RES_COL=60
# terminal sequence to move to that column:
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
# Terminal sequence to set color to a 'success' (bright green):
SETCOLOR_SUCCESS="echo -en \\033[1;36m"
# Terminal sequence to set color to a 'failure' (bright red):
SETCOLOR_FAILURE="echo -en \\033[1;31m"
# Terminal sequence to set color to a 'warning' (bright yellow):
SETCOLOR_WARNING="echo -en \\033[1;33m"
# Terminal sequence to reset to the default color:
SETCOLOR_NORMAL="echo -en \\033[0;39m"
# Verbosity of logging:
LOGLEVEL=1
fi
if [ "$CONSOLETYPE" = "serial" ]; then
BOOTUP=serial
MOVE_TO_COL=
SETCOLOR_SUCCESS=
SETCOLOR_FAILURE=
SETCOLOR_WARNING=
SETCOLOR_NORMAL=
fi
But any changes do have no effect.
I tagged it linux-kernel because my guess was that this could be overriden by some kernel option or patch or config or someting like that.
Vim (v 8.0.1283) is drawing 8-bit color in iTerm2 (v 3.2.4) despite what appears to be correct configuration. $TERM is set correctly:
$ echo $TERM
xterm-256color
Here's the relevant section of my ~/.vimrc:
set background=dark
set termguicolors
colorscheme deep-space
The repo for this theme doesn't point to any further config for the vimrc. Even more confusing, the themes are pulled from rafi/awesome-vim-colorschemes with vim-plug and many of these theme work perfectly. The docs say the plugin requires true color (which I'm assuming is 256).
I've tried restarting and sourcing the vimrc, triple checked spelling and env variables, re-read documentation for the color scheme but can't seem to make heads or tails of it. Is there something obvious I'm missing here?
True color (24 bit) is not 256 color (8 bit). Copied from another of my answers:
When I had this problem, it was because my vim colorscheme was using truecolor (24 bit) and tmux only supports 8bit (256 colors).
Here are the methods to check color support:
First, make sure you have 256 color support in your default terminal and tmux with this python script:
#!/usr/bin/env python
# Copyright (C) 2006 by Johannes Zellner, <johannes#zellner.org>
# modified by mac#calmar.ws to fit my output needs
# modified by crncosta#carloscosta.org to fit my output needs
import sys
import os
def echo(msg):
os.system('echo -n "' + str(msg) + '"')
def out(n):
os.system("tput setab " + str(n) + "; echo -n " + ("\"% 4d\"" % n))
os.system("tput setab 0")
# normal colors 1 - 16
os.system("tput setaf 16")
for n in range(8):
out(n)
echo("\n")
for n in range(8, 16):
out(n)
echo("\n")
echo("\n")
y=16
while y < 231:
for z in range(0,6):
out(y)
y += 1
echo("\n")
echo("\n")
for n in range(232, 256):
out(n)
if n == 237 or n == 243 or n == 249:
echo("\n")
echo("\n")
os.system("tput setaf 7")
os.system("tput setab 0")
The expected output is to have each line be a different color, with a max of 1 white line. If there are more lines with white text on black background, you do not have 256 colors enabled.
Next, check that you have truecolor support in your terminal/tmux with this bash script:
#!/bin/bash
# Based on: https://gist.github.com/XVilka/8346728
awk -v term_cols="${width:-$(tput cols || echo 80)}" 'BEGIN{
s="/\\";
for (colnum = 0; colnum<term_cols; colnum++) {
r = 255-(colnum*255/term_cols);
g = (colnum*510/term_cols);
b = (colnum*255/term_cols);
if (g>255) g = 510-g;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "%s\033[0m", substr(s,colnum%2+1,1);
}
printf "\n";
}'
The expected output of this one looks like this:
The expected behavior is that tmux will support 256 color but not truecolor, and that your terminal will support both. If this is true, and your vim colorscheme still looks bad, it is very likely that you are using a truecolor colorscheme and tmux cannot support that. You can either switch to a 256 color version or just be sad about it. Sorry for the lack of good news.
i am a vim user and got used to the gf command, which opens the file under the cursor.
Now i wanted to ask, if there is something like that for tmux.
I can navigate through a tmux-pane and it happens often that there is a file-path under the cursor. Now i like to have the possibility to open that file under the cursor with vim.
A: in the current window
B: in another window which includes and opened vim
Maybe there is a possibility to run a sh-script in that navigation-mode when invoking a special key-combination? that would make it possible to write my own scripts like i got used to in vim with vimscript.
I am already using some vi-copy modes in mux .tmux.conf
# VIM
# ===================================================================
# Vimlike copy mode.
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Enable vi keys.
setw -g mode-keys vi
# https://coderwall.com/p/4b0d0a/how-to-copy-and-paste-with-tmux-on-ubuntu
bind -t vi-copy y copy-pipe "xclip -sel clip -i"
I've been searching for an answer for years and ended up making a tmux plugin: https://github.com/artemave/tmux_super_fingers. It still baffles me how such in integral part of terminal based workflow is not a solved problem (well, it is now).
To achieve what you want, you need to use the stdin in your command line (xargs can do that) and tell tmux, in a new-window, to open the data with the arguments from the copy buffer:
bind -t vi-copy y copy-pipe "xargs -I{} tmux new-window 'vim {}'"
This needs more tuning (getting the right session, the right command, use $EDITOR instead of vim etc.
It is quite dangerous: Think copying /foo/bar/my;rm -rf /.
Also, as-is, this will only work for paths relative to tmux' working directory.
There's a mod for tmux allowing to bind an action of any complexity in 'mode': http://ershov.github.io/tmux/
There's an example of how to mark the word under cursor using that patch:
proc is_word_char {c} {
print [scan $c %c]
return [expr {$c > " " && $c != "\x7f"}]
}
proc mark-current-word {} {
clear-selection
set l [copy-mode-screenline]
set x [copy-mode-get-cx]
if {![is_word_char [string range $l $x $x]]} return
incr x
while {[is_word_char [string range $l $x $x]]} {
cursor-right
incr x
}
incr x -2
begin-selection
while {[is_word_char [string range $l $x $x]]} {
cursor-left
if {$x < 1} return
incr x -1
}
}
# Open selection in a vim mini-window (no shell and files)
bind-key -t vi-copy y tcl {
split-window -c [f #{pane_current_path}] -l 5 "
echo -n [shell-quote [copy-mode-selection]] | vim -R -"
}
Hence, to open the current file in vim:
mark-current-word
split-window -c [f #{pane_current_path}] -l 5 "vim -R [shell-quote [copy-mode-selection]]"
So i got it running with the following binding:
bind -t vi-copy y copy-pipe "xargs -I{} tmux send-keys -t 1 ';edit {}' Enter && tmux select-pane -t 1"
notes
i changed vim command : to ;
i have a open vim in pane 1
I have a command-line program that outputs some text in different colors depending on the status.
The text doesnt change on multiple calls, but its color changes.
Eg:The program outputs the text "S14789" on the console.
The color of this text will be in red if the status is fail, and green if pass
I need to write a script to do some thing else depending on the color (status). Any suggestions?
As the scripts are giving one value at one time you can modify the script to change the exit values to 0 if it is pass and 1 if it is pass.
#!/bin/bash
echo "Print in green"
exit 0
$ ./script1.sh
Print in green
$ echo $?
0
The same way you can use exit 1 which return a value 1 to ? variable and you can use it to determine whether the script pass or fail.
So, here is what I did to perform tasks depending on color output:
/usr/bin/smoke is my program which outputs text in a particular color. Search for the color escape sequence and perform tasks (Thanks to #Jidder for suggesting cat -v).
#!/bin/bash
COUNT=`/usr/bin/smoke | cat -v | fgrep "^[[32" | wc -l`
if [ $COUNT -gt 0 ]
then
#Color found, do something
else
#Color not found, do something else
fi
Note: 32 is for green. For other colors refer the list of escape sequences: http://wiki.bash-hackers.org/scripting/terminalcodes#general_useful_ascii_codes