DEV Community

Cover image for What are your command line tips?

What are your command line tips?

Ben Halpern on April 19, 2022

Let's here them!

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Oh my, I love this topic! Here comes a wall of text:

Using !! redo the last command, which is specially useful for those times you forgot to run that big annoying command as sudo, you can just run sudo !! this time.

Also, if you just cd without arguments, you end in your home. Sometimes thats useful.

On the same theme of cd, you can cd - to go to last directory. I love this! Also works with git checkout - to go to the last branch!

Another one is that if you use bash or zsh (and probably fish?) in emacs mode, which is the default, there are some commands that are very helpful:

  • Control + w Deletes one word behind, like control + backspace on some editors
  • Control + k Deletes everything from cursor to the end of the line. Maybe only the start of your command was right and you don't want to retype a bunch of things.
  • Control + u Deletes everything from cursor to the start of the line. Maybe only the end of your command was right.
  • Control + y Paste the last thing you deleted using the above commands. A god send.

Honorable mentions:

  • Control + e and Control + a go to the end of the line/start of the line, respectively
  • Control + p is the same as up arrow, useful if you think the arrow keys are far away
  • Control + n is the same as down arrow. Ditto above
  • Control + m is the same as hitting enter
  • `Control + i is the same as tab (completion)

I particularly use control + p a lot!

And as a side note, if you don't want to go sudo !! like I suggested initally, you can always go up in history (control+p or up arrow, you name it), cut everything with control+u, write sudo and then paste it with control+y. Really good commands here!

And you can always customize what key presses do with bind in bash and bindkey in zsh.

If you are on bash, you can see all commands with bind -P and bind -p. To set different things, you can run (or put in config file, eg, .bashrc) something like bind '"\C-f":clear-screen'.

If on zsh though, just bindkey is enough to list them, and bindkey "^F" clear-screen to remap.

I personally use control + f as clear screen for so long, that when I'm at a computer that does not have it I feel lost initially, which is arguably a downside to customization of hotkeys, specially for commands that already have a simple command, like clear screen. Maybe you should not use the mapping I do! 😅

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

!! is nice, but it's really only the beginning. If you force yourself a bit to use more complex expansions even when they slow you down at first, you'll quickly end up making your life infinitely easier with things like !?vim?:gs/foo/bar or mv some_long_file_name.jpeg !#:$:r.jpg (although that last one could also be done easily with rename "s/jpeg$/jpg/" some_long_file_name.jpeg, of course)

Collapse
 
lucassperez profile image
Lucas Perez

Even simple things like mv file.{txt,md} already feels nice to do.

rename is a good program, by the way!

Collapse
 
lucassperez profile image
Lucas Perez

But the most game changing learning was on how to do some shell scripting. How to use grep, regexes, sed, cut, cat and even for loops. Simply a whole new dimension opens up to you, and it's all in your hands.

Collapse
 
leob profile image
leob

Add awk to that ...

Collapse
 
arjunsahlot profile image
Arjun

control + l is a native hotkey for clearing screen. Maybe you would want to switch to that?

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Yes, but when I started using tmux, I remapped control + l to something else (switch to right pane). That's when I remapped control + f. Pretty much a sequence of changing native hotkeys that just pile up for me when I'm not with my computer... 😅

Customizing things sure is nice, but having standards are probably nicer. We should always proceed with care when customizing!

Collapse
 
nandhae profile image
Nandhagopal Ezhilmaran

Amazing write up!! You have listed down all my favourites and few I didn’t know before. Thanks.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I always forget how to bind keys, so I need to fallback to Ctrl+R, on a VPS or un-setup machines.

Collapse
 
afheisleycook profile image
technoshy

I tried selfish

Collapse
 
pedroifgonzalez profile image
Pedro Iván Fernández González

Thanks a lot

Collapse
 
ben profile image
Ben Halpern
Collapse
 
waylonwalker profile image
Waylon Walker

Tack on fzf and you have gold.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Seconded on fzf, only external CLI utility that I will recommend

Collapse
 
pandademic profile image
Pandademic

what? fzf is the gold!

(joke)

Collapse
 
maxxon profile image
Ma-XX-oN

Using Ctrl+O after going back in the history (either with arrow keys or Ctrl+R) will populate the next prompt with the next item in the history. Great if you have multiple commands you want to execute one after the other repeatedly.

Collapse
 
leob profile image
leob • Edited

Yep that would be my #1 too - control-R all the way! Oh and I use cd - all the time ...

Collapse
 
waylonwalker profile image
Waylon Walker

If you are going to take the time to setup your command line real nice, then you need a dotfiles repo, if you have a dotfiles repo you need to stow.

I always thought the stow docs jump right into the weeds and make it sound very complicated, but It's pretty simple if you worry less about the implementation.

Collapse
 
lvjian700 profile image
Jian Lyu • Edited

TL;DR. The first step of being master of any command:

tldr.sh/

➜  ~ tldr curl
curl

Transfers data from or to a server.
Supports most protocols, including HTTP, FTP, and POP3.
More information: <https://curl.se>.

- Download the contents of a URL to a file:
    curl http://example.com --output filename

- Download a file, saving the output under the filename indicated by the URL:
    curl --remote-name http://example.com/filename

- Download a file, following location redirects, and automatically continuing (resuming) a previous file transfer and return an error on server error:
    curl --fail --remote-name --location --continue-at - http://example.com/filename

- Send form-encoded data (POST request of type `application/x-www-form-urlencoded`). Use `--data @file_name` or `--data @'-'` to read from STDIN:
    curl --data 'name=bob' http://example.com/form

- Send a request with an extra header, using a custom HTTP method:
    curl --header 'X-My-Header: 123' --request PUT http://example.com

- Send data in JSON format, specifying the appropriate content-type header:
    curl --data '{"name":"bob"}' --header 'Content-Type: application/json' http://example.com/users/1234

- Pass a username and password for server authentication:
    curl --user myusername:mypassword http://example.com

- Pass client certificate and key for a resource, skipping certificate validation:
    curl --cert client.pem --key key.pem --insecure https://example.com
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tqbit profile image
tq-bit • Edited

if, during development, you

  • must type a command more than twice
  • type several commands that look similar
  • work in a team of more than 2 devs

you should use bash or powershell to automate whatever you're doing.

Computers are way better than keeping stuff in mind than people. I'm writing scripts for every non-throwaway project by now

Latest one for the appwrite hackathon goes

#!/bin/zsh
COMMAND=$1

startAppwrite() {
  docker run --rm -it \
    --volume /var/run/docker.sock:/var/run/docker.sock \
    --volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
    --entrypoint="install" \
    appwrite/appwrite:0.13.4
}

startFrontend() {
  docker-compose up --build
}

install() {
  echo "Installing appwrite cli ..."
  npm install -g appwrite-cli

  echo "Logging in to API ..."
  appwrite login

  echo "Initializing Project"
  appwrite init --all
}

# Commando functions
initServices() {
  echo "Setting up appwrite for the first time"
  startAppwrite
  install
  startFrontend
}

stopServices() {
  echo "Stopping iCuisine services"
  docker-compose down
  docker-compose --file appwrite/docker-compose.yml down
}

# Command execution
case "$COMMAND" in

up)
  initServices
  ;;
down)
  stopServices
  ;;
[iI]*)
  install
  ;;
*)
  echo "No command recognized. "
  echo "Possible commands: [up, down, login, i(nstall)]"
  ;;
esac
Enter fullscreen mode Exit fullscreen mode

This little bit of code saves me at least 1 minute whenever I want to bring up or down my dev server. Took like 5 to write. And I have to memorise only a few commands instead of several. Totally worth it

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Yep. I even write scripts for the simplest of tasks now:

#!/bin/sh
fantasy_test --some_param || exit
made_up_lint || exit
fake_package install project.fake --parameter
Enter fullscreen mode Exit fullscreen mode

These micro-scripts can seem like a waste of time while writing them, but after calling them 10 or 20 times, you've already saved more time than it took to set them up.

Collapse
 
hoffmann profile image
Peter Hoffmann

set -e

Will also exit a skript if a command inside fails

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

No it won't. Look at the script again ;)

Collapse
 
gbhorwood profile image
grant horwood
Collapse
 
equiman profile image
Camilo Martinez • Edited

I have a series of 7th articles with a lot of them for ZSH:
dev.to/equiman/series/11407

Another to config zsh + power level 10 on macOS:

But the most useful for me is config the fingerprint reader to use on the terminal when asking for a password:

Collapse
 
cerchie profile image
Lucia Cerchie

git diff more of a git tip but it saves me time and trouble seeing if/what differences I have between my branch and main.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾 • Edited

Only 1 thing,
Learn a liitle bit of shell scripting, it changes how one works with a terminal/CLI
Knowing a bunch of things and putting them together to improve your workflow as developer everyone should strive for.

Collapse
 
yechielk profile image
Yechiel Kalmenson
Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

One of my recently acquired command line tip, create this function

? () { echo "$*" | bc ; }
Enter fullscreen mode Exit fullscreen mode

usage

$> ? 8*5
40

$> ? 2+2+9*9+(2-8)
79
Enter fullscreen mode Exit fullscreen mode

No need to create a python process or node process ( or any other interactive shell ) for quick calculation. It just takes the mathematical query and throw it to BC which then return the result.

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha • Edited

In case it isn't obvious, Question mark ? is just a name of function. You can choose anything else as long as it doesn't conflict the system.

Also bc is a mathematical tool, it follows bodmas or pemdas order of operation. So 6 ÷ 2(1+2)= is equal to 1, not 9, not 7, or any other thing you saw on facebook. I should have checked on my machine before bashing out here, My casio calculator outputs 1 because of implied multiplication taking precedence, so does what we are taught in schools in India, but apparently precedence of implied multiplication is standardized to be ignored since 1917.

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Sorry, but 6 ÷ 2(1+2) is just 9, indisputably.

Bodmas, pemdas or any other weird word is just a pitfall to trick people into mistakes, and gladly I have never heard such terms in school. Division and multiplication are performed in the order they appear, it could not be simpler. Division, in fact, is multiplication, just like subtraction is the same as addition. When you divide by 5, you are in fact multiplying by the inverse of 5. There is no reason to perform multiplication before division, since they are the same thing.

I couldn't believe BC would output 1, so I opened a terminal and ran it, and of course it showed 9 in the screen.

Thread Thread
 
abhinav1217 profile image
Abhinav Kulshreshtha

Well apparently, In India, we were taught the older method. I did some search and find this video from "mind-your-decision" and what we were taught is the older 1917 order of operation, where parentheses next to number has special exception. My old casio calculator actually outputs "1", because they have implied multiplication

I know that division and multiplication have same level of order and hence calculated left to right. Bodmas doesn't mean addition before subtraction, but it does mean division(or multiplication) happens before subtraction(or addition) and exponents(or log) happens before them, and brackets always have higher priority. The operators with same specificity are solved left-to-right.

Thanks for correcting me. I wrote it as a sarcasm on all the stupid fb posts and twitter polls that have been going around my feeds, but turns out it was a facepalm moment. "Implied multiplication" is still taught in schools in India.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Don't edit bash scripts while it is running; even when it takes forever.

In contrast, other types of scripts, like Python, can be edited for the next run.

I also have seen set -e. I wonder if it can be safely used with other kind of logics?

Collapse
 
stankukucka profile image
Stan Kukučka

To check what specific server is domain running at:

nmap somewebsitegoeshere.com --script whois-ip
Enter fullscreen mode Exit fullscreen mode
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

It's easier to just do ping somewebsitegoeshere.com; it's technically the wrong command, but easier to remember and quicker to type ;)

Collapse
 
eride profile image
Eric R.

Switch to the previous directory you were in: cd -

A similar command exists with Git and branches: git checkout - or simply gco - since git-checkout is often aliased to "gco."

Collapse
 
dumboprogrammer profile image
Tawhid

I've so many to tell like using up and down keys, using \ and &&, checking bash history with

history # show all
history | tail -5 # show last 5
history | grep text # search for text
Enter fullscreen mode Exit fullscreen mode

also using !! to redo last command

Collapse
 
julrich profile image
Jonas Ulrich

Install yourself a variant (for your distro) of github.com/rupa/z and never search for a directory ever again!

DESCRIPTION
       Tracks your most used directories, based on 'frecency'.

       After  a  short  learning  phase, z will take you to the most 'frecent'
       directory that matches ALL of the regexes given on the command line, in
       order.

       For example, z foo bar would match /foo/bar but not /bar/foo.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
curtisl profile image
Curtis Lawrence

I'll sometimes forget to start/stop my time tracker when working on multiple small tickets. I find myself using

history -f
Enter fullscreen mode Exit fullscreen mode

to see exactly when I performed specific actions (checking out a branch, pushing changes...etc) related to a task. Then I can manually update my time logs to match.

Collapse
 
jonashdown profile image
Jon Ashdown

here are a few I use
^string^replacement - changes first occurence of 'string' to 'replacement' e.g 'cat types' when you meant 'cat typos'

history | grep -i 'somecommand' - searches history for occurences of 'somecommand'

!236 - execute the 236th command in the history file

!236:p - in bash this echoes the the 236th command in the history file and makes it '!!'-able without executing it. in zsh it populates the prompt with this command

I also have the following in my bashrc, which converts each subfolder in the folder 'workspace' into a command and sets the version of node using nvm. (it can also be extended for python/ruby)
'for f in ls $HOME/workspace
do
alias $f="cd $HOME/workspace/$f;if [ -f .nvmrc ]; then nvm use --delete-prefix; fi"
done'

Collapse
 
rubiin profile image
Rubin

use z instead of cd.
alias frequently used commands or long commands.
move to the latest alternatvies of commands . for example bat instead of cat.
also use shells like zsh (personal preference) that has plugin system and easy customizability

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited
  • Ctrl + L -> Clear console (the same than typing `clear` and hitting enter.
  • Ctrl + C -> Cancel the current command (I see people closing the terminal and opening it again nowadays so, here it is 😆)
  • Arrow Up/Down -> Command history, you can browse through your command history to re-execute a command again or edit and then execute it.


It won't work in every single terminal but most Linux distros are configured the click of a middle mouse button as paste operation (clicking the mouse wheel).


When it messes up the port that you are using for development, like 1234 or 3000 as usual ones, and you get a "port already in use" complain, you can run
fuser 3000/tcp -k
Enter fullscreen mode Exit fullscreen mode

to kill the process that 'hanged' this port. Being 3000 the given port and tcp or udp as protocol options. -k is just the "kill" optional.



When you have no permissions as a result of an npm install , yarn install or similar command, probably is because you forked the project as root, so you can simply change the owner (chown) of the project directory to your current user like that:
sudo chown -R $(id -u):$(id -g) ./
Enter fullscreen mode Exit fullscreen mode
  • The ./ at the end means the current folder in context.
    You can automate some jobs using bash in you daily work, for example:
#!/bin/bash
docker-compose up &
sleep 3 &&
echo "context inside docker" &&
docker exec -it docker-image-name /bin/bash -c "cd /var/www/html/_dev/; parcel index.html";
Enter fullscreen mode Exit fullscreen mode

running this as bash will:

  1. raise the docker container (docker-compose up)
  2. wait 3 seconds (it's a fast process but you need it to complete before trying next step, otherwise you'll get an error as you can't exec things inside a non-raised container).
  3. exec the container, with terminal context on it, cd into projects root dir.
  4. launch the watch / serve / build command and show the output on the same terminal.

You can do the same but adapting it to your specific docker container specs.



Last but not least, the best advice I can provide is DON'T use the command line if you can avoid it.
I mean, we techies always try to automate things, using let's say a Git GUI like GitKraken will automate most of the job so you can focus yourself in things like... well things that you are paid for, that is developing. The same rule applies to any Gui, cli or automation that helps you avoid throwing commands in a terminal.

Meta-dev/para-dev (meaning things related to development) is nice as weekend project or self learning for fun but if you are paid by job or by time, don't waste your money (time) with that, you'll eventually copy paste commands that you don't fully understand and/or hit enter with a wrong command and mess it up, thus losing time and effort to make your environment to work properly again.

Collapse
 
ashokan profile image
Ashok Nagaraj

> <command> !$ last argument of previous command

ls -d /tmp
/tmp

❯ df -h !$
Filesystem     Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk3s5  460Gi  292Gi  147Gi    67% 3400906 1539869640    0%   /System/Volumes/Data
Enter fullscreen mode Exit fullscreen mode
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Learn your shell expansions.

Not unlike learning vim, it will make you slower at first. You'll have to look things up again and again. But it's worth the effort.

The ones I probably use the most (zsh):

  • !#:$ to repeat the last argument
  • !?search to repeat a command when I remember part of it
  • !!:s/from/to/ to repeat a command with part of it changed
  • And, last but not least, good old sudo !!
Collapse
 
liviufromendtest profile image
Liviu Lupei

Even if it sounds obvious:
Read the full error message.
That's where the answer is.
I always read only parts of it and then I wasted time searching on forums, where I would eventually find the solution.
And I was wondering "How do these people know the exact commands to fix this?".
Turns out those commands were mentioned in the full error message.

Collapse
 
munafsheikh profile image
Munaf Sheikh

The fact that you can create functions to do anything in bash is just amazing:
here are some of my favourite aliases and functions

Pipe your input into awk and pull out something common.

function col() { 
   #eg:  ls -lap | col 3 | uniq | sort
    awk "{print \$$1}"
}
Enter fullscreen mode Exit fullscreen mode

docker images
REPOSITORY                                                        TAG                     IMAGE ID       CREATED         SIZE
api-whatever                                                local                   1fc3b78969ba 
api-whatever1                                                local                   1fc3b78969bb 
api-whatever2                                                local                   1fc3b78969bc 
Enter fullscreen mode Exit fullscreen mode
-> docker images | col 3
IMAGE
1fc3b78969ba
1fc3b78969bb
1fc3b78969bc
Enter fullscreen mode Exit fullscreen mode

Simplifying groups of commands

gLRd() {
 if [ -z "$1" ]; then 
  echo "specify a branch to delete locally and remote"        
 else
  gLd $1
  gRd $1
 fi
}
Enter fullscreen mode Exit fullscreen mode

Combine this with other commands

_getCurrentGitBranch() {
 git branch &>/dev/null
 local OUTCOME=$?
 if [ $OUTCOME -eq 128 ] 
 then
  return 1
 else
  git branch | grep '*' | cut -f2 -d' ' 
 fi
}
Enter fullscreen mode Exit fullscreen mode

alias g_p='type g_p ; git push origin _getCurrentGitBranch'

f(){
    echo 'Searching for:' $1
    grep  -R $1 *
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jmplourde profile image
Jean-Michel Plourde

Automate complex command line flows with tmux + tmuxinator

It originated from an old project using VMs and Vagrant. Starting the project through 5 terminal windows took me 2-3 minutes each day. Everyday for a year, that's ~8 hours. Making everything start at the same time reduced the starting time to 10 seconds.

Collapse
 
zaerald profile image
Zaerald

I have recently written about the Vim + Command Line. Where you can efficiently fix, create, and run ad-hoc commands.

Collapse
 
julrich profile image
Jonas Ulrich

Last one, I'll stop spamming after this... I swear :D

Manage your whole terminal environment / configuration in Git with Homeshick:
github.com/andsens/homeshick

I have all of them on Github, for example, to quickly re-create my setup when needed:
https://github.com/julrich?tab=repositories&q=dotfiles&type=&language=&sort=

Collapse
 
jzombie profile image
jzombie

Up arrow.

Collapse
 
bascodes profile image
Bas Steins

I’ve had a post about some of my favorite tools here: dev.to/bascodes/14-awesome-cli-too...

And recently tweeted an update here:

Collapse
 
johnnnnnnn profile image
Johnnn

I find the git commands that I struggle with are the more advanced ones, such as merge and rebase. These commands can be difficult to understand and use correctly, so I often have to refer to documentation or online resources to make sure I'm using them correctly.
Regard: wildoftech.com/

Collapse
 
jonasbn profile image
Jonas Brømsø

For Bash: CTRL-x-e lets you edit complex command line constructs in the editor specified via $EDITOR

REF: jonasbn.github.io/til/bash/edit_co...

Collapse
 
julrich profile image
Jonas Ulrich

And as I've seen fzf already meantioned... add ripgrep to it for super fast, configurable search / grepping:
github.com/BurntSushi/ripgrep

Collapse
 
codeandclay profile image
Oliver

Make aliases for typos and confuse your colleagues when group programming.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀
git checkout -
Enter fullscreen mode Exit fullscreen mode

Toggle between the last branch and this one

Collapse
 
hoffmann profile image
Peter Hoffmann

I like <(some_command) instead of a filename e.g. for diff to compare dynamic texts

Collapse
 
saptakbhoumik profile image
SaptakBhoumik

Try out the fish shell. It will help you improve your productivity thanks to it's autocomplete and syntax highlighting feature

Collapse
 
grendel profile image
grendel

it's not POSIX compliant though

Collapse
 
saptakbhoumik profile image
SaptakBhoumik • Edited

You can add fish to your bashrc so that you can switch to bash whenever you need. Also it won't be a problem most of the time

Collapse
 
devinthemtn profile image
devinthemtn

For editing long commands, typo or other edits, I recommend using the edit in vim shortcut. mine is set to ctl+x e I'm unsure what default is atm.

Collapse
 
grendel profile image
grendel

i have vi mode set via ~/.inputrc (previously i had set -o vi in my ~/.bashrc) so if i want to edit the line in vim proper, i can just press Mod+V at any point and bam, vim opens with the command buffer in it

Collapse
 
offline profile image
Offline

[command] —help

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

history | grep insert-phrase

Helps you remember the last time you used a command

Collapse
 
mediocredevops profile image
Leon Nunes

vim with fzf + rg is a godsend

Collapse
 
elyasi2142977 profile image
H.elyasi

Just cmd!!!

Collapse
 
deepakdevanand profile image
Deepak Devanand

Bash: Alt + . to cycle through the last argument of last command.

Collapse
 
taijidude profile image
taijidude
Collapse
 
hans2103 profile image
Hans Kuijpers

Add parameter ‘—verbose’ to a lot of long lasting commands like ‘composers update’. Just to see something on the screen instead of a blinking cursor

Collapse
 
rjrobinson profile image
R.J. Robinson

install the_siliver_searcher you'll never grep again

Collapse
 
sereneinserenade profile image
Jeet Mandaliya

github.com/nvbn/thefuck

this is the best one I have.

Collapse
 
jrop profile image
Jonathan Apodaca

Use the Fish Shell and write lots of abbreviations and functions. Also, TMUX and NeoVim are some of the most powerful programs I have ever learned to use.

Collapse
 
ducaale profile image
Mohamed Dahir

Ctrl+x Ctrl+e to edit commands using an editor

Collapse
 
devsmitra profile image
Rahul Sharma

warp for auto suggest command and many other features,
warp.dev

Collapse
 
ajatkj profile image
Ankit Jain

fig.io is pretty cool and supports auto-complete for lots of CLI tools and has support for multiple terminal!

Collapse
 
dhravya profile image
Dhravya

Not a tip but i've been using starship for my terminal and it's been amazing

Collapse
 
afheisleycook profile image
technoshy

Anyone try elvish