DEV Community

Cover image for 3 Terminal Commands to Increase Your Productivity

3 Terminal Commands to Increase Your Productivity

Pankaj Gupta on December 31, 2023

Here are a few important shortcuts that help me be more productive throughout the day at work: Creating aliases for commands. Using pbcopy. Using...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️
alias gc="git commit -m $1"
Enter fullscreen mode Exit fullscreen mode

I hate this one with a passion. People who have aliases like this are almost always the same people who only ever write one-liner commit messages, no matter how simple or complex the actual commit is.

But what they lose by writing one-liners, they often make up with line length, often going way over the best practice character limit.

I really wish committing with -m was just locked for anyone who hasn't been using git for at least 5 years and knows what they're doing.

Collapse
 
viiik profile image
Eduard

Why would you need multi line commit messages? A commit message should be a name and that's it, leave the details for the PR that will eventually squash all commits into a final frozen commit (which will include all the details)

Collapse
 
cappe987 profile image
Casper

When browsing the git logs I don't want to have to find the merge commit to get details about what one commit changed. A commit should, when necessary, explain the changes and maybe the reasoning for it. It's painful when I want to find out why a change was made but the commit doesn't explain anything.

Squashing all commits into one is not good practice in my opinion. If you are going to do squashing, do it in a controlled manner with rebase. Taking 10 commits and squashing into 1 loses a ton of information if the commits were well written.

Collapse
 
goodevilgenius profile image
Dan Jones

PRs are tied to GitHub. Commit messages are agnostic to which git forge you're using. When I want to see why a change was made, I git blame the file, and look at the commit message for that change.

Squashing commits should generally be avoided because you lose a lot of information from individual commits.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I think you're a bit too fixated on your own non-standard git workflow there.

I also sometimes use a sort of throw-away git history where some commits are just "fix", that I later intend to clean up with a rebase. If you want to make that your main workflow for every branch, then sure, you can be a lot less thorough with your individual commit messages.

But the "intended", and afaik most common way of using git, is to keep commits atomic, and have their messages describe the change that was made. If the reason for a change isn't obvious from just looking at the code and maybe some extra context from previous commits (or from a later merge commit), then that should be explained in the commit message.

Putting too much information in a single commit really just makes it hard to reason about the individual parts that make up a larger change, like introducing a new feature. If all you see in your git history is "add feature X", then that's hardly better than a normal change log, and you lose all the important step-by-step information of how feature X was actually added, what preparations were necessary, etc. as well as the ability to undo individual changes if necessary.

Thread Thread
 
viiik profile image
Eduard

Yeah perhaps my workflow is uncommon. My commits are still atomic however, just at the master branch, not my working branch. And usually extra context lives in the task definition that originated the changes, which is usually linked in the PR which ultimately forma part of the master commit description.

Collapse
 
asenar profile image
Asenar

bash aliases are great, but for git sub commands I personally prefer (and recommend) to use [alias] section in .gitconfig.

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

🫣 instead of aliasing directories, use zoxide. After the first cd /this/is/batman then you can just do z batman.

🎉😎 See CLI tools you cant live without

Collapse
 
walfal profile image
Walfal

I have these ones in my zshrc, near others for Linux

alias G=’| grep --color=auto
gitcommit(){
# All element except the last one
git add "${@: 1:-1}"
# Last element
git commit -m "${@: -1:1}"
}
alias ggc=gitcommit

Adding all alias in the plugin git in zsh :)

Collapse
 
goodevilgenius profile image
Dan Jones

For a good Linux alternative to pbcopy and pbpaste, try out xclip.

grep text file.txt | xclip -i # copies into primary selection

grep text file.txt | xclip -i -sel clip # copies into clipboard

xclip -o # prints primary selection
xclip -o -sel clip # prints clipboard, like pbpaste

# highlight a selection of text, you don't need to Ctrl-c, just highlight
xclip -o > out.txt # pastes that selection into a file
Enter fullscreen mode Exit fullscreen mode

And if you're using Termux on Android, termux-clipboard-get and termux-clipboard-set give you access to the Android clipboard.

Collapse
 
moopet profile image
Ben Sinclair

Some problems here:

aliases

This doesn't do what you think it does:

alias gc="git commit -m $1"
Enter fullscreen mode Exit fullscreen mode

The $1 isn't expanded to anything in your alias. It works because your message goes at the end of the line. You can see this if you try a little test:

$ alias foo="echo BEGIN $2 $1 END"

$ foo one two three
BEGIN END one two three
Enter fullscreen mode Exit fullscreen mode

If you want to use substitution then you should be using a function or a script, not an alias.

pbcopy

You go on to say

This command is available on Mac and if you want to use it on Linux distributions, you can follow this guide.

But you don't provide any guides for Linux. If you want similar functionality to pbcopy, you're probably looking for xsel or xclip but ymmv.

reverse search

This isn't a command at all! It's a feature of the input library. I think from memory this is readline most of the time. It gives you a bunch of nice features like the reverse search you mentioned, and it will work that way in any application which uses that library. This means that you can be using something like the mysql client, or a programming language REPL and there's a reasonable chance you have the same super powers there too.

Collapse
 
mhubbardvect profile image
mhubbard-vect

With ZSH you can put all of your custom aliases in a custom file. Default location is ~/.oh-my-zsh/custom.

I use ~/.oh-my-zsh/custom/zsh-aliases

I preface my aliases with my initials and a - , mw- in my case.

Then I can type mw- [tab] to see all of my aliases.

I also created an alias that opens the custom alias file.

As mentioned above ZSH includes a lot of aliases and you can use the "aliases" plug in to display all installed ZSH aliases

This is what my ZSH plugins looks like:
plugins=(
git
zsh-completions
zsh-autosuggestions
zsh-syntax-highlighting
history-substring-search
colored-man-pages
aliases
zsh-docker-aliases
)

github.com/ohmyzsh/ohmyzsh/tree/ma...

Collapse
 
orenc profile image
Chanan Oren

You are aware that OMZ is not required? Nothing OMZ does can't be done without it, it's all ZSH scripts. Though, yes you can source any file you want in to your .zshrc (or any shell script).

Collapse
 
orenc profile image
Chanan Oren

Instead of the cd alias, on ZSH there is a named directory feature:

hash -d shortname=~/some/long/path  # can add this line to your ~/.zshrc
# then you can:
cd ~shortname
Enter fullscreen mode Exit fullscreen mode
Collapse
 
falselight profile image
YURII DE.

Thank you for "cal" =)

Collapse
 
hellosurya1010 profile image
Surya K

Wow 😲 these alias commands are awesome 🔥🔥🔥

Collapse
 
pankajgupta221b profile image
Pankaj Gupta

Thank you so much!

Collapse
 
manvendrask profile image
Manvendra Singh

Use OhMyZsh

Collapse
 
aneshodza profile image
anes

I have a bunch of useful git shortcuts defined in my .zshrc

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Cool Stuff!!

What about fzf? Do you think it could be a good addition?

Collapse
 
hayden profile image
Hayden Rouille

Cool article - one thing I'd add is to be careful with the encryption within vim. This was removed in neovim for good reason, it's very buggy, unmaintained and doesn't do what it says it is.

Collapse
 
iwritepython profile image
sachinsfo

Those bonus ones are amazing 👍

Collapse
 
tharakamts profile image
Tharaka Sandaruwan

Thanks, really helpful!

Collapse
 
lansolo99 profile image
Stéphane CHANGARNIER

Thx, helpful ! I didn't know pbcopy. Regarding aliases, the Ohmyzsh plugin already provide a lot of my usual git commands.

Collapse
 
danielgomezrico profile image
Daniel Gomez

Nice tips, for git aliases it is better to put them inside of .gitconfig instead of bash aliases to me and you could just create on bash g as git to make it accessible, have you tried that?

Collapse
 
pankajgupta221b profile image
Pankaj Gupta

I use zshrc, and it comes with a lot of default git shortcuts, didn't have to do that.

Collapse
 
karltaylor profile image
Karl Taylor

I've been coding for over 10 years now and never thought to alias my projects 😅

Time to alias all my active ones 😈