fbpx

Command Line in Coding: Get Fast, Become Amazing

Coding is an ongoing learning process. It’s never too late to get efficient! Navigate your terminal like a productivity wizard. How do you write great code?

By being efficient. If you want to create something extraordinary, you’ll have to discharge the time drops that slow you down. With just several tricks, you can speed up your work and focus on what matters.

Tricks in Coding

Move Around In Text: Coding

You believed your terminal was all concerning keystrokes? You can alt-click in the core of a line in your terminal. Admittedly, it’s a bit clunky, but it’ll be to influence your friends.

What will conserve you tons of time, though, are keyboard shortcuts. To get excited, we suggest you begin utilizing the following:

  • Hop to the start of a line with ctrl+a.
  • Hop to the end of a string with ctrl+e.
  • Delete from the beginning of the line until the cursor with ctrl+u.
  • Eliminate everything from the cursor to the end of the line with ctrl+k.

You can adopt other shortcuts using the complete list of Apple’s keyboard shortcuts. It serves notable well on a Linux command line, too. Of course, you can’t get all keyboard shortcuts at once. It is suggested to commence with a couple of them and then move on to the next couple once you’ve mastered the first.

Find Files By Name: Coding

Assume you’re exploring for the file foo.txt, but you have no idea where you’ve set it. Then from your home directory, type: find . -name foo.txt

Here, the . holds for the current working directory, and you designate the file name with the alternative -name. You can also apply wildcards. For example, this command will replace all files in txt format: find . -name *.txt

Obtain The Most Of Your History: Coding

Of course, you don’t require to type the same commands over and over again. Then, of course, there’s the tab completion — begin typing a command, then score tab to auto-complete it. But what if you need to access your last commands? There are a few alternatives:

  • The up and down arrows allow you to navigate through your modern history.
  • If you need to re-execute your previous command, type !!.
  • Suppose you need to re-execute the last command beginning with foo, type !foo. 
  • If you need to access the argument of the preceding command, you can apply !$. 

Deal With Lots Of Output: Coding 

Sometimes you’ll desire to grep for something, but the output is too long to be printed on the screen. That’s wherever piping with the symbol | comes in handy. You might require to sort through the output and cherry-pick the data that intrigue you. Then you can type:

  • grep -r bar dirfoo | less

This will display the output in a more absorbable way. You can afterwards take note of the files you want and close the display by typing q. It will give your command-line interface uncluttered.

Use Your Environment Efficiently: Coding

You’ve already confronted a few background variables — PS1, HISTSIZE and HISTFILESIZE. Generally, these variables are formulated in CAPITAL LETTERS that define the fundamental features of the system. You can get a comprehensive inventory of them with the set command. Another part is SHELLOPTS. It records all the programs that are installed to ‘on’ upon startup of your terminal session. 

Search For Files By Content: Coding 

  • Say you desire to hunt for all circumstances of the bar in foo.txt. Then grep is the friend: grep bar foo.txt
  • If you want to search multiple files, you can add them like this: grep bar foo.txt foo2.txt foo3.txt
  • And if you want to s arch for a bar in all files of the directory dirfoo, you can use the recursive mode: grep -r bar dirfoo

Pimp Your Prompt: Coding 

Your command-line prompt is the first thing you’ll see when you start working and probably the last thing you look at before leaving. So it executes to tailor it to your preferences. When you are a minimalist, so you can only include the current directory in your prompt. Therefore, in your ~/.bash_profile, you’ve specified: PS1=’ [\W]$’

Other popular options are:

  • A timestamp helps you trace back your work. Add \@ in your file.
  • Adding your username and hostname makes sense if you’re operating on a remote server. Add \u and / or \h.
  • You could attach the shell and the shell version if it’s appropriate for your work. Append \s and / or \v to your file.
  • A dollar signs $ usually marks the end of the prompt. It could help when you generate lots of output (or error messages…) and need to see where the program started.

Profit From Shell Options: Coding 

You can customize your shell in several steps with shell options. To display all options, run the following commands in your terminal:

  • bash -O
  • bash -o

The option -O refers to opportunities specific to the bash shell, while -o refers to all other options.

Conclusion

You’ve learnt how to customize your terminal with aliases, the prompt, environment variables and shell options. You can now access its history, sift through vast amounts of files and navigate to the meaningful parts.