Thoughts from a 20-year Linux user

Home | About


Posts tagged "bash":

22 Sep 2024

Choosing a terminal for your Linux PC

Choosing a terminal for your Linux PC

Each operating system comes along with a list of preinstalled applications that make your computer usable. Usually, they ship with commonly used applications such as a file manager, text editor, package/app manager, media player, web browser.

Many times though, people want to tweak their environment or do stuff that their applications were not designed for. This is where a terminal emulator comes along. It is not necessarily something that you pick up immediately when starting out with a Linux desktop distribution, but, once you gain a bit of experience, it will come in very handy.

In a nutshell, using a terminal is about combining a list of commands to achieve a specific result. If you're looking to get a quick overview, just go here.

Here's an example showing how to find big files recursively in a directory. This is useful for me when doing cleanup on my system (obviously you shouldn't delete things from your "/usr", I've just used it to showcase the usage).

gnome_terminal.png

Criteria to use

As you dig deeper into this, you will realize that, as always, there are many competing applications. The most common desktop environments (GNOME/KDE) have settled on the GNOME terminal and Konsole.

A terminal wraps a command shell, and a lot of them use GNU Bash by default, but there's a wide variety here as well. As for me, I've come across zsh (default for macOS), dash (default for Ubuntu/Debian /bin/sh, but nor the interactive terminals), but there was never a really good reason to switch to something else.

If you do plan to use something else, I would advise you to keep in mind that most support the POSIX shell standard and to try to not steer away if possible. For instance, in case of Bash one can check deviations via checkbashisms or the POSIX mode.

I'm mainly using GNOME, and I've settled for the GNOME terminal, BASH for shell/command interpreter.

Here are a list of things I look for when trying out a new terminal:

  • Multiple tabs support, also switching between tabs
  • Keyboard shortcuts support, as I'm trying to avoid the mouse
  • Shell integration
  • Themes support

Multiple tabs support

You will find that majority of terminals out there already support multiple tabs, so this is not a major factor to consider

Keyboard shortcuts

Although not obvious, there are two sets of shortcuts:

  1. Command interpreter (bash in our case) - see https://www.gnu.org/software/bash/manual/html_node/Command-Line-Editing.html
  2. Terminal related - see https://help.gnome.org/users/gnome-terminal/stable/adv-keyboard-shortcuts.html.en

Bash relies on the GNU readline library, which tries to help with command editing. By default, it offers similar key bindings as Emacs.

gnu_readline_cheatsheet.png
Figure 1: GNU readline shortcuts via https://readline.kablamo.org/emacs.html

This can be customized though, and there's also a vi mode. I think it is worthwhile to learn them, as there are many programs out there that use this library or competing ones (e.g. libedit).

See below output to get an idea of how common it is.

george@local:~$ apt-cache rdepends libreadline8t64 | wc -l
293

For terminal related shortcuts, it's important to check that they don't mess the interpreter ones.

Also, I find myself needing shortcuts for (available in the GNOME terminal):

  • Copying terminal output
  • Copy/paste in the terminal with the keyboard - CTRL-C has special meaning for interpreters

Themes support

I prefer lighter themes (picked Solarized light) and it's important for me to be able to choose a color scheme, as I spent a lot of time in the terminal.

Productivity tweaks

If you want to tweak your bash based terminal, you can get some inspiration from these themes, based on ohmybash.

I'm personally keeping things to a minimum, but I've outlined a few things I'm doing below

Prompt customization

Here are a few things that I've decided to customize my prompt for:

  • Naming terminal tabs - I use multiple tabs frequently and like to have meaningful names for them. Unfortunately the only way to set it up in the GNOME terminal

is via customizing the prompt.

  • Basic git integration (show the branch name) - I've learned the hard way that it's easy to push to the wrong place.
  • Make my local machine stand out - I connect via ssh to remote machines and I like to output local instead of my host name.

Here's how I achieve this. One can simply add the below excerpt to their .bashrc file in their home directory.

# __git_ps1 relies on the bash completion package
PS1="\[\033[01;32m\]\u@local\[\033[00m\]:\[\033[01;34m\]\w\\[\033[01;36m\]\$(__git_ps1)\[\033[00m\]\$ "

# Basically reuse the idea from https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal
function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

I basically have to tweak the PS1 environment variable. If you want to understand it, the starting point is here, also check the escape codes.

I've outlined a simplified explanation below (only special control variables explained, the rest is mostly inserted as is):

\[\033[01;32m\]
switch color to bold green
\u
insert user
[\033[00m\]
reset color to default
[\033[01;34m\]
switch color to bold blue
\w
insert working directory
\[\033[01;36m\]
switch color to bold cyan
\$(_gitps1)
function available via bash-completion, see here
[\033[00m\]
reset color to default

Afterwards, I have a reusable bash function set-title defined, which can be used as below:

set-title new title

Bash completion

I recommend installing bash-completion packages, which provides TAB completion for various commands.

sudo apt install bash-completion

If you want to know more about the underlying details, please check the documentation

Script checking

If you find yourself repeating a set of useful commands, I recommend saving them in specific files. You can then use shellcheck to verify them.

sudo apt install shellcheck

Helpful utilities

Aside from the usual suspects, there are few tools that I expect would make your life easier

ag
code searching tool, also see rg
fzf
fuzzy finder (free search)
mc
keyboard first file explorer (you might already be familiar with it, as it's a clone of Norton Commander from the MS-DOS days)
tmux
run multiple application in one terminal tab (specially useful for remote logins)
jq
simplify interaction with JSON (configuration) files, see also yq for other file formats

Learn GNU Bash

For much of day to day things, you can find a lot of examples with a simple online search.

As your needs become more customized, you will need to get a better understanding of your command interpreter.

There is a lot of documentation available, I will highlight a few I looked over the years:

Beginners guide
very old beginner's guide
Advanced Bash-Scripting Guide
a more advanced tutorial
The manual
best documentation if you know what you're looking for

Closing thoughts

Using the terminal is one of the ways to take more control of your PC. I think most of them provide a good enough experience, so, you don't need a lot of research to get started. Picking the distribution default is a good place to start. Had I been using KDE, konsole would have covered most of my needs.

Tags: Linux terminal bash
Other posts
Copyright Linux Logs @2024