Post your Command line tricks

Forum rules
Share your brain ;)
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Sat Sep 13, 2014 9:41 am

@wux & aspell:

Code: Select all

spl(){ echo $1 | aspell -a; }
of course users should add the fitting aspell-<LANG> packages first :)
..gnutella..

User avatar
harveyhunt
Haxxor
Posts: 125
Joined: Mon Jul 07, 2014 3:06 am
Contact:

Re: Post your Command line tricks

Unread post by harveyhunt » Sat Sep 13, 2014 11:48 pm

Here is a nice little script that I use to pull in all of the changes to my git repos. I might have 30 repos at a time in my ~/code folder, so this makes it much quicker. I found it on stack overflow or something.

Code: Select all

#!/bin/bash
 
# store the current dir
CUR_DIR=$(pwd)
 
# Let the person running the script know what's going on.
echo -e "\n\033[1mPulling in latest changes for all repositories...\033[0m\n"
 
# Find all git repositories and update it to the master latest revision
for i in $(find . -name ".git" | cut -c 3-); do
echo -e "";
echo -e "\033[33m"+$i+"\033[0m";
 
# We have to go to the .git parent directory to call the pull command
cd "$i";
cd ..;
 
# finally pull
git pull;
 
# lets get back to the CUR_DIR
cd $CUR_DIR
done
 
echo -e "\n\033[32mComplete!\033[0m\n"

User avatar
linuxbbq
Oyster-Slurper
Posts: 27
Joined: Thu Aug 28, 2014 10:03 am

Re: Post your Command line tricks

Unread post by linuxbbq » Sun Sep 14, 2014 8:51 am

Code: Select all

wget https://raw.github.com/DebianJoe/killxdots/master/kxt 
DebianJoe's killX toolkit with git housekeeping functions.
Trailing whitespace? Give me a fucking break, and save me from "experts" and "C-language lawyers."

User avatar
Alad
should take a shower
Posts: 447
Joined: Wed May 21, 2014 12:52 am

Re: Post your Command line tricks

Unread post by Alad » Sun Sep 14, 2014 11:24 am

^ Experts XD
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Post your Command line tricks

Unread post by DebianJoe » Sun Sep 28, 2014 7:12 am

Just recently started using this little one, and even though it's been sitting there all along (coreutils ftw!), I'd never actually played with it. It's a cli tool that adds line numbers from stdin to stdout. In its simplest form, it only numbers non-blank lines, but with a little bit of argument, it will use the same numbering that your favorite text editor would and number all lines.

Code: Select all

nl -b a </path/to/file/>
With creative use of nl + grep/sed/awk, when you get those annoying "line ___zsh: command not found fprtn" or whatever your particular syntax error for the day may be, you don't even have to open a text editor and search around to get a quick display of what you typed in wrong...and you can simply use inline search and replace to fix it.

For more information:

Code: Select all

nl --help
(Thought about putting this in /usr/bin...as it's technically /usr/bin/nl normally on Debian systems. I don't know, the lines between applications and command-line tricks is really blurry these days.)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

twoion
Mr. Saggy Tits
Posts: 29
Joined: Sat Aug 09, 2014 6:19 pm

Re: Post your Command line tricks

Unread post by twoion » Sun Sep 28, 2014 4:37 pm

DebianJoe wrote:

Code: Select all

nl -b a </path/to/file/>
I am using the following for a similar task:

Code: Select all

less -JN
will add line numbers and '*' highlights for lines matching the current search.

User avatar
johnraff
Sperminator
Posts: 199
Joined: Wed Oct 17, 2012 6:38 pm
Location: Japan
Contact:

Re: Post your Command line tricks

Unread post by johnraff » Mon Sep 29, 2014 10:19 am

Code: Select all

cat -b  </path/to/file/>
will do it too. Or cat -n for all lines.
All code is one.

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: Post your Command line tricks

Unread post by dkeg » Sat Dec 13, 2014 5:11 pm

handy little sed one-liner for adding a line within a file

Code: Select all

sed -i "line#i thetexttoadd" path/to/file
so adding a new wall to my .xinitrc

Code: Select all

sed -i "50i hsetroot -fill /data/walls/escalator.png" ~/.xinitrc

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Sun Dec 14, 2014 1:39 am

^ this is how editing is supposed to be :D
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: Post your Command line tricks

Unread post by dkeg » Sun Dec 14, 2014 2:05 am

yeah, I assumed I was late to the party, but was nonetheless happy about it.

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Sun Dec 14, 2014 2:16 am

Putting one and one together:

Code: Select all

cat -b ~/.xinitrc
# check at what line to insert
sed -i "50i hsetroot -fill /data/walls/escalator.png" ~/.xinitrc
This is really neat! Thanks a lot drew!
..gnutella..

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Sat Dec 27, 2014 4:51 pm

Got bored so I started randomly messing around the Ctrl key combos on the commandline and found these:

Ctrl + - or Ctrl + I - Sort of undo (?)
Ctrl + W - Refresh line ( not equivalent to Ctrl + L which is for clearscreen )
Ctrl + P / Ctrl + N - Search history backward / forward ( Same as Up / Down cursor keys )

Found these on /r/commandline:

You can make readline (and bash) much more user friendly by adding a few options to ~/.inputrc

Do not Forget to add this as said in the comments otherwise ctrl+cursor / home / end keys won't work.

Here's my ~/.inputrc ( commented out the single tab completion )

There are some good stuff in the top voted posts at /r/commandline.
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
ivanovnegro
Minister of Truth
Posts: 5449
Joined: Wed Oct 17, 2012 11:12 pm

Re: Post your Command line tricks

Unread post by ivanovnegro » Sat Dec 27, 2014 5:28 pm

Cool, some of them are new to me or better said I forget them easily.

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Sat Dec 27, 2014 5:34 pm

^ Happens to me all the time specially with multiple window / buffer switching keybinds in different applications ( WM, tmux, chromium, text editor,etc ).
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: Post your Command line tricks

Unread post by dkeg » Sat Dec 27, 2014 5:41 pm

nice stark. I thought I had posted some inputrc stuff here, but couldn't find it. If a vim lover, you can have vi-keybindings in your terminal.

Code: Select all

## be like vim
set editing-mode vi
set keymap vi-command

## quick command repeat (while in ESC mode)                     
"p": "i !!*\r"  

Work hard; Complain less

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Sat Dec 27, 2014 6:23 pm

^ Good one. On searching inputrc i found a post by john on page 6 of this topic but i couldn't find anything else :(
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Wed Jan 07, 2015 2:48 pm

A poorly written ( and very poorly commented ) stupid bash function for editing dotfiles with tab completion. For some reason to make the tab completion work you have to call this function atleast once with or without arguments then it will work fine. I tried putting the completion function separately in /usr/share/bash-completion/completions/ but it only showed completion for the --help argument. I'm doing something ( alot of things ) terribly wrong.

Everyone is probably using a much better and a simpler solution but I would still like your feedback and how to improve this poorly written bash function.

Here's the link:
curl -s http://ix.io/fE5 > dofi

Here's how ugly it looks:

Code: Select all

dofi()
{

# Which text editor do you Prefer ? 
deditor=vim

# Where are your Dotfiles ?

dfile1="$HOME/.vimrc"
dfile2="$HOME/.bashrc"
dfile3="$HOME/.bash_profile"
dfile4="$HOME/.bash_aliases"
dfile5="$HOME/.bash_functions"
dfile6="$HOME/.Xresources"
dfile7="$HOME/.xinitrc"
dfile8="$HOME/.mpd/mpd.conf"
dfile9="$HOME/.ncmpcpp/config"
dfile10="$HOME/.config/dunst/dunstrc"
dfile11="$HOME/.config/mpv/mpv.conf"
dfile12="$HOME/.i3/config"
dfile13="$HOME/.config/sxhkd/sxhkdrc"
dfile14="$HOME/.config/compton.conf"
dfile15="$HOME/.tmux.conf"


# Define your arguments ( NOTE: The argument(s) numbering must be same as the dotfile location, i.e. variable darg1 should call dfile1. )

# Longer arguments ( Because You have Tab Completion! ) 
darg1='vim'
darg2='bash'
darg3='profile'
darg4='aliases'
darg5='functions'
darg6='xresources'
darg7='xinit'
darg8='mpd'
darg9='ncmpcpp'
darg10='dunst'
darg11='mpv'
darg12='i3'
darg13='sxhkd'
darg14='compton'
darg15='tmux'

# Shorter arguments ( What ? Why didn't you install 'bash-completion' ? )
#darg1='vi'
#darg2='bash'
#darg3='prof'
#darg4='alas'
#darg5='func'
#darg6='xres'
#darg7='xin'
#darg8='mpd'
#darg9='ncmp'
#darg10='dunst'
#darg11='mpv'
#darg12='i3'
#darg13='sxhk'
#darg14='comp'
#darg15='tmux'


# Tab Completion for dofi arguments. 
function auto-complete_dofi
{
	local OPTIONS='$darg1 $darg2 $darg3 $darg4 $darg5 $darg6 $darg7 $darg8 $darg9 $darg10 $darg11 $darg12 $darg13 $darg14 $darg15 --help'

	COMPREPLY=()

	if [[ $COMP_CWORD -ge 1 ]] ; then
		local current_word="${COMP_WORDS[COMP_CWORD]}"
		if [[ $COMP_CWORD -eq 1 ]] ; then
			COMPREPLY=( $(compgen -W "$OPTIONS" -- "$current_word") )
			return 0
		fi
	fi
}

complete -F auto-complete_dofi dofi


# Help!
function help_dofi
{
esc=""
reset="${esc}[0m"
greenf="${esc}[32m"
redf="${esc}[31m"
bold="${esc}[1m"


cat << EOF

 Usage:
	${bold}${greenf} dofi ${reset} [OPTION]

 Options:
	${bold}${redf} $darg1 ${reset}		$dfile1
	${bold}${redf} $darg2 ${reset}  	$dfile2
	${bold}${redf} $darg3 ${reset}  	$dfile3
	${bold}${redf} $darg4 ${reset}  	$dfile4
	${bold}${redf} $darg5 ${reset}  	$dfile5
	${bold}${redf} $darg6 ${reset}  	$dfile6
	${bold}${redf} $darg7 ${reset}  	$dfile7
	${bold}${redf} $darg8 ${reset}		$dfile8
	${bold}${redf} $darg9 ${reset}  	$dfile9
	${bold}${redf} $darg10 ${reset} 	$dfile10
	${bold}${redf} $darg12 ${reset}		$dfile12
	${bold}${redf} $darg13 ${reset} 	$dfile13
	${bold}${redf} $darg14 ${reset} 	$dfile14
	${bold}${redf} $darg15 ${reset}		$dfile15

	${bold}${redf} -h, --help ${reset}	Show this help.

EOF
}

case "$1" in
	"$darg1")
			$deditor $dfile1
			;;
	"$darg2")
			$deditor $dfile2
			;;
	"$darg3")
			$deditor $dfile3
			;;
	"$darg4")
			$deditor $dfile4
			;;
	"$darg5")
			$deditor $dfile5
			;;
	"$darg6")
			$deditor $dfile6
			;;
	"$darg7")
			$deditor $dfile7
			;;
	"$darg8")
			$deditor $dfile8
			;;
	"$darg9")
			$deditor $dfile9
			;;
	"$darg10")
			$deditor $dfile10
			;;
	"$darg11")
			$deditor $dfile11
			;;
	"$darg12")
			$deditor $dfile12
			;;
	"$darg13")
			$deditor $dfile13
			;;
	"$darg14")
			$deditor $dfile14
			;;
	"$darg15")
			$deditor $dfile15
			;;
	"-h")
			help_dofi
			;;
	"--help")
			help_dofi
			;;
			*)
			echo -e "${bold}${greenf}\n dofi ${reset}${bold}what ?${reset}\n Let me fetch help for you." && help_dofi
			;;
esac
}
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
elixir
Weight Watcher
Posts: 357
Joined: Fri Feb 21, 2014 8:25 am

Re: Post your Command line tricks

Unread post by elixir » Thu Jan 08, 2015 7:16 am

This is just a nice little script I wrote to delete all files of a program. It lets me choose which ones I want to keep or remove.

Code: Select all

#!/bin/bash

search=$1
pathtype=""
confirm=""

find / -name *$search* | while read line
do
        if [ -f $line ]
        then
                printf "$line ... Remove File? [y/n] "
                pathtype="file"
                read confirm </dev/tty
        elif [ -d $line ]
        then
                printf "$line ... Remove Directory? [y/n] "
                pathtype="directory"
                read confirm </dev/tty
        fi

        if [[ $confirm = "y" || $confirm = "Y" ]]
        then
                if [ $pathtype = "file" ]
                then
                        rm $line
                else
                        rm -r $line
                fi
        fi
done
Out of the corner of your eye you spot him... Shia LaBeouf.

https://www.youtube.com/watch?v=o0u4M6vppCI

User avatar
Dr_Chroot
Alfalfa
Posts: 1100
Joined: Mon Jun 09, 2014 9:49 pm
Location: among the sagebrush
Contact:

Re: Post your Command line tricks

Unread post by Dr_Chroot » Thu Jan 08, 2015 2:50 pm

^ Very nice, elixir. Thanks :) I believe that I will be using it often...
Fight internet censorship.
EFF | Tor Project | Bitcoin

"There have been times throughout American history where what is right is not the same as what is legal. Sometimes to do the right thing you have to break the law." - Edward Snowden

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Thu Jan 08, 2015 4:00 pm

@elixir Very nice indeed and Very Clean too :)
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

Post Reply