Terminal for every day
Tags: tooling
Published: 2022-05-13 18:15:23

The console is one of the main tools of many developers. Someone needs to open a terminal more often, someone less often. But one thing I notice exactly, this tool is greatly underestimated. In an era of convenient and advanced IDE, which are created to the maximum comfortable and working out of the box many engineers forget or do not learn about the features that give us a seemingly scary and "inconvenient console for admins". In this article I will talk about several utilities that I use most often in everyday work. I hope this is a description not only will it help to discover something new, but also to look differently at the use of the console.

## Bat

[Bat](https://github.com/sharkdp/bat) perhaps the easiest thing to use from my list. This is the same cat only with syntax highlighting. Let’s compare the two output options.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djgvbc2c2jrlap212fud.png)

The difference is obvious. In my example, I have a screenshot of minimalist nginx config. In reality they are often much larger, especially if it is a main balancer in a large company and highlighting helps a lot. Highlight is not the only feature of the utility. It also has seemingly small, but very useful conveniences.

- Scroll Large Files
- Diffs
- Reading a mans
- Display of service elements (spaces, tabs, hyphens, etc.) 
- Logging.
 
It is difficult not to agree that here such logs to read much more pleasant and visually `tail -n 100 access.log | bat -l log`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpwny894sm0zihk4u39x.png)

Color schemes to choose from have several `bat --list-themes`. I like VSCode stylist. And of course there is a possibility to create your own schemes. 
Also bat can be integrated into other tools, which I will talk about below.

## Lf

[Lf](https://github.com/gokcehan/lf) is a very fast and lightweight file manager that only does its job and does not absorb as combines like mc. With it, you can quickly move to the desired directory and stay in the same session as you were. Of course, you can perform all the basic operations that are required every day: copying, deleting, creating character links, moving, etc. Supports group operations. But still the main advantage for me was its programmability. You can create whole commands that run whole scripts on bash or something. In the wiki project a set of some ready-made functions using the ability to create your own commands.
The bookmark function was very useful. I have a set of directories to which I move quite often that cd even with auto-compile can be tiring. But everything changed when implementing a simple command:

```
cmd bookmark_jump ${{
   res="$(cat $LF_BOOKMARK_PATH/$(ls $LF_BOOKMARK_PATH | fzf))"
   lf -remote "send $id cd \"$res\""
}}

map bj bookmark_jump

cmd bookmark_create ${{
   read ans
   echo "$PWD" > "$LF_BOOKMARK_PATH/$ans"
}}

map bc bookmark_create
```

Now `bj` adds the current directory to the bookmarks, and `bc` lists all the new ones. This example clearly shows how easy it is to implement functionality.
Here is an example of another useful command. Delete to Trash with confirmation.

```
cmd delete ${{
   set -f
   printf "$fx\n"
   printf "delete?[y/n]"
   read ans
   [ $ans = "y" ] && mv $fx ~/.trash
}}

map d delete
```

Pressing `d` will first ask for confirmation of the operation, and then move to the trash. The flight for creativity in creating an instrument for themselves and their needs is unlimited.
Another useful feature is the ability to view files while navigating through directories. But even more importantly, you can attach a bat to the views! This is as simple as bookmarks. First we create scripts that will call when preview.

```
#!/bin/bash
unset COLORTERM
bat --color=always "$1"
```

Now add to Lf configuration.

```
set previewer ~/.config/lf/bat.sh
```

And now bat is used for browsing with all its advantages. And you can set any settings in the script above, including various options depending on the file type, its size and so on.
For vim users the pleasant news will be the opportunity to use for positioning cursor habits `hjkl` without moving your fingers on the arrows. This makes the Lf works extremely fast.
I highly recommend to refer to the wiki project where there is a description of many lf possibilities, as well as some ready-made ways of expansion.

## Exa

[Exa](https://github.com/ogham/exa) is a modern replacement for the old good `ls`. Unlike its predecessor 
- Uses highlight for different file types
- Detects symlinks, displays the actual location of files
- Supports git
- Can display directories as a tree
- Supports various color schemes and fonts (including icons)

Example of what an output might look like

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vflekrc6e6l5syrf3018.png)

## fzf

[Fuzzy finder](https://github.com/junegunn/fzf) is a search utility for a given set of elements that you can embed almost anywhere and anything. I’ll show you a simple example of finding the right process in the system. Most often used to search utilities like grep, which give the opportunity to look not all the sheet and search for keywords or templates what you need.  Let’s see how it looks with fzf

```
ps aux | fzf --height 40% --border
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tp6msv8os3pw7a0799yh.png)

Fzf makes it possible to use live search, which makes it more convenient, fast and visual. Conclusively, Fzf supports templates for fuzzy queries.
- 'wild	Items that include wild
- ^music Items that start with music
- .mp3$	Items that end with .mp3
- etc

But the main convenience is the ability to embed fzf in any script or pipeline and make it interactive. Here is an example for stopping a running container.

```
function ds() {
  local cid
  cid=$(docker ps | sed 1d | fzf -q "$1" | awk '{print $1}')

  [ -n "$cid" ] && docker stop "$cid"
}
```

I have the source of all projects in the same directory. Since my company has many microservices, there are also a lot of catalogs with projects. Navigating through the file manager every time can be exhausting, as I can do this procedure several times a day. This simple script made my life easier:

```
function cdp() {
	local project_path
	project_path=$(ls ~/go/src/github.com/mycompany | fzf)
	
	cd ~/go/src/github.com/mycompany/$project_path

	changes=$(git status --porcelain)
	if [ -z "$changes" ]
	then
		git checkout master
		git pull origin master
	else
		echo "There are uncommited changes. Can't checkout and update master"
	fi
}
```

As you can see here not only moves to the desired directory, but switches to the wizard and its update, but only if there are no unchecked changes. It’s one of my scripts, in reality there are many more of them, and they are tailored to my particular needs. Automation requires a small investment, but after that it pays off with excess.
Examples where I use it are still:
- Create a new branch in the git. The name is the same as the task number in Jira. I take the ticket list of those in the "In progress" status and assigned to me
- I run the timer, choosing from the list of previously run timers. I have many similar records of the form "meeting", "duty", "planning" and so on.
- Switching between kubernetes cluster. I have several dozen of them in operation
As you can see very specific tasks for me and everyone here can create under themselves)

For typical tasks there is a huge number of ready-made solutions for all major areas of the workflow, both plugins and scripts. You can fix the second ones for yourself, not write from scratch.
- [git](https://github.com/wfxr/forgit) Work with git. Switch branches, search history with diffs viewing, interactive rebase and more.
- [kubernetes](https://github.com/thecasualcoder/kube-fzf) Helps to search for pods, namespaces and other frequent features
- [tmux](https://github.com/sainnhe/tmux-fzf) More convenient and intuitive switching between windows, sessions and more.

In the wiki project there is a set of ready-made functions and scripts. These are the ones that seemed most useful to me
- [Switching between tmux sessions](https://github.com/junegunn/fzf/wiki/Examples-(fish)#tmux)
- [Stop/Remove Containers, Clear Images](https://github.com/junegunn/fzf/wiki/Examples#docker)
- [A set of utility for vim](https:/github.com/junegunn/fzf/wiki/Examples-(vim). Navigation by buffers, tags, files (But I still prefer telescope)
- and even [chrome bookmarks] (https://github.com/junegunn/fzf/wiki/Browse-chrome-bookmarks)
By the way, for files there is a possibility to display a preview of their content before selecting it. And of course you can use bat.

## Direnv
[Direnv](https://github.com/direnv/direnv) is a utility allows you to automate setting environment variables in a directory. Works this way. In a file `.envrc` contains the required environment variables and they are automatically displayed when moving to the directory. Do not do it manually and remember about it. That’s all) Quite a simple task, which without automation takes time. 

An example from one of my projects

```
export GITHUB_TOKEN=***
export PET_BOT_TOKEN=***
export PET_DOMAIN=https://*.eu.ngrok.io
export PET_WEBHOOK_TOKEN=***
```

When I move to the project directory I get this result

```
direnv: loading ~/project/gorelex/.envrc
direnv: export +GITHUB_TOKEN +PET_BOT_TOKEN +PET_DOMAIN +PET_WEBHOOK_TOKEN
```

# Conclusion

I told you about some of my utilities that I use every day. The flexibility of their settings and the possibilities of integration are simply limitless. You create your IDE for everything, gradually improving and complementing the automation you need. Of course, I did not tell about all the tools. There are still NeoVim and Tmux, everyone is worthy of not just a single article, but whole courses. I tried to show the main thing - the console is not difficult. The console is convenient and interesting.