Syntax highlighting in zsh using 256 colors? - colors

I'm using prezto with zsh, and my difficulty is with the syntax highlighting, of which a sample line is
'function' 'bg=green'
I could also have here, for example
'function' 'bg=blue,fg=yellow'
However, I want to take advantage of my 256 color terminal (konsole in my case), in which case I would have thought that something like
'function' 'bg=$FG[021],fg=$FG[196]'
for example, would work, given that I have previously loaded the spectrum module.
But I can't get this to work. I've checked that tput colors returns 256, and that the command
echo "$FG[214]Hello, World"
gives me an orange output.
Anyway, whether I use spectrum or not, I'd like more control over my syntax highlighting, and to be able to use 256 colors. Any advice would be very welcome!

You could specify the color index here like below:
'function' 'bg=21,fg=196'
zsh-syntax-highlighting gets the ability to specify colors from the zsh's Character Highlighting rather than ANSI escape sequences directly. This zsh manual describes a 'fg=colour' as below:
fg=colour
 The foreground colour should be set to colour, a decimal integer or the name of one of the eight most widely-supported colours.
We can use the color index rather than ANSI escpape sequences which spectrum module stores on $FG[...].

You can try:
export TERM='xterm-256color'

Related

Setting a sign to highlight only text instead of whole line in Vim

When defining a :sign you can use the linehl argument to assign a highlight group for the whole line the sign is placed in.
This highlights the whole line until the end, but how I do to highlight only the text of that line?
Edit: The idea is to use Syntastic to show the errors and warnings, but I can't redefine the SyntasticStyleError sign to do what I want. It highlights all the line instead of only the text of that line.
It is not possible as far as I know. Look at the :h sign-define:
:sign define {name} {argument}...
Define a new sign or set attributes for an existing sign.
The {name} can either be a number (all digits) or a name
starting with a non-digit. Leading digits are ignored, thus
"0012", "012" and "12" are considered the same name.
About 120 different signs can be defined.
Accepted arguments:
icon={bitmap}
Define the file name where the bitmap can be found. Should be
a full path. The bitmap should fit in the place of two
characters. This is not checked. If the bitmap is too big it
will cause redraw problems. Only GTK 2 can scale the bitmap
to fit the space available.
toolkit supports ~
GTK 1 pixmap (.xpm)
GTK 2 many
Motif pixmap (.xpm)
Win32 .bmp, .ico, .cur
pixmap (.xpm) |+xpm_w32|
linehl={group}
Highlighting group used for the whole line the sign is placed
in. Most useful is defining a background color.
text={text} *E239*
Define the text that is displayed when there is no icon or the
GUI is not being used. Only printable characters are allowed
and they must occupy one or two display cells.
texthl={group}
Highlighting group used for the text item.
It does not offer you a straight-forward way to distinguish between the text background colour and the background colour of the line - in fact, you can only set the linehl parameter.
Maybe there is a hacky way to do what you want. I've stumbled upon this link you might find useful: https://sunaku.github.io/vim-256color-bce.html
Another interesting idea is explained on vim.wikia.com (link) in the Highlighting that stays after cursor moves section. It suggests using the following command:
:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR>
This way you might mix it with the information you get from :sign place and replace signs with your custom highlighting method I posted above. It requires some scripting though.

Syntax Highlighting with Fine Granularity

Is there a way to assign an individual character, as identified by a height and depth index, to a highlight group? Every match feature I have come across uses a regex pattern as input.
The reason I ask is because I am making a syntax coloring plugin that will make text an increasingly lighter shade of gray with increasing parenthesis depth. If vim has no such feature and another algorithm makes character-by-character highlighting unnecessary, please point me to it!
Vim has a whole set of special regular expression atoms that can specify buffer positions.
For lines, \%23l matches only in line 23. You can also use \%>23l for all lines starting from 23, and concatenate two of those with < and > to specify ranges.
For columns, there are the corresponding \%23c and \%23v. The former uses byte indices (what Vim somewhat confusingly calls "columns"), as returned by functions like col() and getpos(), the latter screen widths (from virtcol()).
By combining those atoms, you can select arbitrary blocks of text, and highlight them, e.g. with :call matchadd(...). See :help /\%l for details on the atoms.
For your plugin implementation, you may be able to get some ideas from the vim js context coloring plugin, which highlights JavaScript code according to its scope.

Strange escape sequence sent by vim to terminal

vim appears to send several strange (i.e. undocumented) escape sequences to the underlying terminal when using the inkpot scheme. I have been unable to match them to any VT-100 or ANSI escape codes; does anyone know what these sequences are?
^[[3231m
^[[4232m
^[[3130m
These sequences show up as invalid tokens in a terminal emulator I am responsible for, and I would like to add support for these sequences if I can find proper documentation for them.
The following terminal-related environment variables are set on the affected systems:
TERM=xterm
COLORTERM=
I have not tried any other settings for those two variables.
vim is version 7.2, but I have tried newer versions with same effect.
Thanks!
Does your terminal support 88- or 256-color mode? If so, you should probably use a TERM value like xterm-88color or xterm-256color instead of plain xterm.
inkpot specifically disclaims support for terminals that only support 8/16 colors. From the top of the inkpot source:
" This should work in the GUI, rxvt-unicode (88 colour mode) and xterm (256
" colour mode). It won't work in 8/16 colour terminals.
The code that sets up the syntax coloring assumes that if the terminal does not support 88 colors, then it must support 256 colors. It does not try to provide fallback values for the case of 8 colors (e.g. when TERM=xterm). Maybe it would be nice if inkpot issued an error message (and did not modify the color settings) if the terminal did not appear to have appropriate color support.
The control sequences you are seeing result from trying to generate an 8-color terminal color control sequence for a color number that is outside the 0–7 range of acceptable inputs for that type of terminal. So, these sequences are probably not valid, but they are not entirely intentional either (arising because inkpot assumes 256-color support, but the xterm terminfo entry only knows how to handle basic 8-color support).
For example, inkpot sets the Normal syntax foreground color to 79 on 88-color terminals; this is translated to 231 for non-88-color terminals (i.e. for 256-color terminals, but also for your 8-color xterm).
When you try to format this out-of-range color number with TERM=xterm, you get the result ^[[3231m that you found:
% tput -T xterm setaf 231 | od -a
0000000 esc [ 3 2 3 1 m
0000007
(i.e. inserting 231 between ^[[3 and m)
If you use xterm-256color, you get a more normal looking result:
% tput -T xterm-256color setaf 231 | od -a
0000000 esc [ 3 8 ; 5 ; 2 3 1 m
0000013
(i.e. inserting 231 between ^[[38;5; and m)
Similarly, the Normal syntax 88-color background color of 80 is translated to a 256-color value of 232 and produces the errant ^[[4232m sequence for xterm (but would produce the more reasonable ^[[48;5;232m under xterm-256color).
^[[3231m
looks a lot like a color definition done/gone wrong:
^[[32;31m
which wouldn't make much sense anyway.
Does it happen with a specific value for $TERM or for any value? What Vim version? What terminal emulator are we talking about?

terminal escape sequences for fonts

What I want to develop:
Terminal which can use at least 2 fonts in the same time. One font I will use for shell input lines, another font for command output. For example:
user#host$ ls /home
user user1 user2 user3
Why:
More readable terminal/shell
How: Here I have problem. Probably shell needs to generate some new escape sequences. And terminal need to load different fonts and handle those sequences. Where to start? How to define new escaping sequence, where are standards?
Future: Maybe somebody want to join me in this project?
The standard for control sequences is pretty much the Xterm Control Sequences document ctlseqs.ms in the XTerm source code. (You can turn it into a PDF with the command groff -ms -Tps ctlseqs.ms | ps2pdf - ctlseqs.pdf, though the -ms option seems to be broken on Ubuntu 12.04).
XTerm already supports control sequences to change the font, but for the entire terminal at once. Open xterm and type into your shell—
echo -e "\033[?35h\033]50;#+1^G\033\\" # aka CSI ? 35 h OSC 50 ; #+1 BEL ST
the font for the entire terminal should change. This control sequence actually supports the names of True-Type fonts as well; see page 21.
If you'd like to change an existing terminal to support changing the font inline, you're
welcome to choose pretty much any control sequences not already allocated in ctrlseqs.ms and use them. However, it's a good idea to choose new control sequences similar to the control sequences for functionality that already exists.
Your next step is to get the source code for an existing terminal and start digging. What terminal do you use right now? The source code for Konsole or gnome-terminal is probably going to be easier to work with than that for XTerm.
There is a standard sequence for swapping fonts.
SGR 11
Also known as
CSI 11m
ESC [ 11m
Similarly
SGR 10
will switch back to the default font.
However, as has been commented, almost no terminal actually supports this. You'd likely be better off using some other rendering attribute, such as bold/underline/italics. Though note also not many terminals support italics.
For reference
SGR 1 = bold
SGR 4 = underline
SGR 3 = italics
If you are happy with just different font attributes (and not different fonts) you can even implement something similar without writing your own terminal emulator if you use zsh. You can just set up your shell to emit the right escape sequences to set the correct terminal attribute (italics, bold, color, ...) before and after the prompt and before command execution.
Let's assume you use Xterm and want your prompt to be bold, the typed command line to be italics and the command output to be normal.
Then the setup looks like this:
# bold is \e[1m and italics is \e[3m , \e[0m resets the attributes
PS1=$'\e[1m'$PS1$'\e[0;3m' # I assume you have set PS1 already
function reset-terminal-attributes { printf '\e[0m'; }
autoload add-zsh-hook
add-zsh-hook preexec reset-terminal-attributes

How to turn OFF (or at least override) syntax highlighting in nano via ~/.nanorc?

I am struggling finding a clear answer on disabling or overriding the color settings for the nano editor.
By default color syntax highlighting is enabled on my system. Clicking ALT+Y disables this, which is exactly what I want my default to be.
Any ideas?
To disable syntax highlighting write following lines into ~/.nanorc:
set quiet
syntax "disabled" "."
The first line prevent error reporting. The second line defines a new color syntax.
You can also define a single syntax containing your favorite color in your ~/.nanorc:
syntax "disabled" "."
color brightgreen,black "."
I hope this helps.
For future references, I would add that you can run nano without colors using the command line parameter -Y
nano -Ynone myfile.txt
The "none" syntax is reserved; specifying it on the command line is the same as not having a syntax at all.
You can set an alias in your .bash_profile file:
alias nano='nano -Ynone'
This worked for me better than above since I run a white background terminal. It just makes all the text black again.
set quiet
syntax "disabled" "."
color black "."
Add the following to your ~/.nanorc file to disable syntax highlighting for all file types.
syntax "" ""
color white ""
There's a limitation in nano that every syntax requires at least one color rule. And, on nano 4.0 at least, the color rule's regex can't be empty. But you can make a rule that just targets whitespace, or a rule that just targets an empty line.
I'd recommend defining an extremely minimal color scheme first that applies colors in a way that you can tolerate. For example, this rule sets the background to green in places where you have trailing whitespace.
syntax "nothing" "."
color ,green "[[:space:]]+$"
You can also create a rule that targets an empty line. This rule will have no visible effect, but the right hand side is technically not empty so nano will accept it.
syntax "nothing" "."
color green "^$"
Instead of using syntax "disabled" "." and forcibly keeping all hghlighting off, add this to bottom of your ~/.nanorc and use an alias when you want no highlighting:
## Syntax - Black and White only (for override)
syntax "blackandwhite" "."
color white,black "."
then:
nano --syntax=blackandwhite myfile-nohighlighting.php
(Too much to type? Then use an alias in your .bashrc/shellrc):
alias bw='nano --syntax=blackandwhite'
or you could simply (See #Adam answer):
alias bw='nano -Ynone'
And avoid creating a highlight profile.
then you can open using the alias and have no highlighting:
bw myfile.php
Using it this way, you also leave highlighting available in the .rc for when you may need it..
This answer is for nano version 5.9 on Termux version 0.117. Disclaimer: I don't understand exactly how .nanorc code works here (so, it possibly should be a bit different, but it seems to work).
To turn syntax highlighting off for all file types, do this in ~/.nanorc:
syntax "all" "\.*$"
color white,black "^.*$"
In the latter set of double quotes for each line, those are regular expressions. On the first line, the regex matches any file extension (I don't think "all" actually does anything; I just had to type a word in there; it doesn't work if it's blank or has multiple words). On the second line, the regex matches the entire text of the document, and sets the foreground (the font color) to white, and the highlight background to black.
I guess this does the same thing in a simpler way (I suppose it evaluates each character at a time):
syntax "all" "."
color white,black "."
If you just want to do the same thing for one kind of file, here is an example for .txt files:
syntax "txt" "\.txt$"
color white,black "^.*$"
Or
syntax "txt" "\.txt$"
color white,black "."
If you don't want to specify the background highlight color (which is not the entire background color of nano, btw), too, then instead of doing color white,black . . . do color white . . ..
Note: My personal reason for wanting to disable syntax highlighting was because comments beginning with # were a different color in plain text files and similar (and I didn't want that). These solutions make the comments white.

Resources