Skip to content

Instantly share code, notes, and snippets.

@mispdev
Created February 12, 2025 02:17
Show Gist options
  • Save mispdev/525499b96351c73e7a501a21e54fbb27 to your computer and use it in GitHub Desktop.
Save mispdev/525499b96351c73e7a501a21e54fbb27 to your computer and use it in GitHub Desktop.
Showing images in sixel-compatible terminals with Midnight Commander (mc)
Showing images in sixel-compatible terminals with Midnight Commander (mc)
=========================================================================
Task
- Using Midnight Commander (mc), I want to show the selected image file currently selected in `mc` on the terminal by just pressing the enter key.
Prerequisite
- Using a terminal that is able to show images in sixel format (e.g. [Windows Terminal](https://github.com/microsoft/terminal) in version v1.22 or newer).
- Having [ImageMagick](https://imagemagick.org/) installed for resizing and convertion to sixel format.
What worked for me (Ubuntu 24.04 server, using "bash" as shell, access via SSH within Windows Terminal v1.22 from Windows machine):
- In Midnight Commander (mc) press F9 to access menu, then go to "Command", then select "Edit extension file".
- In the configuration file that is opened (usually `~/.config/mc/mc.ext.ini`), search for `[Include/image]` section.
- The default command for opening images there should be something like: `Open=/usr/lib/mc/ext.d/image.sh open ALL_FORMATS`
- Change this line to: `Open=echo "\\n"; convert %f -resize "$(expr 10 \\* $(tput cols))x$(expr 21 \\* $(tput lines))>" sixel:-; read wait_for_enter_key`
Line dissected:
- `echo "\\n"` switches to a new line on the terminal, making sure that the image starts at the left side of terminal.
- `convert %f -resize "$(expr 10 \\* $(tput cols))x$(expr 21 \\* $(tput lines))>" sixel:-`
- Uses `convert` command of ImageMagick to convert the image file provided by mc via `%f` to sixel format and dump it to the console. (Newer ImageMagick versions use `magick` command instead.)
- The parameter `-resize "$(expr 10 \\* $(tput cols))x$(expr 21 \\* $(tput lines))>"` does resizing, if the image should be too big for the rendering space on the terminal.
- `tput cols` gives the number of colums the terminal is currently showing.
- `tput lines` gives the number of lines/rows the terminal is currently showing.
- Here it is assumed that each column takes about 10 pixel of space to render, and each line 21 pixels to render (for my setup that is quite a good fit). In your case that might differ, depending on the font and font size your specific terminal is using on your device.
- The last `>` given in the parameter makes sure that images being smaller than the maximum calculated pixel size on terminal are _not_ resized, but any images that are larger will be rescaled to this maximum size, keeping aspect-ratio as-is.
- `read wait_for_enter_key` will store everything you type before pressing enter key to a dummy varibale called "wait_for_enter_key". We use it simply to wait for the user pressing the enter key, before switching back to Midnight Commander (mc), as otherwise mc would immediately switch back to its UI after rendering the image to the terminal. (Note: You can always switch back and forth to terminal from within mc using CRTL+o.)
Notes
- If having trouble, test `img2sixel <imagefile>` first, to see if rendering a picture as sixel on your terminal works at all. (Using Ubuntu install `libsixel-bin` via apt.)
- If this does not render the image to the terminal, then do not spend any time on trying to fix issues with Midnight Commander (mc) - first ensure that your terminal renders images in sixel format at all.
@aramirezreyes
Copy link

aramirezreyes commented May 9, 2025

Nice line. Thanks for sharing!
I found that piping some of it to less -r allows to not clutter the terminal with all the previously viewed image. Something like this with the modifications for size:
Open=img2sixel %f | less -r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment