You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
9.6 KiB

---
title: Terminal tools that make my life easier
tags:
- Linux
- Vim
- FZF
description: I spend a lot of time on the command line, and honestly believe that I am more productive because of that. Here are some of the tools that help me out on a day to day basis.
date: 2020-03-20
---
The terminal is often seen as a "hipster" tool that people only use to make their lives difficult. I disagree with that sentiment. I can honestly say that I am more productive due to the efficiency boost I get from using command line tools.
That is not to say I do everything in the terminal, I don't. There are graphical tools that I rely on and I would struggle to do a days work without. I am a proponent of people using the tools that they want to use. The purpose of this post is to shed light on some of the tools I use, not to evangelise you into using them.
Many of these tools come with a learning curve. However, in my case, the time I put into learning them has been more than made up for in the increased productivity I've enjoyed since learning them.
## The shell
This is often overlooked, but is arguably the most important tool that you use in the terminal. It is the program that you interact with when you first open a terminal. The most common shell on unix-y operating systems is [Bash](https://www.gnu.org/software/bash/). I don't want this to become a comparison of different shells, most of them are similar in function, but I would encourage terminal users to learn the basics of their shell as a programming language. Being able to `cd` into folder is not really much faster than double clicking on a folder in your GUI file manager. File manipulation on the terminal gets much faster when you can say create 3 folders called `folder1`, `folder2` and `folder3`, then in each of those folders create files called `file1`, `file2` and `file3`.
```bash
mkdir folder{1..3}
touch folder{1..3}/file{1..3}
```
This results in
```
.
├── folder1/
   ├── file1
   ├── file2
   └── file3
├── folder2/
   ├── file1
   ├── file2
   └── file3
└── folder3/
├── file1
├── file2
└── file3
```
Or, how about pinging a list of ip addresses 3 times, and outputting the result to file with a sensible name.
```bash
for ip in "10.0.0.1" "10.0.0.2" "10.0.0.3" "10.0.0.4" ; do
ping -c 3 "$ip" > "$ip.ping"
done
```
This results in
```
.
├── 10.0.0.1.ping
├── 10.0.0.2.ping
├── 10.0.0.3.ping
└── 10.0.0.4.ping
```
Using your shell as a programming language could easily make a blog post by itself. When that is paired with the fact that you can write these as scripts in separate files (shell scripts) and call them on demand as needed makes this the single largest productivity boost for me. A lot of common tasks can be automated with shell scripts. Here are a few of my favourite:
* [Organise photos into portrait or landscape folders](https://git.jonathanh.co.uk/Jab2870/dotfiles/blob/master/bin/.bin/images/lanPortImages)
* [Add up the length of all audio tracks in a folder](https://git.jonathanh.co.uk/Jab2870/dotfiles/blob/master/bin/.bin/albumDuration)
* [Encode text in multiple different ways at once](https://git.jonathanh.co.uk/Jab2870/dotfiles/blob/master/bin/.bin/encode)
## Vim
[Vim](https://www.vim.org/) is the text editor I use, and I use it a lot. For everything from programming, emails, letters, reports and this blog. I don't do this to punish myself, as many assume, but because it makes my life easier.
There is a misconception that Vim is hard to use. In truth, anyone can use Vim. Type `vim <filename>` into your terminal to open a file, push the `i` key to enter insert mode and you should be able to do what you need. The arrow keys will move your cursor and the letter keys will type letters. There is nothing groundbreaking there. Apart from mouse support, you have a basic text editor that anyone can use. This obviously isn't the whole story, but I hope it goes some way to shift the misconception that it is hard. Rather than hard, Vim is powerful. It has many features that can help you, but you don't need to learn all of them before you start.
Modal editing is probably Vim's distinguishing feature. The concept is simple: change what the keyboard keys do based on the mode we are in. Our smartphones all do something similar: when we are typing a text message, we see a full keyboard; if we are typing a phone number, we just get numbers. Modal editing in Vim is very similar. Obviously, it can't change the keys on the keyboard, but it can change what they do.
![Android keyboard modes. Qwerty on the left, number on the right](/assets/tools/android-keyboard-modes.png "Android Keyboard Modes")
Vim has 4 main modes:
* Insert mode - this is the mode that is most like all other text editors, if you push the `a` key, an `a` is inserted where your cursor is
* Normal mode - this is the mode you land in when you first enter Vim. It is optimised for moving around the document and editing specific parts of a document
* Visual mode - this mode allows you to select text to perform actions on
* Command mode - this mode if for running more complex commands on either a document or a selection.
If you only take one thing away from this blog post, I hope it is this: Vim isn't as hard as a lot of Vim users would like you to think it is.
If your interest has been peaked, I would encourage you to open a terminal and type `vimtutor`. It will walk you through some of the basics of normal mode and insert mode. From there, you should be able to start using Vim at a basic level. You can then pick things up from reading Vim's excellent help pages or, if you prefer, there are hundreds of blog posts and videos on the internet that walk you through some of the more advanced aspects. Remember, you don't need to master all of Vim's features to use Vim.
## Colemak
I spend a lot of my day typing, and I hope to continue to be typing for some time to come. [Colemak](https://colemak.com/) is an alternative keyboard layout that is designed to be faster and more ergonomic than Qwerty. It is not strictly a terminal tool, but since the keyboard is the primary method of input when using terminal tools, I feel I can justify including it.
If you are an English speaker, you are probably using a Qwerty keyboard, named because the top row spells qwerty with the first 6 keys. It was designed when typists used typewriters and was sold as an improvement over alphabetical keyboards in 1873! One of its improvements over the old keyboard was that the layout made the typewriter's "arms" jam far less often. My keyboard today doesn't have arms to jam, so using a keyboard layout that was designed to reduce jams seems pointless.
If you haven't ever looked into alternative keyboard layouts, I should warn you - it is quite a rabbit hole. The three most common non-querty layouts are, at the time of writing:
* [Dvorak](https://www.dvorak-keyboard.com/)
* [Colemak](https://colemak.com)
* [Workman](https://workmanlayout.org/)
## FZF
This is a relatively recent find for me. However, it has soon became one of the tools I use most. It follows perfectly one of the key Unix philosophies of doing one thing, and doing it well. In its most simple form, [FZF](https://github.com/junegunn/fzf) is a tool for searching through a list interactively. It is powerful because the list you give it, can be anything. Here are a few examples I use it for:
![Searching through files](/assets/tools/fzf-files.gif)
![Search through wordlists](/assets/tools/fzf-wordlists.gif)
![Killing Processes](/assets/tools/fzf-process.gif)
![[Completing hashcat modes](/blog/fuzzy-search-hashcat-modes.html)](/assets/fuzzy-search-hashcat-modes/hashcat-fzf.gif)
## FD
[Find](https://www.gnu.org/software/findutils/manual/html_mono/find.html) is one of those tools on Unix-y systems that most people only scratch the surface of. It is incredibly powerful, but for most use cases, it is overkill.
Enter, [FD](https://github.com/sharkdp/fd). It doesn't do everything find does, but it does do what most people use find for. As an added bonus, [it is fast](https://github.com/sharkdp/fd#benchmark).
As an example, lets say we were looking for a file who's name contains the word "test", although we can't remember if it is capital or lowercase.
**Find**
```bash
find -iname '*test*'
```
**Fd**
```bash
fd test
```
So, not only is the command shorter, it defaults to a "smart case" search. Basically, if you type in all lowercase, it will do a case-insensitive search; if you put a capital in there, it assumes you know the case and will search case-sensitively.
Also, by default, it will ignore files in your `.gitignore` and hidden files / directories. Of course, you can change this functionality but for 90% of use cases, this is helpful.
## RipGrep
As you might have guessed, [RipGrep](https://github.com/BurntSushi/ripgrep) is to [grep](https://www.gnu.org/software/grep/), what FD is to find. A fast alternative with defaults that are more helpful 90% of the time.
Similarly to Fd, RipGrep has smart case, interprets `.gitignore` files and will ignore hidden files.
## Bat
Anyone who has used the terminal a couple of times knows about the `cat` command. [Bat](https://github.com/sharkdp/bat) is very similar to cat, but with a few big advantages.
![Bat with syntax highlighting](/assets/tools/bat.png)
If you are not piping the output to another file, Bat will syntax highlight your code, it will add line numbers and, if you're in a git repo, it will show you which lines have changed since your last commit.
---
This is by no means an extensive list. There are many tools I use that I have not listed here, but these are the ones that have made the biggest difference to my workflow.