Coding Painlessly in Python or JS on Android in Termux

But What About Replit?

I still love Replit. It makes coding in Python and JavaScript on Android super easy.

But recently I was tempted to cheat on Replit with Termux. You can do most things you'd ever want to do in Python on Replit — but not all.

That's not a shortcoming of Replit. After all, you can still write whatever code you want in whatever language. The key point is that the "Run" button is only going to run supported code successfully. Therefore, if you're going to write code using a technology that is difficult, janky, or downright impossible to test with Replit's tools in the browser (e.g. Tkinter, curses, etc), the best you'll be able to do is use Replit strictly as an IDE for writing your code, and then test it elsewhere after syncing it with Github. Eek.

Also I may have been a bit dismissive of Termux in a previous article, considering how much power it has.

What's the Advantage of Termux?

Termux is basically a Linux distro that runs in an app on your Android phone — even if you're not rooted. Loads of linux packages are available for you to install, including (but not limited to) the real versions of such venerable tools as vim, ssh, git, nodejs, and python.

There are some limitations, however. Any tool that requires a specific desktop environment or architecture might be able to detect that you're on an unsupported platform (e.g. if you try to install and use expo-cli for react native development).

There are basically two or three ways around this, depending on how you're counting:

TermuxArch

This is metaphorically speaking a kind of a wild west option, and like anything that comes in wild west flavors it's sure to be an adventure.

It is possible to install other full-fledged linux distros in a directory within Termux, and in doing so that directory becomes the root directory of the filesystem for the other linux distro.

The proot project that enables this is an implementation of an older unix technology called chroot. Within that local "jail" filesystem, root privileges are emulated.

After installation, you'll set up a VNC server that serves up the GUI of the new distro to a port on your phone's localhost. Then you'll consume that GUI via a free VNC client app on Android that emulates mouse and keyboard functionality for you.

Where this has the potential to get wonky for you is that obviously not every workflow you might want to engage in is even tested on any linux desktop, much less in an obscure situation like this.

Another downside is that you might end up using up a huge amount of the limited storage space on your phone with all the downloaded distro files.

Option 2: Just get a Raspberry Pi or a VPS

Either one of these options is relatively low-cost and enables you to choose from a great selection of mainstream linux distributions. Personally, I would prefer the one-time cost of a raspberry pi on my home network, but that option may not work for everyone.

In both cases, you'll set up SSH and VNC so that you can easily consume your desktop from your phone. You can write your code via ssh in nano or vim, and you can test anything graphical via VNC.

Does this seem like a lot to go through?

If you're up for a challenge like this, you're in good company because Replit itself is powered by some of the same technologies.

Text-Editing Painlessly in Termux

Unless you're already a vim user, start out with the nano text editor. After installing Termux from F-Droid (NOT Google Play), make sure nano is installed with the command pkg search nano. (Basic bash commands to do things like list the contents of directories and change directories are outside the scope of this article.)

Before you go any further, make sure you have your keyboard situation figured out. If you are using a hardware keyboard (e.g., via bluetooth) then you don't have anything to worry about.

There are a few more options when it comes to software/touch keyboards.

The simplest, low-config option option is to use something like Hacker's Keyboard (freely available in the play store). It gives you access to all the keys you'll need in the terminal. The downside is the keys are a bit smaller and the layout is different from your usual keyboard probably, so you'll be switching back and forth and it might get annoying.

The option I prefer, which is not much more difficult, is to keep using your keyboard that you use for everything else but activate Termux's extra keys row/section above your keyboard. To do that, you'll swipe in from the left of the screen to open the drawer and long press on the keyboard button. You can edit the ~/.termux/termux.properties file in nano so that the exact selection of extra keys you want appears above your normal keyboard everytime you're in Termux (I highly recommend at least having ctrl, alt, tab, esc, forward slash, and all four arrow keys).

If nano is installed and your keyboard situation is taken care of, then there are a few simple configurations I recommend you make to the editor to make it more user-friendly. Here's what my .nanorc file looks like (located at ~/.nanorc in the Termux filesystem):

set tabsize 4
set tabstospaces
set softwrap
set atblanks
set linenumbers
set indicator

You can find more ways to customize this file here. To create and edit the file in one command you can type touch ~/.nanorc && nano ~/.nanorc. Note that you can scroll the cursor up and down the editor by scrolling your thumb in Termux. To save the file hit CTRL-o or CTRL-s when you're done and then CTRL-x to exit the editor. Note that your phone's volume down button is a shortcut for the CTRL button when you're in Termux.

What About Vim?

It's just one pkg install vim away. One advantage of vim is that it's more configurable than nano. I actually prefer nano when I'm using the touch keyboard because nano isn't based on "modes" so much as WYSIWYG. However when I have time to break out the bluetooth keyboard I'll open up vimtutor and practice.

Conclusion

That's about it. I like finding new ways of writing and running code. Any questions or experiences you've had with Termux or coding on Android? Let me know in the comments. Don't forget to like, subscribe, and share.