Terminal Essentials: Advanced Command Line Tools For Devs and Sysadmins

Previously, I had written about terminal tools potentially useful for computer users who have discovered how handy the command line can be. For this article, I’ll dive into some advanced setups for developers and system administrators. This is not an exhaustive list of tools and tricks for the command line, and the selections may not be the ultimate ones, but they surely are a right balance of being cool as well as useful at the same time.
Various vim Tools
I won’t go into the traditional vim vs emacs battle, as they are two of the most widely-used terminal editors in use today, by a large margin. Most admins prefer one or another, and they are pretty religious about their choices.
But I do want to share some tips on how to supercharge vim and essentially make it an IDE of sorts.
To make vim useful, there are three essential things that you need to get. First is the NERDTree plugin that gives you a tree-like file explorer in the sidebar. This makes browsing and opening different files in the same project quite easy.
Second is the Tagbar plugin that shows the tags in the current file such as variables, function names etc. Of course, for that, you need Exuberant Ctags library installed.
Another trick to use them the most efficient way is to make shortcuts to toggle the NERDTree and Tagbar plugins. For that, just add the following to your vimrc
file:
1 2 3 4 5 |
" Tagbar Toggle Shortcut noremap <leader>] :TagbarToggle<CR> " NERDtree Toggle Shortcut noremap <leader>q :NERDTreeToggle<CR> |
So now, you can just do \ + ] to toggle the tree and \ + q to toggle the tagbar. Pretty cool! The next thing you should not miss is the Powerline plugin for the vim. This presents a short status-bar at the bottom of the open buffer which shows which git branch you are on, the current mode of vim (different colors for different modes), current actions and line number. For other advanced configurations of vim, you can check out Xero’s dotfiles. Another useful resource for mastering vim is the awesome vim screencasts over at vimcasts.org
vimdiff
While there are many feature rich GUI tools for diff and merge such as Meld, the ultimate one for terminal geeks is the vimdiff command. It keeps the developer in the same familiar terminal and vim zone. To diff two versions of files, you can just simply,
1 |
$ vimdiff old_version.c new_version.c |
This opens two vim buffers with those files in split window arrangement. As you can see below, these are two versions of a spec file. Changes withing a line are highlighted in purple and additions and subtractions are in red and teal colors. The colors scheme can, however, vary based on your choice.
You can jump between the two split windows with Ctrl + w + w. To move to next change, do ] + c and to move to a previous change do [ + c. Usually, you would just need to be in a single buffer and use :diffput and :diffget commands to either push changes to an adjacent file or get those changes from that file. Just move your cursor to that line and give those commands. It’s that easy!
For more advanced features such as 3-way diffs, check out this vimcast. Usually, we end up caring about diff when we have just applied a patch or diff between multiple versions in a git managed repository. Which brings us to our next tool.
tig
Have you been using the usual git log
and git diff
commands while trying to make sense of your current situation of git branch or the previous commits? Well, there are numerous GUIs for git browsing. However, a fast and lightweight terminal based is Tig. It is available on most of Linux distributions out of the box.
Start by doing a simple tig
command (easy to remember — the reverse of ‘git’) in your repo directory. The first view you will get is an organized list of all commits, identifying merge points and branches. you can use Up and Down keys to browse the commits. When you hit Enter on any commit, the view will change and would look something like in the image. It opens up a split view at the bottom which shows details of the commit — including additions and subtractions. Pressing Up and Down here would start scrolling the whole commit information. You can do PageUp and PageDown to scroll in the split view commit list above. The lower split view changes accordingly. This is useful to follow what changed since the previous commit. You can press q to quit the split view and then q again to quit tig
.
GDB
Frankly, you can’t escape GDB GNU Project Debugger. It can handle C/C++, Go, Java etc. quite well. Either through Eclipse CDT plugin or through numerous other GUIs, it must have crossed your path sometimes.
However advanced and complex GDB may have become, its command line roots are still rock solid. A debugger is actually just a gdb command away! One of the most popular uses of GDB is to just understand program flow. And there is no easier way to use it than the TUI mode.
The easiest way to toggle TUI mode during debugging is to do a Ctrl + x + a. By default, you get the source code view. To get the assembly window, type layout split
command in gdb
console. The command layout next
and layout prev
switches the TUI layouts. The assembly and the source views are synchronized and the breakpoint is highlighted as B+>
in the code window. You can select the individual split windows by Ctrl + x + o. Now as you start stepping instructions or functions, the source, and the assembly view will guide you.
For more details, Beej’s GDB guide can be a good starting point for efficient debugging. Another nifty tool is the gdb-dashboard project that gives you yet another visual representation of what is going on in your program while debugging.
htop
Writing programs in not enough. On a running system, you need to see how they are impacting the system. In case you just wish to know what processes are running on the system and what resources they are taking, top
has been a default choice. However, a more improved and interactive version of top is htop.
htop is more than just a process viewer. It can be used to set CPU affinity, send kill signals to processes, observe resource usage graphically. You can press t to observe the parent-child relationship of processes represented as a tree. Search through the processes using a familiar / and kill processes using k. And the most awesome feature till now — you can trace the application with strace
by just pressing s and attaching to it. Magical!
ncdu
Since we covered resource consumption, another nifty tool for your digital belt is the Ncurses based Disk Usage utility called ncdu
. This is the most welcome upgrade to the du
command everyone is most used to.
The files are sorted by their size and you can instantly identify the most space consuming ones. You can press i to get more information about the selected file and d to quickly delete the ones that bother you.
And of course, devs can’t miss the fun. So here is weather output piped to lolcat command. Forecast for today is … rainbow!
1 |
$ curl wttr.in/Montreal | lolcat |
That’s all folks! Logging off now.