5 Unix Terminal Tips To Boost Your Coding Speed | by Shalitha Suranga | Nov, 2021

5 Unix Terminal Tips To Boost Your Coding Speed | by Shalitha Suranga | Nov, 2021

Use these practices to complete your programming tasks faster

Laptop on desk. Image with title of article on it
Photo by Jantine Doornbos on Unsplash, edited with Canva.

Programmers often use terminal applications during programming to do various activities such as entering Git commands, running build scripts, and executing automation scripts. Almost all GUI operating systems have built-in terminal programs to let users do tasks via a command-line interface (CLI).

Every terminal program typically sends user-entered commands to a command-line interpreter to get the output. For example, when you enter to create a new directory, the terminal program executes the process with input arguments via a command-line interpreter like Bash. Next, the binary creates a new directory with the syscall and directs the output back to the terminal.

We can run various commands on the terminal to do different actions. These commands are almost the same on Unix-like operating systems like GNU/Linux and macOS. Most programmers prefer the terminal-based interaction with the operating system during programming. Therefore, the way you work with the terminal program directly affects your programming speed.

In this article, I will explain several Unix terminal tips to boost your programming speed.

We typically enter short-time commands on the terminal. For example, the command immediately displays the current repository status and exits. Sometimes, we have to run commands to perform long-running tasks such as opening the GUI text editor to edit a file, installing a new package, and cloning a repository over the internet. In these situations, most programmers often tend to open multiple shell instances with more tabs. But, almost all command-line interpreters offer features to manage these long-running tasks inside one shell instance.

There are different background task modes in Unix-like operating systems. The simplest and the most helpful background task type is the one that we can start with the ampersand () character. I often use the following command to open a text file with the gedit app:

gedit myFile.py &

This command opens the text editor without blocking the current shell. Therefore, I can enter another command without closing the text editor. However, this approach is not suitable for long-time tasks that print stuff to the console — because the shell gets blocked when the process prints some texts. In those scenarios, we can use the following command to detach output/error streams from the shell:

./download.sh &> /dev/null &

If the above syntax looks long, you can always create a function in as shown below:

function bgt() {
"[email protected]" &> /dev/null &
bgt ./download.sh # will do the same!

We can check the status of the background tasks with the built-in command. Spawn a process via the command if you need to keep the process active even after closing the terminal.

Practicing to type faster is not the only secret behind programmers who can enter terminal commands faster. Every terminal application offers a set of keyboard shortcuts to make you more productive while entering terminal commands. Every developer knows to fetch the previously entered commands with the arrow keys and autocomplete with the tab key. But do you know how to move the cursor faster while editing a command? Try pressing and to go to start and end respectively.

Sometimes, we often have to find a command that we entered some time ago. There are two ways to pick an old command without pressing the up arrow key too many times. If you like to use the mouse, combine the command and the command together, as shown below:

history | grep git

The above command shows all commands you entered earlier, so you can copy and run again. If you don’t like to use the mouse, do a reverse search. Press and enter a part of the command. Next, press until you find the command you need.

We often have to create new files, remove folders, and copy files during programming tasks. You don’t need to leave your terminal to do these file operations. Even though the built-in GUI file browser gives a better visual overview of files, it reduces the productivity of file operations because we have to do a lot of clicks and mouse movements. Your favorite code editor may offer some features to perform file operations, but you still have to work with a GUI.

Practice using the , , , , and commands instead of using the GUI file manager application. But, be careful with commands like because it won’t ask you to confirm once you use the force option.

Also, we can perform file searches and text pattern searches faster on the terminal application. For example, the following command shows all places where you included the header file:

grep -rn "#include <unistd.h>"

If these commands look a bit longer, you can always define these commands as functions in the file, as shown below:

function sp() {
grep -rn "[email protected]"
sp "#include <unistd.h>" # will do the same!

But, we have to use use the GUI file manager sometimes. Then enter the following command to open it without leaving your terminal application:

xdg-open ~/folder # Linux
open ~/folder # macOS

When we have to enter repetitive commands on the terminal every day, we typically write a shell script with all commands. Most programmers use Bash to write their automation scripts. Bash is great for shell scripting, but it’s not suitable to build all automation scripts. What if you need to create a file with a multiline string template? Bash is a command language. Therefore, it doesn’t offer all developer-friendly syntax and features to write modern automation scripts.

Try to pick an alternative programming language for writing modern automation scripts. JavaScript and Python are great alternatives. Every Unix-like operating system comes with a pre-installed version of Python. Also, many programmers have installed Nodejs on their computers. Therefore, you can write portable automation scripts similar to Bash with both Python and JavaScript. You can write automation scripts for CI/CD servers also with these alternatives.

You can use zx or shelljs to make your JavaScript utility scripts look similar to Bash. On the other hand, shellpy helps you to write Bash-like Python scripts. The following story explains further about shell scripting languages:

A specific terminal command typically does one action. For example, the command displays the provided file’s lines in reverse order. Also, some external commands come with subcommands. Almost all commands typically support several command-line options to control the internal behaviors. Sometimes, these command-line options help us to speed up our work by skipping additional commands.

For example, you can use the following command to create multiple child directories at once:

mkdir -p ./src/core/filesystem

Shell interpreters support two ways to run a sequence of commands within one line: separating commands via semicolons () or two ampersands () and with pipelines. We can indeed create one-liners to boost our productivity instead of typing one command at a time. The following one-liner makes a set of directories recursively and sets the current working directory as the most recently created directory:

mkdir -p ./src/core/filesystem && cd $_

Sometimes, we can do things faster by sending a particular command’s output as another command’s command-line arguments (not as the standard input via a pipeline). Look at the following example that removes all Docker containers at once:

docker rm -f $(docker ps -a -q)

Moreover, we can build arrays and use them as command-line arguments instantly. The following one-liner will make five Python scripts with a sequential naming:

touch script_{1..5}.py

Try to make your own command combinations to do things at once without entering one command at a time all the time.

Source link

Leave a reply

Please enter your comment!
Please enter your name here