The terminal is the preferred choice of working space for many web developers these days. And why not? With the right set of commands, the terminal can definitely get things done faster. However, beginners make certain time-consuming mistakes. Time is money and in this post, we’ll focus on a few areas that can help you complete your actions quickly and efficiently.
1. Use Tab Completion
This is perhaps the most common mistake I have seen beginners make. Suppose you need to open a file named “
my_long_file_name.py” in a text editor. You type the whole name of the file and hit enter — only to realize you misspelled it. So you start typing it again. That’s not how it should work.
Once you have typed 2-3 characters, hit tab and the rest of the name would be competed for you. If you have similar file names (like “
my_long_file_name.py” and “
my_long_file_name.py.bkp”), the common part would be completed for you and you would be shown both options, as shown below.
The same applies for commands too. Type in part of the command and hit tab to complete the command. If there are similar commands, you would be shown the option just like in case of the files.
There is no downsides of over tabbing, so go ahead and start using it frequently, if you haven’t already.
2. Use !! to prefix to the last command
In a previous article, I mentioned
sudo !! runs the previous command with administrator privileges. What I didn’t mention is that all
!! does is replace itself with the previous command.
To perform the same task, hit the ‘up’ arrow, followed by the
home key (or
<-) to reach the beginning of the command. It really comes down to your personal choice.
!:n selects the nth argument of the last command (but not the rest of the command), whereas
!$ selects the last argument of the last command.
3. Cut and paste lines
If you are typing something long and realized it needs to be put somewhere else, you could cut whatever is there in your current line by pressing
U. You could then paste it somewhere else by pressing
Y. They are useful shortcuts, if you don’t want to get messy by selecting text to perform these actions.
4. Clear screen
If you want to clear the output of the previous commands from the terminal’s view, you can run —
There is a shortcut for this, however. Just hit
L and all the text above your current line is cleared. This shortcut can be used when you are halfway writing a command, without losing it, unlike the
cd to home directory
No matter what your working directory is (run
pwd to get your absolute working directory), you can switch back to your home directory (
/home/username/) any time by running the following —
Alternately, you can run the following too:
Which brings us to your next tip…
6. Relative path to your home directory
You probably know that your home directory is
/home/username/, but while working in the terminal, you can use
~ as a relative path to your home directory. For instance:
Here’s a look at some other examples.
vi ~/.bash_history cd ~/git.slides/ scp -r ./ email@example.com:~/
The commands listed above are equivalent to the following:
vi /home/username/.bash_history cd /home/username/git.slides/ scp -r ./ firstname.lastname@example.org:/home/donny/
7. Using an alias
I’m sure you must have run long commands that are essential to your work. Commands like the following:
python manage.py runserver --settings=project.development
To avoid typing such long commands every time you need them, you can use an alias.
alias start-dj="python manage.py runserver --settings=project.development"
Next time you run
start-dj, what actually gets executed is this —
python manage.py runserver --settings=project.development.
8. Use the
Using an alias is great, but what’s sad is that they get removed from the memory once you close the terminal. To save them, you need to store them in your
.bashrc file. By default, it is stored in your home directory (
~/.bashrc). Just paste the alias commands in the
.bashrc file and restart the terminal for them to take effect.
If you don’t want to restart the terminal, save the
.bashrc file and run the following for the new aliases to take effect.
Developers can get pretty innovative when customizing commands using aliases. There is a wide variety of custom commands to make your work easier, but I wouldn’t mention any of them because they vary greatly from person to person. Perhaps, you will develop your own shortcuts in due time.
9. Create a file on the go
You can dynamically create a file and with the contents as the output of a function pretty easily.
[command] > sample_file
sample_file is created and is replaced if it already exists (without any confirmation). Therefore, you must be careful while doing this.
This command is great for taking database dumps. For example:
mysqldump -u [username] -p -D [database] > db_dump
A consequence of this is that you can easily create empty files by giving an empty command (which of course, has an empty output).
The command above will create an empty file with the name
10. Use file contents as arguments to a command
Just like we can store the output of a command into a file, we can use the contents of a file as the input for a command. For example, the following will run the command with the inputs of
sample_file as arguments.
[command] < file
A use case of this is to restore an SQL dump.
mysql -u [username] -p -D [database_name] < db_dump
This will first login into MySQL and then run each line in
db_dump in the MySQL shell.
Now that you have completed your work on the terminal, you can type
exit and hit enter, or you could just press
D to do the same — your choice.
With this, we come to the end of our list of time savers. Do you have any interesting UNIX tips or aliases you want to share? Feel free to comment below.
Wrapping Your Head Around Python
Introduction to Swift
Jump Start Git
Level Up Your Web Apps With Go
Jump Start MySQL
Jump Start Git
- 1 Phpseclib: Securely Communicating with Remote Servers via PHP
- 2 Extend the Quick Edit Actions in the WordPress Dashboard
- 3 Quick Tip - Installing the Ionic Framework for HTML5 Apps
- 4 Working with Data in React: Properties & State
- 5 Picking the Brains of Your Customers with Microsoft's Text Analytics