Search:  
Gentoo Wiki

TIP_alias

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other


Please improve it in any way that you see fit, and remove this notice {{Cleanup}} from the article. For tips on cleaning and formatting see Cleanup process


Contents

Alias, Setting command aliases

The alias command allows you to make new shortcuts and synonyms for commonly used commands. The basic usage is:

alias newcommand='yourcommand -arguments'

If you want to start aterm according to your preferences with the command term, do something like:

alias term='aterm -ls -fg gray -bg black'

If you want a quick alias like ll for a more informative file listing:

alias ll='ls -al --color=auto'

Starting alias without any options lists the current aliases:

Code: alias
alias ls='ll'
alias ls -al --color=auto
alias term='aterm -ls -fg gray -bg black'

Use unalias to remove an alias.

unalias term

You can also make aliases for existing commands. If you want ls to show colors by default, do:

alias ls='ls --color=auto'

These aliases can be put in your login script (~/.bash_profile or ~/.profile depending on what shell you are using).

Create verbose output:

alias cp="cp -v"
alias rm="rm -v"
alias mv="mv -v"

You can also add an (UNDOCUMENTED!) progress indicator to cp and mv (need to manually patch the files for this feature to work. Look at http://forums.gentoo.org/viewtopic-t-460045.html?sid=fbc59e4c0cb42430ff5cbc7aaa506739 for help):

alias cp="cp -g"
alias mv="mv -g"
Note: The progress indicator will only show up if the file takes a certain amount of time to copy (about 4 seconds)

Note: this is pretty dangerous...:

-copy a large amount of file with cp -g so you can see the percentages

-some keyboard key can abort copying a file... but it still continue the overal copy process (pressing the up arow key i think)

=>so that can be problematic if the person using it doesn't know about it (for example in my case using a tty and dmps (the screen goes black after a moment)... so you have the reflex to press an arow key... now i do alt+Fsomething)

Or both at once:

alias cp="cp -vg"
alias mv="mv -vg"

Aliases with switches:

alias 'rm -rf'="rm -rfv"

Bypass an alias temporarily:

alias rm="rm -i" # here we create an alias to force interactive mode
\rm              # but to temporarily bypass that alias, we preceed the command with a \

Clear the screen when you logout:

alias logout="clear && logout"

Color grepping!

export GREP_COLOR=31
alias grep='grep --color=auto' # alternatively export GREP_OPTIONS='--color=auto'

Show the process name when pgrep'in:

alias pgrep="pgrep -l"

Color less and more:

alias less="less -r"
alias more="less -r"  # less is more :)

Exclude comments from config files:

alias nocomment='sed -e '\s/#.*//;/^\s*$/d'\ '
nocomment /etc/ntp.conf|less

Gentoo Linux Aliases

Emerge with distcc enabled:

alias demerge='FEATURES="distcc" PATH="/usr/lib/distcc/bin:${PATH}" emerge'

Display current $PATH variable:

alias path='echo $PATH'

Other alias-ideas for speedy typing:

alias em='LINGUAS="en ru de fr ja zh_CN" emerge'
alias es='em -s'
alias eS='em -S'
alias emo='em --oneshot'
alias ep='em -pv'
alias epw='em -pUDv world'
alias esy='emerge --sync'
alias eu='emerge --unmerge'
alias euw='em -UDv world'
alias neuw='LINGUAS="en ru de fr ja zh_CN" nice -n 15 emerge -UDv world'
alias ackw='ACCEPT_KEYWORDS="~x86"'

using ACCEPT_KEYWORDS is DEPRECATED see TIP Dealing with masked packages ---

alias emunmask="echo '$@' >> /etc/portage/package.keywords"
alias emuse="echo '$@' >> /etc/portage/package.use"
Use :
 # emunmask sys-kernel/gentoo-sources
 # emuse dev-util/anjuta glade

More aliases:

alias h='history'
alias j='jobs'
alias less='less -r'
alias more='less -r'
alias whois='jwhois'
alias du='du -h'
alias dux='du / 2>/dev/null | egrep "[0-9]([0-9]{2}M|G)"'
alias su-='su -'

Fast directory switch:

OLDPWD='/etc'
OLDPWD1='/root'
OLDPWD2='/home'
OLDPWD3='/'
OLDPWD4='/'
alias cd='OLDPWD5=$OLDPWD4; OLDPWD4=$OLDPWD3; OLDPWD3=$OLDPWD2; OLDPWD2=$OLDPWD1; OLDPWD1=$OLDPWD; cd'
alias cd1='cd $OLDPWD1'
alias cd2='cd $OLDPWD2'
alias cd3='cd $OLDPWD3'
alias cd4='cd $OLDPWD4'
alias cd5='cd $OLDPWD5'
alias pwd='echo " cd1: $OLDPWD"; echo " cd2: $OLDPWD1"; echo " cd3: $OLDPWD2"; echo " cd4: $OLDPWD3"; echo " cd5: $OLDPWD4"; echo -e '\\033[0;30;42m pwd: '\$PWD'\ \033[00m'\;'

Typo-correction:

alias cd...='cd ../..'
alias cd..='cd ..'
alias cd.='cd .'
alias cd~='cd ~'
alias ...='cd ../..'
alias ..='cd ..'
alias ~='cd ~'

More complex alias and a similar version with a function:

alias psox='echo ps ax -Ho pid,ppid,%cpu,%mem,stat,euser,egroup,tname,start_time,ni,priority,command; /bin/ps ax -Ho pid,ppid,%cpu,%mem,stat,euser,egroup,tname,start_time,ni,priority,command'
function pso { if [ "$*" = "" ]; then COL="--width=$COLUMNS"; else COL="$*"; fi; echo "ps ax -o pid,ppid,%cpu,%mem,stat,euser,egroup,tname,start_time,ni,priority,command -H $COL| awk '{if (\$2"'!'"=lpid && \$12!~dummy && \$12"'!'"~\"(^hald-[ar])|(kdeinit)|(agetty)|(dbus)|(awk)\" && \$13"'!'"~\"(kdeinit)|(ksmserver)|(startkde)|(ax)\"){ print \$0}; dummy=\$12; if (\$2"'!'"=lpid) {lpid=0}; if (\$12~\"(spamd)|(postfix)|(kdm)\" || \$2==0){lpid=\$1}}'"; /bin/ps ax -o pid,ppid,%cpu,%mem,stat,euser,egroup,tname,start_time,ni,priority,command -H $COL | awk '{if ($2!=lpid && $12!~dummy && $12!~"(^hald-[ar])|(kdeinit)|(agetty)|(dbus)|(awk)" && $13!~"(kdeinit)|(ksmserver)|(startkde)|(ax)"){ print $0}; dummy=$12; if ($2!=lpid) {lpid=0}; if ($12~"(spamd)|(postfix)|(kdm)" || $2==0){lpid=$1}}' ; }


So an update is now:

esy && euw -a

Alias with variables

You can not make aliases with variables. But you can make functions, having a function in your ~/.bashrc will work just like an alias. To use ssh to copy files to a location on a server you can use:

sendpic () { scp "$@" mina@foo.bar.ca:/www/misc/Pictures/; }

Another way for aliases with variables

If you dont like to use a function, if you need variables, try the following to change to the last working directory:

alias cdo="cd \"\$OLDPWD\""
Note: It is important that there are ONLY double quotes in the expression above, no single quotes like in the other examples!
Note: This should only be taken as an example, because the same thing can be accomplished with cd -.

Creating aliases on shell startup

You can have your aliases created anytime you open an instance of a shell. If you are using bash, edit your ~/.bashrc file and add one alias per line. Once you save and close the file, run this to load your new aliases immediately:

source ~/.bashrc

Otherwise, the new aliases will load whenever you open a new instance of the shell.

Another place to put your aliases if you want them to be system-wide for all users is in /etc/bashrc. To load those aliases, add this line to ~/.bashrc:

File: ~/.bashrc
source /etc/bashrc

or equivalently:

. /etc/bashrc


Another approach. Create an /etc/aliasrc file, and at the top add these lines:

File: /etc/aliasrc
# remove all old aliases  
unalias -a
# reload aliases
alias realias='source /etc/aliasrc'  
# edit aliases  
alias vialias='$EDITOR /etc/aliasrc && realias'

Now add this to the bottom of your ~/.bashrc:

File: ~/.bashrc
source /etc/aliasrc

Links

Retrieved from "http://www.gentoo-wiki.info/TIP_alias"

Last modified: Thu, 26 Jun 2008 01:26:00 +0000 Hits: 35,713