DEV Community

Cover image for What are your command line tips?
Ben Halpern
Ben Halpern Subscriber

Posted on

What are your command line tips?

Let's here them!

Latest comments (75)

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
 
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
 
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
 
hoffmann profile image
Peter Hoffmann

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

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
 
sereneinserenade profile image
Jeet Mandaliya

github.com/nvbn/thefuck

this is the best one I have.

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
 
mediocredevops profile image
Leon Nunes

vim with fzf + rg is a godsend

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