5

5 Unix Terminal Tips To Boost Your Coding Speed

 2 years ago
source link: https://betterprogramming.pub/5-unix-terminal-tips-to-boost-your-coding-speed-c85f880606ed
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

5 Unix Terminal Tips To Boost Your Coding Speed

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 mkdir to create a new directory, the terminal program executes the /usr/bin/mkdir process with input arguments via a command-line interpreter like Bash. Next, the mkdir binary creates a new directory with the mkdir 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.

Use Background Tasks Effectively

We typically enter short-time commands on the terminal. For example, the git status 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 .bashrc as shown below:

function bgt() {
"$@" &> /dev/null &
}
....
....bgt ./download.sh # will do the same!

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

Learn To Retrieve and Enter Commands Faster

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 Ctrl + A and Ctrl + E 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 grep command and the history command together, as shown below:

history | grep git

The above command shows all git 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 Ctrl + R and enter a part of the command. Next, press Ctrl + R until you find the command you need.

Try To Do All File Operations Within the Terminal

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 touch, cp, mv, rm, andmkdir commands instead of using the GUI file manager application. But, be careful with commands like rm 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 theunistd.h header file:

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

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

function sp() {
grep -rn "$@"
}
....
....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

Use JavaScript or Python To Write Modern Automation Scripts

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:

Always Check Command Options and Possible Combinations

A specific terminal command typically does one action. For example, the tac 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK