You are not logged in.

#1 2013-02-01 20:08:40

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Stupid shell tricks

This is the thread for silly little tricks that make your command shell more usable.

Example: abusing the PROMPT_COMMAND environment variable, which can execute a command every time you enter something into the shell. Like, for instance, with the nice one-liner I discovered through Google, and modified to my own needs:

export PROMPT_COMMAND='[[ $curdir != $PWD ]] && ls -a --color=auto -F; curdir=$PWD'

which prints the contents (with color and glyphs) of a directory as soon as you enter it, then shuts up until you enter another directory. IOW it turns a standard bash prompt into a very simple file manager.

What are yours?

Edit: the above can also be written, less elegantly but perhaps more readably,

export PROMPT_COMMAND='if [[ $curdir != $PWD ]]; then ls -a --color=auto -F; curdir=$PWD; fi'

Last edited by Gullible Jones (2013-02-01 20:13:04)

Offline

#2 2013-02-01 23:24:54

Stebalien
Member
Registered: 2010-04-27
Posts: 1,227
Website

Re: Stupid shell tricks

If you need to do things like this you might want to look into zsh and hook functions.


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com

Offline

#3 2013-02-02 02:25:03

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,058

Re: Stupid shell tricks

To accomplish the same thing in zsh, I just setup this function:

function chpwd { ls }

Also to have the current command in my terminal's title, I use:

case $TERM in
	*xterm*|*rxvt*|(dt|k|E|a)term)
		precmd() { print -Pn "\e]0;%~\a" }
		preexec () { print -Pn "\e]0;$1\a" }
		;;
	screen*)
		preexec () { print -Pn "\e\"$1\e\134" }
		;;
esac

Last edited by anonymous_user (2013-02-02 02:25:23)

Offline

#4 2013-02-02 14:07:00

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Stupid shell tricks

Thank you for the Zsh version, anonymous_user.

Offline

#5 2013-02-06 16:12:00

aesiris
Member
Registered: 2012-02-25
Posts: 94

Re: Stupid shell tricks

I liked the suggestion, but when there are too many files it is annoying.
So I changed it to:

PROMPT_COMMAND='[[ $curdir != $PWD ]] && curdir=$PWD && (($( find . -maxdepth 1 ! -iname .\* -printf "0" | wc -c )<100)) && ls --color=auto -F'

Offline

#6 2013-02-07 03:25:55

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Stupid shell tricks

Hmm. Isn't find kind of slow for that? Or would it be slower to call ls twice?

Offline

#7 2013-02-07 09:15:06

aesiris
Member
Registered: 2012-02-25
Posts: 94

Re: Stupid shell tricks

Gullible Jones wrote:

Hmm. Isn't find kind of slow for that? Or would it be slower to call ls twice?

I tested it before chosing.
It depends on the number of files: on my system ls is faster when they there less than a few hundreds.

Other alternative to consider

{ cwdfiles=(*); ls "${cwdfiles[@]:0:100}" -d --color=auto; }

Consider also the cost of printing.
For example printing the output of "ls /usr/lib" on my console takes many seconds (and imagine from a ssh session!), hence the need to count them before printing.

Offline

Board footer

Powered by FluxBB