Skip to content
jdurston.net/blog
Primary Navigation Menu
Menu
  • About

Emacs

Getting more Bash for your buck

2016-12-21
On: December 21, 2016
In: learning, old school, toolchain

Bash logo
image source

As someone who is interested in programming and Linux, I feel it’s important to be comfortable on a command-line. I guess I subscribe to an old-school mentality. I really enjoy being able to interact with my computer through a keyboard.

Recently I decided to go further back to basics and focus more on what a stock system can do. So I uninstalled Zsh, and Tmux, and have been running just plain-old Bash again for a while now.

Before I go any further, I’d like to acknowledge that most of what I’m going to talk about below, I learnt from Peteris Krumins‘ blog. He has several excellent pages and cheat-sheets that I will link-to throughout this post.

So, here’s what I should have learnt first, things that Bash has built-in:

GNU Readline

GNU Readline is a library that can be used with any software project. It is built-in to Bash. It has two main features:

  1. Editing of command lines
  2. Maintain searchable/editable history of previously entered commands

A note on Vi/Emacs

GNU readline has two editing modes; Vi, and Emacs. And (I guess because it’s GNU) the default mode is Emacs. This means that the keyboard shortcuts are Emacs or emacs-style ones. I am a Vim user. I don’t want to get too-much into the eternal Vi/Emacs holy war; but I am in fact happy to stick with the default Emacs setting, for two reasons:

  1. There really aren’t that many commands/use-cases to learn for single-line editing
  2. I think Vi-style modal editing might be a little overkill for single-line editing. Admittedly I haven’t given this a lot of thought, it just seems slightly more clunky

Editing of command lines

Peteris has this page about Bash Emacs editing mode, and an awesome cheat-sheet there too.

Bash command-line history

Peteris has this page, entitled ‘The Definitive Guide to Bash Command Line History’

So, I have been focussing on this page and it’s cheatsheet. And in particular, the Emacs-mode keyboard shortcuts. Here’s how I understand manoeuvring the history:

Incremental vs non-incremental search.

As far as I can tell non-incremental search means search once. i.e. I type Alt+p, and then e, and then hit Return, it gives me exit, which is the first command backwards in (my) search history. if I then try to type Alt+p again, to get the next-command-back that matches e, the search resets. So really this command is useful for going to the first matching command only.
With incremental search, If I type Ctrl+r, then e and Return, I get exit (as expected), but if I then type Ctrl+r again, I get vim /etc/inputrc. Each time I subsequently type Ctrl+r I go back through the history to the next matching command. Of course this works forwards as well as backwards. Here’s a summary:

Do one search (non-incremental):

Alt+p backwards << + >> forwards Alt+n

Do an incremental search – allows you to hit the command again to move through matches:

Ctrl+r backwards << + >> forwards Ctrl+s

 

Wait! Ctrl+s doesn’t work, it just hangs my terminal!!!

Ah yes, on a lot of terminals / terminal emulators (and on Gnome-terminal in Ubuntu by default at the time of writing this) Ctrl+s is used to send the XOFF instruction. According to this Stack Exchange question it is a legacy of (very) old terminals which was something to do with pausing the terminal output. This was useful on terminals that did not allow scrolling; presumably you could use XOFF and XON like a pause button. But I guess nowadays it’s a lot less relevant. I disabled it by adding the following line in my ~/.bashrc file, as recommended by the Stack Exchange answer:

stty -ixon #Disable XON

 

That’s all for now. I’ve only scratched the surface here of what Bash is capable of, but already I feel more productive being able to call-up frequently used commands from history, and more competent not resorting to mashing the up/down arrows so much!

Tags

Bash Code Golf Code Kata Code Wars command line Emacs GNU Readline Java old school Programming Project Euler Tmux Vi Vim Zsh

Categories

  • learning
  • old school
  • programming
  • toolchain
 

Enter your email to subscribe and receive notifications of new posts

All content, unless explicitly otherwise stated, is Copyright © 2015 - 2021 John A Durston.
Cookie Policy

This site uses cookies: Find out more.