Termux Extra Keys Boosted

Foreword

In case you don’t know yet, I finally decided to separate en and CJK posts publicly. Although a language flag for internal usage exists from Day One of this revived blog, I never took the time to implement it. As always, it’s also possible to receive en only posts via RSS simply by appending an index.xml suffix to the URL. Taxonomies still have all posts though.

If you have no idea what Termux is, then the post is not for you (for the time being). You’d better download it from either F-Droid or GitHub (make sure NOT download Google Play one as that’s terribly obsolete due to SAF enforcement), and try it first.

Background

Last month I was forced to move out for a few days due to family stuff. As the rpi4 uses microHDMI and my DIY portable monitor uses miniHDMI (when I discovered that I was like 🙄), and I forgot to bring the HDMI adapter, I had no way but to use Termux as the primary programming device before the package was arrived and delivered to the No Man’s Land I was at…

Since I prefer cli tools like newsboat, buku, w3m, vim et cetera and develop VNDB-Calendar and CnGalCalendar in Python at the time, most things are fine. It’s just a bit annoying that the external USB keyboard cannot type many characters or input certain key combinations, otherwise Termux virtual keyboard would just randomly panic for fuck’s sake.

Introduction

So many words, and I’m getting to the point at the long last.

I run into ShellCheck yet another time last week and happened to be open-minded on that day, so refreshed all Bash scripts in the scripts directory in Linux Distros and Termux.

When updating termux-setup.sh to make it pass shellcheck, I revisited the linked guide Termux 高级终端安装使用配置教程 I referred to years ago and was surprised to find out that it’s still being updated.

There is a customize extra key section in the article that I’m not aware of so I spent some time doing that, hence the post.

Extra Keys

If you can’t read Chinese, the related section is located in Touch Keyboard on Termux Wiki. You’ll need the support keys list as many key entires are strings like QUOTE for " and BACKSLASH (or a silly '\\\\', but backslash won’t work) for \.

Alternatively, you can check ExtraKeysConstants.java for a complete list although the documentation said it’s defined in ExtraKeysView.java.

You can edit "$HOME"/.termux/termux.properties to customize it, and remember to run termux-reload-settings to apply the changes, otherwise it would only work after you open a new session or reopen the app.

Default

Extra keys are enable by default since 2020 so chances are you won’t need to customize it if the following makes you satisfied already.

Termux Default Extra Keys Layout

The default settings in termux.property:

extra-keys = [ \
 ['ESC','|','/','HOME','UP','END','PGUP','DEL'], \
 ['TAB','CTRL','ALT','LEFT','DOWN','RIGHT','PGDN','BKSP'] \
]

Advanced

Quoted from Termux Wiki:

In Termux v0.95 extra keys configuration was extended with configurable popups. Popups keys can be triggered by swiping up on the respective keys.

The syntax:

{key: KEY, popup: POPUP_KEY}

{key: KEY, popup: {macro: 'KEY COMBINATION', display: 'Key combo'}}

And the official example (which is also included in termux.property for your reference):

extra-keys = [[ \
  {key: ESC, popup: {macro: "CTRL d", display: "tmux exit"}}, \
  {key: CTRL, popup: {macro: "CTRL f BKSP", display: "tmux ←"}}, \
  {key: ALT, popup: {macro: "CTRL f TAB", display: "tmux →"}}, \
  {key: TAB, popup: {macro: "ALT a", display: A-a}}, \
  {key: LEFT, popup: HOME}, \
  {key: DOWN, popup: PGDN}, \
  {key: UP, popup: PGUP}, \
  {key: RIGHT, popup: END}, \
  {macro: "ALT j", display: A-j, popup: {macro: "ALT g", display: A-g}}, \
  {key: KEYBOARD, popup: {macro: "CTRL d", display: exit}} \
]]

I think there is something wrong with it as running termux-reload-settings after setting this would throw a JSON parsing error. Anyway, I combine all the configuration above and create my own.

Boosted

Let’s have a simple one first:

Termux Customized Extra Keys Layout

extra-keys = [['ESC','|','BACKSLASH','`','UP','QUOTE','APOSTROPHE'], \
['TAB','CTRL','ALT','~','LEFT','DOWN','RIGHT','ENTER']]

Every [] stands for a line of buttons, so we would get two rows using the settings above.

~ is the literal ~, meaning $HOME in shells. I think all other keys are evident.

Configuration

Now we have this (j/k, don’t worry, I’ll explain it in #Explanation):

Termux Boosted Extra Keys Layout

extra-keys = [[ \
  {key: ESC, popup: {macro: ": q ENTER", display: "vim quit"}}, \
  {key: '|', popup: '-'}, \
  {key: BACKSLASH, popup: '/'}, \
  {key: '`', popup: {macro: "CTRL SPACE %", display: "tmux vert"}}, \
  {key: UP, popup: {macro: "CTRL u", display: "UP PG"}}, \
  {key: QUOTE, popup: {macro: "CTRL SPACE QUOTE", display: "tmux land"}}, \
  {key: APOSTROPHE}, \
  {key: PGUP, popup: HOME], [\
  {key: TAB}, \
  {key: CTRL, popup: {macro: ": w ENTER", display: "vim save"}}, \
  {key: ALT, popup: {macro: ": wq ENTER", display: "vim wq"}}, \
  {key: LEFT, popup: {macro: "CTRL a", display: "HOME"}}}, \
  {key: DOWN, popup: {macro: "CTRL d", display: "DN PG"}}, \
  {key: RIGHT, popup: {macro: "CTRL e", display: "END"}}, \
  {key: '~', popup: {macro: "QUOTE $ H O M E QUOTE", display: "$HOME"}}, \
  {key: PGDN, popup: PGDN} \
]]

Explanation

Vim

{key: ESC, popup: {macro: ": q ENTER", display: "vim quit"}}, \
{key: CTRL, popup: {macro: ": w ENTER", display: "vim save"}}, \
{key: ALT, popup: {macro: ": wq ENTER", display: "vim wq"}}

Although I know how to exit Vim and use it everyday, it’s really cumbersome to do it in virtual keyboard. BTW I tried Emacs a few times, and every time it ended up feeling like a monster I can’t control, but god mode and org mode are really cool! It would be a bit pointless if I switch to Emacs and settle on evil mode. Also, I tried Neovim and Vim plugins several times, there are always minor issues preventing me from completion. Maybe I’ll try these another time soon™.

Also, I don’t want to press two keys to move half page forward/backward. Instead, I scroll the screen directly, or swipe up extra keys:

{key: UP, popup: {macro: "CTRL u", display: "UP PG"}}, \
{key: DOWN, popup: {macro: "CTRL d", display: "DN PG"}}  

Tmux

{key: '`', popup: {macro: "CTRL SPACE %", display: "tmux vert"}}, \
{key: QUOTE, popup: {macro: "CTRL SPACE QUOTE", display: "tmux land"}}  

Note that I use ohmytmux and change the command prefix (<prefix) to <ctrl> + <space>. I think by default <prefix> would be <ctrl> + a for ohmytmux and <ctrl> + b for vanilla tmux.

Shell

{key: LEFT, popup: {macro: "CTRL a", display: "HOME"}}}, \
{key: RIGHT, popup: {macro: "CTRL e", display: "END"}}, \
{key: '~', popup: {macro: "QUOTE $ H O M E QUOTE", display: "$HOME"}}, \

The first two are common hotkeys in Bash, and is just in the very beginning of The Art of Command Line which I highly recommend reading. The last one is used when I write scripts, as relative path won’t work sometimes. Since I mentioned The Art of Command Line, I also spent a lot of time reading Bash Strict Mode, pure-sh-bible and pure-bash-bible when learning Bash scripting. They are all extremely useful materials and worth checking every once in a while.

Misc

The remaining ones are quite evident to me so no explanation on these:

{key: '|', popup: '-'}, \
{key: BACKSLASH, popup: '/'}, \
{key: APOSTROPHE}, \
{key: PGUP, popup: HOME}, \
{key: TAB}, \
{key: PGDN, popup: END}

Limitation

First, you need to really use it. Tools are just tools, they can make you seem 100x more productive, but only when you are producing something.

Second, key strokes in macros are sent in a sequence, NOT simultaneously. That’s how I understand why the following configuration does not work:

{key: LEFT, popup: {macro: "CTRL SPACE LEFT", display: "tmux ←"}},
{key: RIGHT, popup: {macro: "CTRL SPACE RIGHT", display: "tmux →"}}

Or maybe I just misspell it somehow, as I notice the default advanced has a similar macro.

Postscript

I’m (again) back to the No Man’s Land I mentioned in Background so many of my plans have to be postponed. I don’t know why I’m writing this postscript since it means nothing to you, but anyway just write it to make me feel connected with the world as there is literally no entertainment in a rural (read: isolated) land besides gossiping and bragging which I’m totally sick of.

Vinfall's Geekademy

Sine īrā et studiō