7 ms·
ANSI Escape Codes
- blueflow 5y agoTake this gist with a grain of salt, many of the things listed there are NOT standardized by ansi, and some are specific to 16-bit real mode terminal emulators on the IBM PC or compatibles. I suspect this gist actually refers to the ANSI.SYS terminal implementation of MS-DOS. If you want to have reliable and accurate information on how to control your terminal, consult the documentation on your terminal emulator or the ncurses database. The ncurses database is vast but does not include most of the exotic features of specific terminals. [tl;dr] dont rely on secondary sources like this, please consult primary documentation instead
- timw4mail 5y agoI'm pretty sure these lean towards the VT series of DEC terminals when not standard, not the DOS terminal.
- blueflow 5y agoSee Section "Set Mode", these are the real mode BIOS mode sets for interrupt 10h, subfunction AH=0[1]. The keyboard remapping function is also specific to ANSI.SYS[2]. [1] http://www.ctyme.com/intr/rb-0069.htm http://www.ctyme.com/intr/rb-0069.htm [2] https://en.wikipedia.org/wiki/ANSI.SYS#Keyboard_remapping https://en.wikipedia.org/wiki/ANSI.SYS#Keyboard_remapping
- deleted 5y ago[deleted]
- mananaysiempre 5y agoSpecifically, relevant primary and reputable sources might include the ECMA-48 standard[1] (supposedly identical to the withdrawn ANSI one), the manuals for the various DEC VT-series terminals[2], the XTerm documentation already mentioned here[3], the detailed description of the VT100 state machine including how it fills the holes in the ECMA/ANSI spec[4], and maybe some compatibility tables[5]. There’s also a lot of folk wisdom beyond that, apparently, but nobody seems to have really gathered it in a usable form. There is a Terminals WG over at the XDG[6], but I don’t know if they have produced anything useful yet. [1]: https://www.ecma-international.org/publications-and-standards/standards/ecma-48/ https://www.ecma-international.org/publications-and-standard... [2]: https://vt100.net/ https://vt100.net/ [3]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html https://invisible-island.net/xterm/ctlseqs/ctlseqs.html [4]: https://vt100.net/emu/dec_ansi_parser https://vt100.net/emu/dec_ansi_parser [5]: https://terminalguide.namepad.de/ https://terminalguide.namepad.de/ [6]: https://gitlab.freedesktop.org/terminal-wg https://gitlab.freedesktop.org/terminal-wg (ETA: another commenter also recommends https://bjh21.me.uk/all-escapes/all-escapes.xhtml https://bjh21.me.uk/all-escapes/all-escapes.xhtml)
- neilv 5y agoExactly, and those links you gave are invaluable. I have an overlapping list at: https://www.neilvandyke.org/racket/charterm/#%28part._.References%29 https://www.neilvandyke.org/racket/charterm/#%28part._.Refer... FWIW, in a low-level cross-terminal API, I made up some concepts that seemed helpful (and there are also a few comments scattered in there, such as hints about why key mapping is less consistent than one would've guessed): https://www.neilvandyke.org/racket/charterm/#%28part._.Terminal_.Diversity%29 https://www.neilvandyke.org/racket/charterm/#%28part._.Termi...
- rbanffy 5y agoYou can also emulate a VT terminal using MAME. I’d say that’s the next best thing if you don’t have functioning metal.
- neilv 5y agoExcellent idea. I didn't think of that when I was working on that library.
- rbanffy 5y agoWhen discussing the possibility of implementing double width or height on VTE, someone did exactly that to finally settle how things behave. IIRC the same approach was used to find out what’s the “correct” way to do sixel graphics.
- anyfoo 5y agoMS-DOSisms like the BIOS int 10h mode switch you mentioned in another comment are included, but so are things like (xterm) 256 color codes, TrueColor, and alternate screen switching, which IIRC were not a thing in ANSI.SYS. (The color thing I'm very sure because VGA text modes only supported 16 colors, the alternate screen mode is only a guess, it's very popular in unixy terminal emulators, but seems mostly useless for DOS.)
- mark-r 5y agoThese escape sequences were known as ANSI long before the invention of ANSI.SYS. I suspect the standardization may be for the syntax and not the specific commands.
- MisterKeefe 5y agoI've always liked these. It's fun to use them directly to jazz up quick CLI scripts without bothering with dependencies etc. A few months ago I put together a kind of interactive cheatsheet for working with them quickly: http://ansi-escape-artist.surge.sh/ http://ansi-escape-artist.surge.sh/
- omoikane 5y agoThe 256 color palette was the most useful bit for me: not every terminal supports true color, and many terminals allow users to select color themes for the first 16 colors (e.g. "xterm -reverse"). So the upper 240 of the 256 color palette were the most reliable colors.
- chasil 5y agoHere is a shell script to set and use colors. I added the truecolor functions just now. #!/bin/dash fg () ( printf '\033[38;2;%d;%d;%dm' "${1:-0}" "${2:-0}" "${3:-0}"; ) bg () ( printf '\033[48;2;%d;%d;%dm' "${1:-0}" "${2:-0}" "${3:-0}"; ) fg256 () ( printf '\033[38;5;%dm' "${1:-0}"; ) bg256 () ( printf '\033[48;5;%dm' "${1:-0}"; ) N='\033[' x=30 for a in Bl R G Y B M C W # Black Red Green Yellow Blue Magenta Cyan White do eval $a='$N'"'"$(( x))"m'" \ b$a='$N'"'"$((60+x))"m'" \ ${a}bg='$N'"'"$((10+x))"m'" \ b${a}bg='$N'"'"$((70+x))"m'" # bX=bright Xbg=background bXbg=brgt bgnd x=$((1+x)) done N=$N'0m' printf "${B}${Gbg}This is blue on a green background.$N\n" fg 255 bg 255 255 255 printf "This is red on a white background.$N\n" x=0 while [ $x -lt 256 ] do y=$((255-x)) fg256 $x bg256 $y printf "%.3d $N" $x [ $(((1+x)%16)) -eq 0 ] && echo '' x=$((1+x)) done echo ''
- fanf2 5y agoI generally use xterm's ctlseqs.ms as a reference - https://www.invisible-island.net/xterm/ctlseqs/ctlseqs.html https://www.invisible-island.net/xterm/ctlseqs/ctlseqs.html There's also Ben Harris's concordance of escape sequences https://bjh21.me.uk/all-escapes/all-escapes.xhtml https://bjh21.me.uk/all-escapes/all-escapes.xhtml (Ben is a member of the PuTTY team)
- lsllc 5y agoRelated: See also this page documenting escape sequences supported by xterm: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html https://invisible-island.net/xterm/ctlseqs/ctlseqs.html EDIT: This is actually linked at the bottom of the article.
- allenu 5y agoPlaying around with BBSes as a teen, I'm super familiar with ANSI codes. I remember being fascinated by how they worked after opening up a .ANS file to see their contents. One of my earliest programming projects was creating my own diary app which used ANSI codes to let you format the text. It also kept track of your cursor movements and encoded them so that you could do neat things like write text anywhere on the screen or overwrite existing text.
- teknopaul 5y agoI has a load of fun when I discovered this too, but eventually realised that a good cli app has focus, sensible use of opts, a succinct -h, reliable exit codes and above all easily parsed output on stdout.
- eropple 5y agoI am all about sensible options, reliable exit codes, and easily parsed output on stdout--but easily parsed output on stdout when it isn't a tty is different from what you show a person when it is a tty.
- nitrogen 5y agoIt's also fairly straightforward to strip escape codes if you need to using sed or similar[0], and there probably already exists a tool that will do just that specifically and more robustly than my example. [0] Here's a basic example I wrote in Ruby: https://github.com/mike-bourgeous/mb-util/blob/5616cc4ce6218b5c541242ac47b0c99a30952a6c/lib/mb/util/text_methods.rb#L21-L24 https://github.com/mike-bourgeous/mb-util/blob/5616cc4ce6218... str.gsub(/\e\[[^A-Za-z~]*[A-Za-z~]/, '')
- rbanffy 5y agoI just select the styling depending on what stdout/stderr are. If output is not going to a terminal, styling strings are empty.
- 5y ago
- cyberge99 5y agoIf you use ‘vi’, in insert mode, it’s CTRL-V then tap escape button. Followed by [31m or whatever code.
- fanf2 5y agoCTRL-V is a common "quote the next character" input command: it's specified by POSIX (https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/sh.html https://pubs.opengroup.org/onlinepubs/9699919799.2018edition... https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/vi.html https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...) and it works in bash (in vi mode and emacs mode) and zsh It also works with just the bare terminal line discipline in many flavours of unix: look at the 'lnext' control character in the output from 'stty' on Linux, macOS, or BSD (but not POSIX). (however the equivalent in emacs itself is ctrl-Q, probably for bad reasons)
- skissane 5y ago> (however the equivalent in emacs itself is ctrl-Q, probably for bad reasons) Good reasons: 1) Q is mnemonic for Quote. 2) emacs began life on non-Unix platforms (ITS, TOPS-20, Multics), and was only ported to Unix later. Ctrl-Q is inherited from the original ITS emacs.
- bruce343434 5y agoWhat did the vertical tab use to look like?
- tyingq 5y agoHere's a good example in this printer manual: https://mpi.com.es/wp-content/uploads/2015/06/USER-GUIDE-IGP-DASCOM-TALLY-T2265_T2280_EN.pdf https://mpi.com.es/wp-content/uploads/2015/06/USER-GUIDE-IGP... Search for both "vertical tab" and "VFU" (Vertical Format Unit). The "VFU" would accept commands to set the vertical tab stops to match whatever form you had. Say there was an address box that was 6 lines the top, a multi-line field 10 lines from the top, and an SSN field 15 lines from the top, and so on. You could program those tab stops and have a tractor feed of those forms loaded. Which would be easier programmatically than, for example, doing the math on how the length of text in some multi-line field in between those two would change the amount of line feeds you would need to send to land in the SSN field. So, something like: <vt> Enter the Address Stuff <vt> Enter some amount of multiline text without having to calculate how many lines it filled <vt> Now you're in the SSN field
- epc 5y agoI only recall seeing it used on line or dot matrix printers. You could scroll the paper down at the current cursor point (some how this was different from line feed, maybe some printers allowed the number of lines skipped to be configured).
- hnlmorg 5y agoLike how the horizontal tab is several horizontal cells of whitespace, the vertical tab is several vertical cells of whitespace.
- GartzenDeHaes 5y agoXTERM docs cover most common terminal codes, including DEC. https://www.xfree86.org/current/ctlseqs.html https://www.xfree86.org/current/ctlseqs.html
- rbanffy 5y agoAnd xterm even supports DECDWL, DECDHL, and DECSWL.
- michaelsbradley 5y agoFor those interested in learning more about terminal emulators and character graphics, checkout Nick Black's book Hacking the Planet (with Notcurses): A Guide to TUIs and Character Graphics[1]. While the overall focus of the book is on programming with Notcurses[2], the author shares a wealth of related info and history throughout its pages. [1] https://nick-black.com/htp-notcurses.pdf https://nick-black.com/htp-notcurses.pdf [2] https://github.com/dankamongmen/notcurses#readme https://github.com/dankamongmen/notcurses#readme
- wombatpm 5y agoAhh the joy of sending a file with a 10k ^G to the line printer. Used to annoy the poor slobs who had the crapy terminals
- ChrisArchitect 5y agothis was posted why? considering comments here saying it's not reliable information and the number of comments on the gist itself from when it was initially posted months ago saying it needed forks to fix problems and clarify......
- deleted 5y ago[deleted]
- rascul 5y agoLinux console_codes man page, section 4 https://man7.org/linux/man-pages/man4/console_codes.4.html https://man7.org/linux/man-pages/man4/console_codes.4.html
- rbanffy 5y agoWould be cool if the console did bold, italics, underline, and, my favourite, overline.
- woodruffw 5y agoBold, italics, and underline should all work in most terminal emulators! I'm not sure about overline, but strikethrough should also work.
- derefr 5y agoI think the parent poster's point was that the Linux console framebuffer renderer in particular, is not a particularly full-featured terminal in terms of the SGR sequences it supports. Which can be slightly unpleasant when you're stuck working in "text" (really, console framebuffer) mode in Linux for whatever reason. (Which makes me wonder: is there a way to tell Linux to not use its own console framebuffer driver for displaying TTYs, but rather to instead spawn some userland libsdl binary and connect it to the raw framebuffer? If so, we could presumably just drop in a fully-featured terminal emulator to act as the Linux console.)
- nitrogen 5y agoI seem to recall there being an fbdev-based terminal emulator that does just take over a VT, maybe it was this one: https://www.systutorials.com/docs/linux/man/1-fbterm/ https://www.systutorials.com/docs/linux/man/1-fbterm/ but searches show a couple of possibilities. I'm not sure how it works with systemd, but back in init land you could just replace the getty lines in /etc/inittab with whatever you wanted to use on that VT, or replace the default login command. What you wonder should be possible. Source: Quite a long time ago I believed that the Linux framebuffer was the future of development, and spent a lot of time making animated bootup sequences, custom login screens, writing libraries (in collaboration with others) to make development easier without having to go into a "framework" like DirectFB, and using elinks (or was it links2?) for browsing and fbi for image and PDF viewing.
- sillysaurusx 5y agoIf someone wants to do this in Python, I made a lib for it: pip3 install ansi-escapes It’s a port of sindresorhus’s ansi-escapes library. I find it way more convenient than looking up codes. https://github.com/shawwn/ansi-escapes-python https://github.com/shawwn/ansi-escapes-python It has some basic iTerm2 support too, so you can print out annotations in the console. Think of it like sticky notes. See also ansi-styles, the lib that Chalk is based on. (Colors! You like colors? Are you a curmudgeony salt like me that refuses to use Rich for some reason? Use that.) As with ansi-escapes, you can just pip install ansi-styles. https://github.com/shawwn/ansi-styles-python https://github.com/shawwn/ansi-styles-python
- dfee 5y agoAside: it’s so nice seeing a pyproject.toml these days. It looks like we may be able to forego setup.cfg soon too. https://stackoverflow.com/a/64151860 https://stackoverflow.com/a/64151860
- teddyh 5y agoOr you can use curses (or ncurses, or any other terminal-independent library) like an adult, and not create yet another “works best in iTerm” application. We’ve been down this road before, and we settled on curses and similar libraries to be terminal-independent.
- booleanbetrayal 5y agoBack in the day, during the dorm wars, we would all have syslogd writing to terms to keep track on our nefarious neighbors in realtime. Echoing ANSI escape codes strategically was a good way to erase those tracks.
- oneweekwonder 5y agoSomething i do way to much is `watch telnet <static-ip> 22` because it will print a Terminal bell and then i know a embedded device with a short ssh/dhcp window is up. This is for dropbear ssh have not tested it on openssh, yet.
- jacquesm 5y agoDoes anybody remember ANSI art demos? Some of those were really amazing and ran for minutes.
- rbanffy 5y agoI cringe when they call it ASCII art…
- grilledcheez 5y agoMy first paid programming job was making a client for a remote monitoring solution for the water industry. I must have been 14 or 15. One of my father's acquaintances knew I had been messing around with programming, and he needed a shiny Windows 95 application for his customers. It would dial the plant via modem, and I remember being given a list of "escape codes" that determined how the stream of bytes should be rendered on the screen. My program parsed (some of) these codes and displayed the text accordingly, I specifically remember implementing the "move cursor" and "reverse mode" codes... Not until years after did I realise I had basically built a shitty terminal emulator using Visual Basic text fields.
- ChrisArchitect 5y agoRelated from a few months back: ANSI Escape Artist – webapp for generating terminal escape codes https://news.ycombinator.com/item?id=26609452 https://news.ycombinator.com/item?id=26609452