Ubuntu being an open source operating system is quite popular among developers. And with time it is gaining popularity among general users. Regardless, Ubuntu is really great platform which provides much freedom. Whether you are a developer or a normal user. There are certain shortcuts to doing things in Ubuntu. Here, I present you 10 of the Ubuntu tips and tricks that I currently use.
NOTE: Everything in normal paragraph in between [ ]
brackets is code snippet.
Quick Navigation
1. Alias for long commands
Aliases come really very handy in Linux bases OSes. They provide too much productivity benefit. Here I will show you how to create an alias. And how it is used.
I will be using a long git command git [ diff –name-only ]. This is one of many commands I use on a regular basis. or [ git add . && git commit -m “my commit message” && git push ] that adds, commits and pushes the code remote server.
Navigate to your home folder by typing in the following command.
cd ~ # navigate to your home folder
Code language: PHP (php)
Next we will open the .bash_aliases
in nano
with the command below.
nano .bash_aliases # open the file in nano text editor
Code language: PHP (php)
Next we will be adding example command here.
alias gdno="git diff --name-only"
alias refresh="source ~/.bash_rc"
Code language: JavaScript (javascript)
The code above are two separate things. [ gdno ] will execute the command [ git diff –name-only ] in the current directory. But what about the command just below that ? Well you’ll see it in a bit. Next we will need to refresh the current terminal with the command below.
source ~/.bash_rc
See, the above command is mapped to [ refresh ]. It is because the terminal must load the functions and variables from .bash_rc. And since the .bash_aliases is included in .bash_rc. We will be reloading the main configuration file for terminal. From next time you can just type [refresh
] and your terminal will autorefresh.
TECHENUM
2. Show git branch name in terminal
If you don’t already use something like zsh. You cannot see the active branch name by default. This is such an annoyance for bash users. However you can do this without changing your terminal. This is such a handy among other Ubuntu tips and tricks.
PART 1:
Open up your .bashrc
file in nano or your favorite editor. It doesn’t matter what you pick. Navigate to the end of the file and append the function below.
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ( \1 )/'
}
Code language: JavaScript (javascript)
Lets break down what is in the code above. At first this might look overwhelming but it’s not that hard to see what the function is doing. All the errors from command [ git branch
] is redirected to the [ /dev/null
] by [ 2>
]. Here is a nice explanation about /dev/null
on medium read more here.
Now the |
just pipes the command for whatever’s on the right side of it. In our case [ sed ]
. [ sed ] is nothing but another utility provided in the Ubuntu distribution itself. It’s full form in stream editor. It transforms and parses the text that is provided to it. In our case the piping will pass the result from [ git branch
] and extract the branch name with the pattern provided '/^[^]/d' -e 's/ (.*)/ ( \1 )/'
.
PART 2:
After that we will be adding another command provided below. Don’t worry I’ll break it down just to fulfill your ever questioning mind’s query.
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[00;32m\]\$(git_branch) \[\033[00m\]\$ "
Code language: JavaScript (javascript)
The above command is a rather long one. And it is very overwhelming at first. But I’m breaking it down part by part.
[ \[\e]0;\u@\h: \w\a]
]
Let use evaluate what’s happening here. The \[\e]0;
is escape character the one you see in your keyboard and 0 that follows is the command that tells the bash to set anything to title and icon both.
The \u@\h
part is just [ $USER:$HOSTNAME ]
which will be parsed by the terminal program. \w
is nothing but the current working directory and \a
is just the bell character, which ends sequence here. Press CTRL + G
in your terminal and you’ll know, make sure to bump up your speakers. But the reason it’s there surprises me too. Maybe I need to dig in a little further.
${debian_chroot:+($debian_chroot)}
is nothing nasty. It is simply calling out the contents contained in /etc/debian_chroot
. The code can be found at the file /etc/bash.bashrc
, if you want to dig in be my guest.
And we have come to [\033[01;32m]
this. It simply is coloring the text in terminal with desired color. With \$
shows either # (for root)
or $(for other users)
. And finally we have exported it as PS1
variable. Now PS1
is just the primary prompt for terminal which is what you see by default in terminal.
Save the file and refresh the configuration by typing
source ~/.bash_rc
TIP: For color codes and more explanation read more here.
Also Read: Learning about encrypting files & folders on Ubuntu
TECHENUM
3. Shortcuts for terminal things
While you’re in a terminal which command do you use most ? Let me guess it’s apparently clear
. That’s what I do at least. But for so long I always used the common old clear + ENTER
. It’s time to understand with few other shortcuts for it.
ctrl + l
: To clear the screen; same as clear
commandctrl + g
: To get default bell soundctrl + j
: To go to new line; same as pressing enterctrl + d
: This will close the current terminal; same as exit
shift + ins
: Pastes text from clipboard; same as ctrl + v
There you go keep getting productive with these commands. Pick out the ones you like and make a habit of using them.
4. Open current directory in files from terminal
How many times you have gone back and forth just to open one directory in the file manager. Let me tell you how many times I did that; countless. Here I will provide you another Ubuntu tips and tricks to open up the current directory in files.
nautilus . & disown
This command will open up current directory in file manager for you. The & disown
will detach the process from current terminal. Meaning the window will not close even if you close the terminal window.
5. Find the actual URL from shortened URL
When on the internet we come across a shortened URL multiple times. Those bit.ly URL mostly. We cannot really know what is behind that masked URL unless we click it. But there is a trick to unmask the URL right from your terminal.
curl -kIs -w "%{redirect_url}\n" -o /dev/null <url_here>
Code language: JavaScript (javascript)
Replace the <url_here> portion with your shortened URL. This requires you to be connected to internet. Simple explanation about how this works is. curl
follows the redirects right from terminal until it gets 200 OK
. You have to understand HTTP protocol to grasp a understanding on HTTP Status codes.
Conclusion
These are some of my productivity hacks which I use on a regular basis. You can just implement some or all things mentioned here for your own use case. Happy Ubuntu(ing) and keep learning folks.
Also Read: What is Denial of Service (DoS) and how does it work ?
TECHENUM
Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.