SECRET OF CSS

7 Shell Shortcuts To Speed Up Development | by Tate Galbraith | Sep, 2022


With the advent of elegant, full-service IDEs there are now fewer and fewer command-line interactions by developers. When you can install a package or manage files from a pretty UI why would you need an arsenal of outdated shell tricks? If you’ve ever run into a problem that couldn’t be solved by a fancy IDE then you’ll understand why having these tools at your disposal is of paramount importance.

In this article, we’ll discover some simple, yet incredibly effective shell shortcuts and commands. Using these tips you can troubleshoot errors quickly, run commands faster and execute searches with minimal effort.

This is one of the fastest ways you can leverage your shell history to run a command you’ve previously run before. If you struggle to remember a command you ran hundreds or even thousands of entries ago (and you’re not alone), simply scrolling through your history isn’t going to be an efficient way.

This is where the reverse-i-search comes in. Using this tool you can quickly search for things in your shell history and then execute them quickly. You won’t have to mess with grepping through your history or copying and pasting any commands.

Let’s assume I ran the following command a long time ago:

echo "foobar"

Now its been a little while and I can’t quite remember the word I used. If I wanted to leverage reverse-i-search I could press Ctrl + R and then drop into a search prompt and type foobar:

(reverse-i-search)`foobar': echo "foobar"

As you begin typing into the search field it will auto-populate with results. When the result you want appears, you simply hit enter and the command will run. Easy, fast, simple.

This is a slightly different way to leverage the shell history. When you run the normal history command, you get a list of commands you’ve run. Next to them are ID numbers. These correspond to the order each command was executed:

1  pwd
2 cd ...
3 history 3

Using these ID numbers you can re-run any command listed in history. All you need to do is prefix the number with a bang (exclamation mark) like this:

!1

When you run this command you will execute whatever command is stored in history at ID number 1. In this case it would be the pwd command.

This is a really handy way to look back through history and execute any longer commands you want to re-run. You can also get pretty clever by using this in shell scripts, too.

This is simple, but deceptively useful. If you want to re-run the last command you just executed you can simply enter:

!!

It seems like it would be easier to just hit the up arrow and then press enter, and in most cases it is. But, perhaps you want to run a new snippet and include the output of the last command in the next one. You could call !! inside of parentheses and nest commands to do other fun tricks:

echo "foobar"echo -e "the last command was $(!!)"

Using this method you can be absolutely certain you’re referencing the last executed command. No more re-typing or copying and pasting.

When you have to write a really crazy one-liner it can be a terrible pain to edit something at the beginning or the end of it. Luckily, there is also a quick way to do this.

If you want to move to the start of a command you can press:

Ctrl + a

If you want to move to the end you can press:

Ctrl + e

Both of these will save you from having to hold down an arrow key and wait forever to move back and forth within a single line.

If you’re still working with that long one-liner command, deleting small portions of it can be cumbersome. This is especially true if you need to remove everything before or after a certain point in the middle of the line.

There is a shell shortcut for this exact problem, too.

If you want to delete everything before the current position of your cursor you can press:

Ctrl + u

Likewise, if you want to delete everything after the cursor you can press:

Ctrl + k

Now you don’t have to hold down the backspace key for a small eternity to change a lengthy command.

Sometimes a command isn’t always what it seems. What you type into the console to execute can be manipulated behind the scenes by a number of different processes or configurations.

For example, you could add shell aliases for common commands in order to append your favorite arguments to them. One example of this might be appending some extra arguments to ls like this:

alias ls='ls -lah'

In this case, when you type ls and execute it, you might think you’re just running ls but you’re actually running it with hidden parameters.

If you’re suspicious of a particular command, you can quickly inspect it using the aptly named command utility:

command -V ls
ls is aliased to 'ls -lah'

When you run command using the -V flag you can see what is really being executed. With this example, command tells us that we’re actually running ls -lah instead of just ls.

Last up is a classic one-liner. This snippet will allow you to run a set of commands in a loop with a brief delay between them. This is super helpful if you need to constantly monitor a process or send some alert when something happens.

Let’s use a simple example that will run a loop, print the current date and then sleep for one second:

while true; do echo "$(date)"; sleep 1; done

This line is separated by semicolons which indicates that multiple commands or functions are to be executed:

  • The first command is while true which will start our infinite loop.
  • The next command is do echo "$(date)" which tells the shell to run this command inside the loop and print the date to the console.
  • Finally, we sleep for one second and then end the loop with done.
  • Since the loop started with the condition of true it will continue on forever until you exit the loop or hit Ctrl + c.

Inside this loop, you could replace the echo command with anything you wanted to keep running. This could be something that tails a file, checks for the existence of a file, or even does some simple arithmetic calculations. The best part is you didn’t even have to leave the shell to do any of it.



News Credit

%d bloggers like this: