Command-Line Efficiency: Optimizing your Workflow

1 178
Avatar for Polydot
4 years ago

Ah yes, the joyous command-line interface, a friend among the tech savvy, and often a feared entity by computer novices. While those who work in tech usually know better, average people likely have seen the command-line interface associated with hacking. This is no doubt thanks in part to the way Hollywood films portray "hackers" furiously typing away inside their command-lines while command outputs flash down their terminals, I digress.

Note: Terminals and command-line interfaces (cli) are different things that work together, in short the cli is the part that takes input from a user and outputs it in the terminal.

Realistically command-lines are far more often used for simple tasks such as text editing, emailing, or browsing files, in an efficient fashion. You can even do basic web browsing in the command-line, though it is somewhat limited, nevertheless it is neat and worth checking out. With that in mind we will be diving into how to make your everyday cli usage more efficient, and all around less annoying.

Aliases

Setting up an alias to be called inside your command-line will vary depending on what shell you have. I'm going to be making the assumption that you are using the Bash shell since it is quite universal in all Linux based operating systems. If you have no idea what shell you are using then open up a terminal and print it out with the following command.

$ echo "$SHELL"

The output should say "/bin/bash", if it doesn't don't worry, you can probably make aliases in whatever shell you are using, the syntax will just be a little different, but that isn't anything a quick web search can't solve. You could also change your shell to the Bash shell if you are so inclined.

To make your first alias in the Bash shell, open up your '.bashrc' file in your home folder (~) with whatever editor you like, I'm using Vim.

$ vim ~/.bashrc

Once the file is open jump to the bottom and add this line. Then save and quit back to your terminal.

alias c="cd"

That is your first alias, we are simply mapping (aliasing) c to be the cd command inside when ran from the cli. To make these changes take effect you will need to reload your bashrc, this can be done with the following command.

$ . ~/.bashrc

Now run any normal cd command by only typing c followed by it's parameters. The following example is me moving back one directory.

$ c ..

How simple was that? There isn't much to making aliases but nevertheless you can get a lot of bang for your buck. I'll list a few more simple aliases to get your imagination started.

alias reload=". ~/.bashrc"       # Reload your bashrc

alias diff="diff --color"        # Always use diff with color

alias t="touch"                  # Shorthand for touch

alias mv="mv -i"                 # Prompt before file overwrite

alias cp="cp -i"                 # Prompt before file overwrite

alias rm="rm -I --preserve-root" # No root delete, warn when removing 3+ files

Shell

Shells in short are interactive programming languages. A very literal example of a shell being a interactive programming language is the python shell. In the previous "Aliases" section of this article I mentioned the Bash shell. Bash is a more traditional shell that provides a simple language for executing programs, writing programs, and combining programs; as well as extra features like the aforementioned aliases.

It is worth starting with a little history, Bash was first released in 1989, it was intended to be a free and open source replacement for the "Bourne Shell", hints Bash being short for "Bourne Again Shell". Since then Bash's prominence has exploded and it has become the default shell on many operating systems. This includes versions of macOS, and most Linux distributions.

Now that we know what we are dealing with lets take a look at configuring our Bash shell.

Start by opening the bashrc file like previously shown, this file will be the hub for configuration. Depending on your operating system this file may already have some defaults, to avoid breaking anything, you can always backup the bashrc by saving a copy of the current bashrc file and calling it bashrc.old.

$ cp ~/.bashrc ~/.bashrc.old

With a backup file made it is safe to go ahead and start making changes to our bashrc file. We are going to get started by toggling some parameters to make our cli use more pleasant and efficient.

shopt -s autocd   # Cd into directory with only it's name

shopt -s dirspell # Correct spelling errors

shopt -s cdspell  # Also correct spelling errors

Using the shopt command followed by -s flag (shopt -s) we are able to enable different shell options. Now if we reload our bashrc with the previously mentioned command -- or the alias we previously made -- then these changes will take effect.

$ . ~/.bashrc

To test the enabled autocd functionality you should be able to type a directory name and hit the return key to change directories (this is without using cd or the c alias). With Bash's dirspell and cdspell enabled Bash will be able to fix spelling errors when you tab complete a command -- yes you can hit tab to auto fill in partial commands, folder names, etc.

Here are a few more shell options I like to toggle on.

shopt -s histappend   # Append to history file

shopt -s cmdhist      # Save multi-line command as one line

shopt -s checkwinsize # Commands fit to window

To get a list of possible bash shell options (shopts) you can run the following command, this command also tells you if the option is toggled on or off.

$ bash -O

Another set of configuration parameters exists inside of Bash, but are not Bash specific. Having two sets of options comes down to Bash's history of being a replacement for the "Bourne Shell". Options accessed through shopt came later as additions to Bash, but options accessed through set are "Bourne Shell" options. To access a list of these "Bourne Shell" options you just run the previous command but with a lowercase flag.

$ bash -o

Two options that might be of interest to lovers of Emacs or Vim, are the corresponding emacs and vi mode. Both provide some standard functionality of their respective editor, I'm personally a big fan of vi mode in my shell. Setting these legacy options is almost the same, but with the difference being you use the set command, followed by a -o flag (set -o).

set -o vi

Once I reload my configuration file I have access to Vim's normal and insert mode. You can swap between them with the standard keys of i and Esc. Vi mode is pretty feature rich, so as a Vim user it sure beats standard cli navigation.

Summary

One final tip, if you would like to separate out your aliases from your other Bash configurations you can make an alias file. Separating your bashrc file from a Bash alias file allows for quicker and cleaner configuration. To use an alias file first create one inside your home directory.

$ touch ~/.bash_aliases

Then place the Bash if statement below into the bottom of your bashrc,

if [ -f ~/.bash_aliases ]; then

    . ~/.bash_aliases

fi

Now any alias you create can be added and read from the bash_aliases file -- be sure to reload the bashrc configuration file each edit. Separation like this is wonderful, especially when you get a lot of aliases, or Bash configurations.

Even with just these small tweaks we have improved our ability to perform tasks in an efficient and customizable fashion. You can take all these configurations much further and may want to do so depending on the kind of workflow you are aiming for. I would even recommend writing custom shell scripts for automating common repetitive tasks -- like extracting files or compiling programs -- but explaining shell scripting is out of the scope of this tutorial.

Veteran and novice alike, implementing some of these basic quality of life changes will make everyday tasks vastly more enjoyable. As we all know when you actually enjoy what you do, you'll do it a lot more, so get to optimizing your workflow.

1
$ 0.00
Avatar for Polydot
4 years ago

Comments

We all know very well that efficiency is the backbone of everything. But here we will help the students to get a personal statement online can save time and stress, but it's crucial to ensure originality and authenticity. Make sure to https://ukwritings.com/write-my-essay choose a reputable service that offers custom, plagiarism-free writing tailored to your unique experiences and goals. This approach can provide a polished, professional statement that effectively showcases your strengths to admissions committees.

$ 0.00
5 months ago