https://wiki.archlinux.org/api.php?action=feedcontributions&user=Micblayo&feedformat=atomArchWiki - User contributions [en]2024-03-29T07:15:26ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Bash&diff=259226Bash2013-05-27T22:20:42Z<p>Micblayo: Change the aliases a bit to more safely and effectively use the referenced tools.</p>
<hr />
<div>[[Category:Command shells]] <br />
[[de:Bash]]<br />
[[es:Bashrc]]<br />
[[it:Bash]]<br />
[[nl:Bashrc]]<br />
[[zh-CN:Bash]]<br />
{{Article summary start}}<br />
{{Article summary text|Discussing and improving Bash's capabilities.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Readline}}<br />
{{Article summary wiki|Environment Variables}}<br />
{{Article summary wiki|Color Bash Prompt}}<br />
{{Article summary end}}<br />
'''Bash''' (Bourne-again Shell) is a [[Command shell|shell]]/programming language by the [[GNU Project]]. Its name is a homaging reference to its predecessor: the long-deprecated Bourne shell. Bash can be run on most UNIX-like operating systems, including GNU/Linux.<br />
<br />
== Invocation ==<br />
<br />
Bash behaviour can be altered depending on how it is invoked. Some descriptions of different modes follow.<br />
<br />
=== Login shell ===<br />
<br />
If Bash is spawned by {{ic|login}} in a tty, by an [[SSH]] daemon, or similar means, it is considered a login shell. This mode can also be engaged using the {{ic|-l}} or {{ic|--login}} command line options.<br />
<br />
=== Interactive shell ===<br />
<br />
Bash is considered an interactive shell if it is started neither with the {{ic|-c}} option nor any non-option arguments, and whose standard input and error are connected to terminals.<br />
<br />
=== POSIX compliance ===<br />
<br />
Bash can be run with enhanced POSIX compliance by starting Bash with the {{ic|--posix}} command-line option or executing ‘{{ic|set -o posix}}’ while Bash is running.<br />
<br />
=== Legacy mode ===<br />
<br />
In Arch {{ic|/bin/sh}} (which used to be the Bourne shell executable) is symlinked to {{ic|/bin/bash}}.<br />
<br />
If Bash is invoked with the name {{ic|sh}}, it tries to mimic the startup behavior of historical versions of {{ic|sh}}.<br />
<br />
== Configuration ==<br />
<br />
The following files can be used to configure Bash:<br />
* {{ic|/etc/profile}}<br />
* {{ic|~/.bash_profile}}<br />
* {{ic|~/.bash_login}}<br />
* {{ic|~/.profile}}<br />
* {{ic|/etc/bash.bashrc}} (''Non-standard'': only some distros, Arch included)<br />
* {{ic|~/.bashrc}}<br />
* {{ic|~/.bash_logout}}<br />
<br />
These files are commonly used:<br />
* {{ic|/etc/profile}} is sourced by all Bourne-compatible shells upon login. It sets up an environment upon login and loads application-specific ({{ic|/etc/profile.d/*.sh}}) settings.<br />
* {{ic|~/.profile}} is read and sourced by Bash when an interactive login shell is started.<br />
* {{ic|~/.bashrc}} is read and sourced by Bash when a non-login interactive shell is started, for example, when you open a virtual console from the desktop environment. This file is useful for setting up a user-specific shell environment.<br />
<br />
=== Configuration file sourcing order at startup ===<br />
<br />
These files are sourced by Bash in different circumstances. <br />
* if interactive + login shell → {{ic|/etc/profile}} then the first readable of {{ic|~/.bash_profile}}, {{ic|~/.bash_login}}, and {{ic|~/.profile}}<br />
** Bash will source {{ic|~/.bash_logout}} upon exit. <br />
* if interactive + non-login shell → {{ic|/etc/bash.bashrc}} then {{ic|~/.bashrc}}<br />
* if login shell + legacy mode → {{ic|/etc/profile}} then {{ic|~/.profile}}<br />
<br />
And by default in Arch:<br />
* {{ic|/etc/profile}} (indirectly) sources {{ic|/etc/bash.bashrc}}<br />
* {{ic|/etc/skel/.bash_profile}} which users are encouraged to copy to {{ic|~/.bash_profile}}, sources {{ic|~/.bashrc}}<br />
which means that {{ic|/etc/bash.bashrc}} and {{ic|~/.bashrc}} will be executed for all interactive shells, whether they are login shells or not.<br />
<br />
=== Shell and environment variables ===<br />
<br />
The behavior of Bash and programs run by it can be influenced by a number of environment variable. Environment variables are used to store useful values such as command search directories, or which browser to use. When a new shell or script is launched it inherits its parent's variables, thus starting with an internal set of shell variables[http://www.kingcomputerservices.com/unix_101/understanding_unix_shells_and_environment_variables.htm ].<br />
<br />
These shell variables in Bash can be exported in order to become environment variables:<br />
VARIABLE=content<br />
export VARIABLE<br />
or with a shortcut<br />
export VARIABLE=content<br />
<br />
Environment variables are conventionally placed in {{ic|~/.profile}} or {{ic|/etc/profile}} so that all bourne-compatible shells can use them.<br />
<br />
See [[Environment Variables]] for more general information.<br />
<br />
== Command line ==<br />
<br />
Bash command line is managed by the separate library called [[Readline]]. Readline provides a lot of shortcuts for interacting with the command line i.e. moving back and forth on the word basis, deleting words etc. It is also Readline's responsibility to manage [[Readline#History|history]] of input commands. Last, but not least, it allows you to create [[Readline#Macros|macros]].<br />
<br />
== Aliases ==<br />
<br />
[[Wikipedia:alias|alias]] is a command, which enables a replacement of a word with another string. It is often used for abbreviating a system command, or for adding default arguments to a regularly used command.<br />
<br />
Personal aliases are preferably stored in {{ic|~/.bashrc}}, and system-wide aliases (which affect all users) belong in {{ic|/etc/bash.bashrc}}.<br />
<br />
An example excerpt from {{ic|~/.bashrc}} covering several time-saving aliases:<br />
{{hc|~/.bashrc<br />
|2=<nowiki><br />
## Modified commands ## {{{<br />
alias diff='colordiff' # requires colordiff package<br />
alias grep='grep --color=auto'<br />
alias more='less'<br />
alias df='df -h'<br />
alias du='du -c -h'<br />
alias mkdir='mkdir -p -v'<br />
alias nano='nano -w'<br />
alias ping='ping -c 5'<br />
# }}}<br />
<br />
## New commands ## {{{<br />
alias da='date "+%A, %B %d, %Y [%T]"'<br />
alias du1='du --max-depth=1'<br />
alias hist='history | grep' # requires an argument<br />
alias openports='ss --all --numeric --processes --ipv4 --ipv6'<br />
alias pgg='ps -Af | grep' # requires an argument<br />
alias ..='cd ..'<br />
# }}}<br />
<br />
# Privileged access<br />
if [ $UID -ne 0 ]; then<br />
alias sudo='sudo '<br />
alias scat='sudo cat'<br />
alias svim='sudoedit'<br />
alias root='sudo -s'<br />
alias reboot='sudo systemctl reboot'<br />
alias poweroff='sudo systemctl poweroff'<br />
alias update='sudo pacman -Su'<br />
alias netctl='sudo netctl'<br />
fi<br />
<br />
## ls ## {{{<br />
alias ls='ls -hF --color=auto'<br />
alias lr='ls -R' # recursive ls<br />
alias ll='ls -l'<br />
alias la='ll -A'<br />
alias lx='ll -BX' # sort by extension<br />
alias lz='ll -rS' # sort by size<br />
alias lt='ll -rt' # sort by date<br />
alias lm='la | more'<br />
# }}}<br />
<br />
## Safety features ## {{{<br />
alias cp='cp -i'<br />
alias mv='mv -i'<br />
alias rm='rm -I' # 'rm -i' prompts for every file<br />
# safer alternative w/ timeout, not stored in history<br />
alias rm=' timeout 3 rm -Iv'<br />
alias ln='ln -i'<br />
alias chown='chown --preserve-root'<br />
alias chmod='chmod --preserve-root'<br />
alias chgrp='chgrp --preserve-root'<br />
alias cls=' echo -ne "\033c"' # clear screen for real<br />
# }}}<br />
<br />
## Make Bash error tollerant ## {{{<br />
alias :q=' exit'<br />
alias :Q=' exit'<br />
alias :x=' exit'<br />
alias cd..='cd ..'<br />
# }}}<br />
<br />
## Pacman aliases ## {{{<br />
#if necessary, replace 'pacman' with your favorite AUR helper and adapt the commands accordingly<br />
alias pac="sudo /usr/bin/pacman -S" # default action - install one or more packages<br />
alias pacu="/usr/bin/pacman -Syu" # '[u]pdate' - upgrade all packages to their newest version<br />
alias pacr="sudo /usr/bin/pacman -Rs" # '[r]emove' - uninstall one or more packages<br />
alias pacs="/usr/bin/pacman -Ss" # '[s]earch' - search for a package using one or more keywords<br />
alias paci="/usr/bin/pacman -Si" # '[i]nfo' - show information about a package<br />
alias paclo="/usr/bin/pacman -Qdt" # '[l]ist [o]rphans' - list all packages which are orphaned<br />
alias pacc="sudo /usr/bin/pacman -Scc" # '[c]lean cache' - delete all not currently installed package files<br />
alias paclf="/usr/bin/pacman -Ql" # '[l]ist [f]iles' - list all files installed by a given package<br />
alias pacexpl="/usr/bin/pacman -D --asexp" # 'mark as [expl]icit' - mark one or more packages as explicitly installed <br />
alias pacimpl="/usr/bin/pacman -D --asdep" # 'mark as [impl]icit' - mark one or more packages as non explicitly installed<br />
<br />
# '[r]emove [o]rphans' - recursively remove ALL orphaned packages<br />
alias pacro="/usr/bin/pacman -Qtdq > /dev/null && sudo /usr/bin/pacman -Rs \$(/usr/bin/pacman -Qtdq | sed -e ':a;N;$!ba;s/\n/ /g')"<br />
# }}}<br />
</nowiki>}}<br />
<br />
== Functions ==<br />
<br />
Bash also supports functions. The following function will extract a wide range of compressed file types. Add the function to {{ic|~/.bashrc}} and use it with the syntax {{ic|extract <file1> <file2> ...}}<br />
<br />
{{hc|~/.bashrc<br />
|2=<nowiki><br />
extract() {<br />
local c e i<br />
<br />
(($#)) || return<br />
<br />
for i; do<br />
c=''<br />
e=1<br />
<br />
if [[ ! -r $i ]]; then<br />
echo "$0: file is unreadable: \`$i'" >&2<br />
continue<br />
fi<br />
<br />
case $i in<br />
*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))<br />
c='bsdtar xvf';;<br />
*.7z) c='7z x';;<br />
*.Z) c='uncompress';;<br />
*.bz2) c='bunzip2';;<br />
*.exe) c='cabextract';;<br />
*.gz) c='gunzip';;<br />
*.rar) c='unrar x';;<br />
*.xz) c='unxz';;<br />
*.zip) c='unzip';;<br />
*) echo "$0: unrecognized file extension: \`$i'" >&2<br />
continue;;<br />
esac<br />
<br />
command $c "$i"<br />
e=$?<br />
done<br />
<br />
return $e<br />
}<br />
</nowiki>}}<br />
<br />
{{Note|Make sure extglob is enabled: {{ic|shopt -s extglob}}, by adding it to the {{ic|~/.bashrc}} (see: http://mywiki.wooledge.org/glob#Options_which_change_globbing_behavior). It is enabled by default, if using [[#Advanced completion|Bash completion]].}}<br />
Another way to do this is to install the {{AUR|unp}} package from the [[AUR]] which contains a Perl script.<br />
<br />
Very often changing to a directory is followed by the {{ic|ls}} command to list its contents. Therefore it is helpful to have a second function doing both at once.<br />
In this example we will name it {{ic|cl}} and show an error message if the specified directory does not exist.<br />
{{hc|~/.bashrc<br />
|2=<nowiki><br />
# cd and ls in one<br />
cl() {<br />
if [ -d "$1" ]; then<br />
cd "$1"<br />
ls<br />
else<br />
echo "bash: cl: '$1': Directory not found"<br />
fi<br />
}<br />
</nowiki>}}<br />
Of course the ls command can be altered to fit your needs, for example {{ic|1=$ ls -hall --color=auto}}.<br />
<br />
More Bash function examples can be found in [https://bbs.archlinux.org/viewtopic.php?id=30155 BBS#30155.]<br />
<br />
== Tips and tricks ==<br />
<br />
=== Prompt customization ===<br />
<br />
The Bash prompt is governed by the variable {{ic|$PS1}}. To colorize the Bash prompt, use:<br />
{{hc|~/.bashrc|2=<br />
#PS1='[\u@\h \W]\$ ' # To leave the default one<br />
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\] '<br />
}}<br />
This {{ic|$PS1}} is useful for a root Bash prompt, with red designation and green console text. For more info, see: [[Color Bash Prompt]].<br />
<br />
=== Tab completion ===<br />
<br />
[[Wikipedia:Command-line_completion|Tab completion]] allows for completing partially typed commands by pressing {{Keypress|Tab}} twice.<br />
<br />
Despite Bash's native support for basic file name, command, and variable tab completion the package {{Pkg|bash-completion}} (available in the [[Official repositories]]) extends functionality by adding it to a wide range of commands and their options.<br />
<br />
Start a new shell and it will be automatically enabled by {{ic|/etc/bash.bashrc}}.<br />
{{Note|1=The normal expansions that you are used to like {{ic|$ ls file.*<tab><tab>}} will not work unless you {{ic|$ compopt -o bashdefault <prog>}} for all programs you want to fallback to the normal glob expansions. See https://bbs.archlinux.org/viewtopic.php?id=128471 and https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html}}<br />
<br />
==== Faster completion ====<br />
<br />
For a single press of {{Keypress|Tab}} to produce a list of all possible completions (both when a partial or no completion is possible):<br />
<br />
{{hc|~/.inputrc|<br />
set show-all-if-ambiguous on<br />
}}<br />
<br />
Alternatively, to produce such a list only when no completion is possible:<br />
<br />
{{hc|~/.inputrc|<br />
set show-all-if-ambiguous on<br />
set show-all-if-unmodified on<br />
}}<br />
<br />
==== Manually ====<br />
<br />
For basic completion use lines in the form of {{ic|complete -cf your_command}}:<br />
{{hc|~/.bashrc|<br />
complete -cf sudo<br />
complete -cf man<br />
}}<br />
{{Note|These will conflict with {{Pkg|bash-completion}}.}}<br />
<br />
=== The "command not found" hook ===<br />
<br />
The [[pkgfile]] package includes a "command not found" hook that will automatically search the [[official repositories]] when you enter an unrecognized command. Then it will display something like this:<br />
<br />
{{hc|$ abiword|<br />
abiword may be found in the following packages:<br />
extra/abiword 2.8.6-7 usr/bin/abiword<br />
}}<br />
<br />
An alternative "command not found" hook is also provided by the AUR package [https://aur.archlinux.org/packages.php?ID=52305 command-not-found], which will generate an output like the following:<br />
<br />
{{hc|$ abiword|<br />
The command 'abiword' is been provided by the following packages:<br />
'''abiword''' (2.8.6-7) from extra<br />
[ abiword ]<br />
'''abiword''' (2.8.6-7) from staging<br />
[ abiword ]<br />
'''abiword''' (2.8.6-7) from testing<br />
[ abiword ]<br />
}}<br />
<br />
=== Display error codes ===<br />
<br />
To set {{ic|trap}} to intercept the non-zero return code of last program:<br />
{{hc|~/.bashrc|<br />
EC() { echo -e '\e[1;33m'code $?'\e[m\n'; }<br />
trap EC ERR<br />
}}<br />
<br />
=== Disable Ctrl+z in terminal ===<br />
<br />
You can disable the {{Keypress|Ctrl+z}} feature (pauses/closes your application) by wrapping your command like this:<br />
#!/bin/bash<br />
trap "" 20<br />
''adom''<br />
<br />
Now when you accidentally press {{Keypress|Ctrl+z}} in {{AUR|adom}} instead of {{Keypress|Shift+z}} nothing will happen because {{Keypress|Ctrl+z}} will be ignored.<br />
<br />
=== Clear the screen after logging out ===<br />
<br />
To clear the screen after logging out on a virtual terminal:<br />
{{hc|~/.bash_logout|<br />
clear<br />
reset<br />
}}<br />
<br />
=== ASCII art, fortunes and cowsay ===<br />
<br />
Along with colors, system info and ASCII symbols, Bash can be made to display a piece of ASCII art on login. ASCII images can be found online and pasted into a text file, or generated from scratch. To set the image to display in a terminal on login, use:<br />
{{hc|~/.bashrc|<br />
cat ''/path/to/text/file''<br />
}}<br />
<br />
Random poignant, inspirational, silly or snide phrases can be found in {{Pkg|fortune-mod}}.<br />
<br />
{{hc|$ fortune|<br />
It is Texas law that when two trains meet each other at a railroad crossing,<br />
each shall come to a full stop, and neither shall proceed until the other has gone.<br />
}}<br />
<br />
{{Note|By default, {{ic|fortune}} displays quotes and phrases that are rather innoccuous. However, the package does contain a set of comments some people will find offensive, located in {{ic|/usr/share/fortune/off/}}. See the [http://manpages.ubuntu.com/manpages/quantal/en/man6/fortune.6.html man page] ({{ic|man fortune}}) for more info on these.}}<br />
<br />
To have a random phrase displayed when logging into a terminal, use:<br />
{{hc|~/.bashrc|<br />
command fortune<br />
}}<br />
<br />
These two features can be combined, using the program {{Pkg|cowsay}}:<br />
<br />
{{hc|command cowsay $(fortune)|<nowiki><br />
The earth is like a tiny grain of sand, <br />
only much, much heavier. <br />
----------------------------------------- <br />
\ ^__^<br />
\ (oo)\_______<br />
(__)\ )\/\<br />
||----w |<br />
|| ||<br />
</nowiki>'''<font color<nowiki>=</nowiki>red>(</font><font color<nowiki>=</nowiki>green>user@host</font><font color<nowiki>=</nowiki>red>)-(</font><font color<nowiki>=</nowiki>green>10:10 AM Wed Dec 22</font><font color<nowiki>=</nowiki>red>)'''<br />
--(</font><font color<nowiki>=</nowiki>green>~</font>)<font color<nowiki>=</nowiki>red>)---></font><br />
}}<br />
{{hc|command cowthink $(fortune)|<nowiki><br />
( The best cure for insomnia is to get a )<br />
( lot of sleep. -W.C. Fields )<br />
---------------------------------------- <br />
o ^__^<br />
o (oo)\_______<br />
(__)\ )\/\<br />
||----w |<br />
|| ||<br />
</nowiki>'''<font color<nowiki>=</nowiki>red>(</font><font color<nowiki>=</nowiki>green>user@host</font><font color<nowiki>=</nowiki>red>)-(</font><font color<nowiki>=</nowiki>green>10:10 AM Wed Dec 22</font><font color<nowiki>=</nowiki>red>)'''<br />
--(</font><font color<nowiki>=</nowiki>green>~</font>)<font color<nowiki>=</nowiki>red>)---></font><br />
}}<br />
<br />
The ASCII images are generated by {{ic|.cow}} text files located in {{ic|/usr/share/cows}}, and all themes can be listed with the {{ic|cowsay -l}}. These files can be edited to the user's liking; custom images can also be created from scratch or found on the net. The easiest way create a custom cow file is to use an existing one as a template. To test the custom file:<br />
<br />
$ cowsay -f ''/path/to/file'' $(fortune)<br />
<br />
This can produce some nice eye candy, and the commands used can be more complex. For a specialized example, take a look [http://bambambambam.wordpress.com/2009/07/04/futurama-ascii-with-slashdot-header-quotes-in-your-terminal/ here.] Another example, to use a random cow, random facial expression, and nicely wrap the text of long fortunes:<br />
<br />
{{hc|<nowiki>command fortune -a | fmt -80 -s | $(shuf -n 1 -e cowsay cowthink) -$(shuf -n 1 -e b d g p s t w y) -f $(shuf -n 1 -e $(cowsay -l | tail -n +2)) -n</nowiki>|<nowiki><br />
________________________________________ <br />
( Fry: I must be a robot. Why else would )<br />
( human women refuse to date me? )<br />
---------------------------------------- <br />
o<br />
o<br />
o <br />
,'``.._ ,'``.<br />
:,--._:)\,:,._,.:<br />
:`--,''@@@:`...';\ <br />
`,'@@@@@@@`---'@@`. <br />
/@@@@@@@@@@@@@@@@@:<br />
/@@@@@@@@@@@@@@@@@@@\<br />
,'@@@@@@@@@@@@@@@@@@@@@:\.___,-.<br />
`...,---'``````-..._@@@@|:@@@@@@@\<br />
( )@@@;:@@@@)@@@\ _,-.<br />
`. (@@@//@@@@@@@@@@`'@@@@\<br />
: `.//@@)@@@@@@)@@@@@,@;<br />
|`. _,'/@@@@@@@)@@@@)@,'@,'<br />
:`.`-..____..=:.-':@@@@@.@@@@@_,@@,'<br />
,'\ ``--....-)=' `._,@@\ )@@@'``._<br />
/@_@`. (@) /@@@@@) ; / \ \`-.'<br />
(@@@`-:`. `' ___..'@@_,-' |/ `.)<br />
`-. `.`.``-----``--,@@.'<br />
|/`.\`' ,',');<br />
` (/ (/<br />
</nowiki>'''<font color<nowiki>=</nowiki>red>(</font><font color<nowiki>=</nowiki>green>user@host</font><font color<nowiki>=</nowiki>red>)-(</font><font color<nowiki>=</nowiki>green>10:10 AM Wed Dec 22</font><font color<nowiki>=</nowiki>red>)'''<br />
--(</font><font color<nowiki>=</nowiki>green>~</font>)<font color<nowiki>=</nowiki>red>)---></font><br />
}}<br />
<br />
{{Note|For full 256-colored cowsay-like art use {{Pkg|ponysay}} (version 3.0 has 422 ponies). The syntax is the same, meaning {{ic|$ ponysay ''message''}} to say something and {{ic|ponysay -l}} for a complete list of ponies.<br />
To create more ponies use {{aur|util-say-git}} and store them in {{ic|~/.local/share/ponysay/ponies}} and {{ic|~/.local/share/ponysay/ttyponies/}} for desktop and TTY, respectively.}}<br />
<br />
=== ASCII historical calendar ===<br />
<br />
To install [http://www.openbsd.org/cgi-bin/man.cgi?query=calendar&sektion=1 calendar] files in your {{ic|~/.calendar}} directory you will need the {{Pkg|rpmextract}} package installed. Then from your home directory, run the following:<br />
$ mkdir -p ~/.calendar<br />
$ curl -o calendar.rpm http://download.fedora.redhat.com/pub/epel/5/x86_64/calendar-1.25-4.el5.x86_64.rpm<br />
$ rpm2cpio calendar.rpm | bsdtar -C ~/.calendar --strip-components=4 -xf - ./usr/share/c*<br />
<br />
This will then print out the calendar items:<br />
$ sed -n "/$(date +%m\\/%d\\\|%b\*\ %d)/p" $(find ~/.calendar /usr/share/calendar -maxdepth 1 -type f -name 'c*' 2>/dev/null);<br />
<br />
=== Customise title ===<br />
<br />
The {{ic|$PROMPT_COMMAND}} variable allows you to execute a command before the prompt. For example, this will change the title to your full current working directory:<br />
<br />
{{hc|~/.bashrc|<nowiki><br />
export PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"'<br />
</nowiki>}}<br />
<br />
This will change your title to the last command run, and make sure your history file is always up-to-date:<br />
{{hc|~/.bashrc|<nowiki><br />
export HISTCONTROL=ignoreboth<br />
export HISTIGNORE='history*'<br />
export PROMPT_COMMAND='history -a;echo -en "\e]2;";history 1|sed "s/^[ \t]*[0-9]\{1,\} //g";echo -en "\e\\";'<br />
</nowiki>}}<br />
<br />
=== Fix line wrap on window resize ===<br />
<br />
When you resize your xterm in vi for example, Bash will not get the resize signal, and the text you type will not wrap correctly, overlapping the prompt.<br />
<br />
{{hc|~/.bashrc|<br />
# check the window size after each command and, if necessary,<br />
# update the values of LINES and COLUMNS.<br />
shopt -s checkwinsize<br />
}}<br />
<br />
=== History completion ===<br />
<br />
History completion bound to arrow keys (down, up) (see: [[Readline#History]] and [https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html Readline Init File Syntax]):<br />
<br />
{{hc|~/.bashrc|<br />
bind '"\e[A": history-search-backward'<br />
bind '"\e[B": history-search-forward'<br />
}}<br />
<br />
or:<br />
<br />
{{hc|~/.inputrc|<br />
"\e[A": history-search-backward<br />
"\e[B": history-search-forward<br />
}}<br />
<br />
=== Auto "cd" when entering just a path ===<br />
<br />
Bash can automatically prepend {{ic|cd }} when entering just a path in the shell. For example:<br />
{{hc|$ /etc|<br />
bash: /etc: Is a directory<br />
}}<br />
<br />
But after:<br />
{{hc|~/.bashrc|<br />
shopt -s autocd<br />
}}<br />
<br />
You get:<br />
[user@host ~] $ /etc<br />
cd /etc<br />
[user@host etc]<br />
<br />
=== Mimic Zsh run-help ability ===<br />
<br />
Zsh can invoke the manual for the written command pushing {{Keypress|Alt+h}}.<br />
A similar behaviour is obtained in Bash by appending this line in your {{ic|inputrc}} file:<br />
{{hc|/etc/inputrc|<br />
"\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"<br />
}}<br />
<br />
== See also ==<br />
<br />
* [http://tldp.org/LDP/abs/html/ Advanced Bash Scripting Guide] - Very good resource regarding shell scripting using Bash<br />
* [irc://irc.freenode.net#bash An active and friendly IRC channel for Bash]<br />
* [http://wiki.bash-hackers.org/doku.php Bash Hackers Wiki] - Excellent Bash Wiki<br />
* [http://www.gnu.org/software/bash/manual/bashref.html Bash Reference Manual] - Official reference (654K)<br />
* [http://www.ibm.com/developerworks/linux/library/l-bash.html Bash Scripting by Example]<br />
* [http://bashscripts.org Bashscripts.org] - A Forum for bashers.<br />
* [http://chakra-linux.org/wiki/index.php?title=Startup_files Chakra Wiki: Startup files]<br />
* [http://www.caliban.org/bash Completion Guide]<br />
* [http://gotux.net/arch-linux/custom-bash-commands-functions/ Custom Bash Commands & Functions]<br />
* [[General Recommendations]] - General Recommendations for Arch<br />
* [http://mywiki.wooledge.org/BashFAQ Greg's Wiki] - Highly recommended<br />
* [http://tldp.org/HOWTO/Xterm-Title-4.html How to change the title of an xterm]<br />
* [http://www.gnu.org/software/bash/manual/bash.html Bash manual page]<br />
* [http://www.grymoire.com/Unix/Quote.html Quote Tutorial]<br />
* [http://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html Readline Init File Syntax ]* [http://www.aosabook.org/en/bash.html The Bourne-Again Shell] - The third chapter of ''The Architecture of Open Source Applications''</div>Micblayohttps://wiki.archlinux.org/index.php?title=Tmux&diff=258672Tmux2013-05-25T00:47:21Z<p>Micblayo: Fixed the conditional for launching tmux - originally it only worked because tmux is smart.</p>
<hr />
<div>[[es:Tmux]]<br />
[[ru:Tmux]]<br />
[[tr:Tmux]]<br />
[[Category:Terminal emulators]]<br />
{{Article summary start}}<br />
{{Article summary text|This article explains how to install and configure tmux.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|GNU Screen}}<br />
{{Article summary end}}<br />
[http://tmux.sourceforge.net/ Tmux] is a "terminal multiplexer: it enables a number of terminals (or windows), each running a separate program, to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached." <br />
<br />
Tmux is notable as a BSD-licensed alternative to [[Screen Tips|GNU Screen]]. Although similar, there are many differences between the programs, as noted on the [http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/FAQ tmux FAQ page]. Most notably, tmux is currently under active development, in contrast to screen, which has not had a stable release since August 8, 2008.<br />
<br />
==Installation==<br />
[[pacman|Install]] {{Pkg|tmux}}, available in the [[Official Repositories]].<br />
<br />
==Configuration==<br />
A user-specific configuration file should be located at {{ic|~/.tmux.conf}}, while a global configuration file should be located at {{ic|/etc/tmux.conf}}. Default configuration files can be found in {{Ic|/usr/share/tmux/}}. <br />
<br />
===Key bindings===<br />
{| style="float:right;border:1px #cccccc solid;margin:5px;padding:5px;width:200px;"<br />
|+ ''Prefix all commands with'' {{Ic|Ctrl-b}}<br />
!Cmd<br />
!Action<br />
|-<br />
|c<br />
|Create a new window<br />
|-<br />
|n<br />
|Change to next window<br />
|-<br />
|p<br />
|Change to previous window<br />
|-<br />
|"<br />
|Split pane horizontally<br />
|-<br />
|%<br />
|Split pane vertically<br />
|-<br />
|,<br />
|Rename current window<br />
|-<br />
|o<br />
|Move to next pane<br />
|}<br />
<br />
By default, command key bindings are prefixed by Ctrl-b. For example, to vertically split a window type {{Ic|Ctrl-b %}}.<br />
<br />
After splitting a window into multiple panes, you can resize a pane by the hitting prefix key (i.e. {{Ic|Ctrl-b}}) and, while continuing to hold Ctrl, press Left/Right/Up/Down. Swapping panes is achieved in the same manner, but by hitting ''o'' instead of a directional key.<br />
<br />
{{Tip|To mimic screen key bindings copy {{ic|/usr/share/tmux/screen-keys.conf}} to either of the configuration locations.}}<br />
<br />
Key bindings may be changed with the bind and unbind commands in {{ic|tmux.conf}}. For example, you can change the prefix key (i.e. {{Ic|Ctrl-b}}) to {{Ic|Ctrl-a}} by adding the following commands in your configuration file:<br />
{{bc|<br />
unbind C-b<br />
set -g prefix C-a<br />
}}<br />
<br />
Additional ways to move between windows include:<br />
Ctrl-b l (Move to the previously selected window)<br />
Ctrl-b w (List all windows / window numbers)<br />
Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)<br />
Ctrl-b q (Show pane numbers, when the numbers show up type the key to goto that pane)<br />
<br />
What if you have 10+ windows open? Tmux has a find-window option & keybinding. <br />
Ctrl-b f <window name> (Search for window name)<br />
Ctrl-b w (Select from interactive list of windows)<br />
<br />
===Browsing URL's===<br />
To browse URL's inside tmux you must have {{aur|urlview}} installed and configured.<br />
<br />
Inside a new terminal:<br />
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"<br />
<br />
Or inside a new tmux window (no new terminal needed):<br />
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'<br />
<br />
=== Setting the correct term===<br />
If you are using a 256 colour terminal, you will need to set the correct term in tmux. You can do this in either the {{ic|tmux.conf}}:<br />
<br />
set -g default-terminal "screen-256color" <br />
<br />
or in your {{ic|.bashrc}} with a test like:<br />
<br />
# for tmux: export 256color<br />
[ -n "$TMUX" ] && export TERM=screen-256color<br />
<br />
If you enable xterm-keys in your {{ic|tmux.conf}}, then you need to build a custom terminfo to declare the new escape codes or applications will not know about them. Compile the following with {{ic|tic}} and you can use "xterm-screen-256color" as your TERM:<br />
<br />
# A screen- based TERMINFO that declares the escape sequences<br />
# enabled by the tmux config "set-window-option -g xterm-keys".<br />
#<br />
# Prefix the name with xterm- since some applications inspect<br />
# the TERM *name* in addition to the terminal capabilities advertised.<br />
xterm-screen-256color|GNU Screen with 256 colors bce and tmux xterm-keys,<br />
<br />
# As of Nov'11, the below keys are picked up by<br />
# .../tmux/blob/master/trunk/xterm-keys.c:<br />
kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H,<br />
kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~,<br />
kRIT=\E[1;2C,<br />
<br />
# Change this to screen-256color if the terminal you run tmux in<br />
# doesn't support bce:<br />
use=screen-256color-bce,<br />
<br />
=== Other Settings ===<br />
Set scrollback to 10000 lines with <br />
set -g history-limit 10000<br />
<br />
==Session initialization==<br />
You can have tmux open a session with preloaded windows by including those details in your {{ic|~/.tmux.conf}}:<br />
<br />
new -n WindowName Command<br />
neww -n WindowName Command<br />
neww -n WindowName Command<br />
<br />
To start a session with split windows (multiple panes), include the splitw command below the neww you would like to split; thus:<br />
<br />
new -s SessionName -n WindowName Command<br />
neww -n foo/bar foo<br />
splitw -v -p 50 -t 0 bar<br />
selectw -t 1 <br />
selectp -t 0<br />
<br />
would open 2 windows, the second of which would be named foo/bar and would be split vertically in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).<br />
<br />
{{Note|Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your {{ic|.conf}} }}<br />
<br />
To manage multiple sessions, source separate session files from your conf file:<br />
<br />
# initialize sessions<br />
bind F source-file ~/.tmux/foo<br />
bind B source-file ~/.tmux/bar<br />
<br />
==Troubleshooting==<br />
===Scrolling issues===<br />
If you have issues scrolling with Shift-PageUp/Shift-PageDown in your terminal, try this:<br />
<br />
set -g terminal-overrides 'xterm*:smcup@:rmcup@'<br />
<br />
=== Shift+F6 not working in Midnight Commander ===<br />
<br />
If the {{Keypress|Shift+F6}} key combination is not working with either {{ic|1=TERM=screen}} or {{ic|1=TERM=screen-256color}}, then from inside tmux, run this command:<br />
infocmp > screen (or screen-256color)<br />
<br />
Open the file in a text editor, and add the following to the bottom of that file:<br />
kf16=\E[29~,<br />
<br />
Then compile the file with {{ic|tic}}. The keys should be working now.<br />
<br />
==ICCCM Selection Integration==<br />
It is possible to copy a tmux paste buffer to an ICCCM selection, and vice-versa, by defining a shell command which interfaces tmux with an X11 selection interface. The following tmux config file snippet effectively integrates {{Ic|CLIPBOARD}} with the current tmux paste buffer using xclip:<br />
<br />
{{hc|~/.tmux.conf|<br />
...<br />
##CLIPBOARD selection integration<br />
##Requires prefix key before the command key<br />
#Copy tmux paste buffer to CLIPBOARD<br />
bind C-c run "tmux show-buffer <nowiki>|</nowiki> xclip -i -selection clipboard"<br />
#Copy CLIPBOARD to tmux paste buffer and paste tmux paste buffer<br />
bind C-v run "tmux set-buffer -- \"$(xclip -o -selection clipboard)\"; tmux paste-buffer"<br />
}}<br />
<br />
If you get an output similar to {{ic| \346\227\245\346\234\254\350\252\236\343\201\247 }} when pasting utf-8 characters, try changing this line:<br />
{{bc|bind C-c run "tmux show-buffer <nowiki>|</nowiki> xclip -i -selection clipboard"}}<br />
to this:<br />
{{bc|bind C-p run "tmux save-buffer - <nowiki>|</nowiki> xclip -i -selection clipboard"}}<br />
<br />
==Tips and tricks==<br />
<br />
===Start tmux in urxvt===<br />
Use this command to start urxvt with a started tmux session. I use this with the exec command from my .ratpoisonrc file.<br />
{{bc|<nowiki>urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"</nowiki>}}<br />
<br />
===Start tmux on every shell login===<br />
<br />
Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:<br />
{{bc|<nowiki>[[ -z "$TMUX" ]] && exec tmux</nowiki>}}<br />
<br />
{{hc|~/.bashrc|<nowiki><br />
# If not running interactively, do not do anything<br />
[[ $- != *i* ]] && return<br />
[[ -z "$TMUX" ]] && exec tmux</nowiki>}}<br />
<br />
{{note|This snippet ensures that tmux is not launched inside of itself (something tmux usually already checks for anyway). tmux sets $TMUX to the socket it is using whenever it runs, so if $TMUX isn't set or is length 0, we know we aren't already running tmux.}}<br />
<br />
And this snippet start only one session(unless you start some manually), on login, try attach at first, only create a session if no tmux is running.<br />
<br />
{{bc|<nowiki># TMUX<br />
if which tmux 2>&1 >/dev/null; then<br />
#if not inside a tmux session, and if no session is started, start a new session<br />
test -z "$TMUX" && (tmux attach || tmux new-session)<br />
fi</nowiki>}}<br />
<br />
This snippet does the same thing, but also checks tmux is installed before trying to launch it. It also tries to reattach you to an existing tmux session at logout, so that you can shut down every tmux session quickly from the same terminal at logout.<br />
{{bc|<nowiki># TMUX<br />
if which tmux 2>&1 >/dev/null; then<br />
# if no session is started, start a new session<br />
test -z ${TMUX} && tmux<br />
<br />
# when quitting tmux, try to attach<br />
while test -z ${TMUX}; do<br />
tmux attach || break<br />
done<br />
fi</nowiki>}}<br />
<br />
{{note|Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)}}<br />
<br />
===Use tmux windows like tabs===<br />
<br />
The following settings added to {{ic|~/.tmux.conf}} allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — [[rxvt-unicode#urxvtq_with_tabbing|urxvt's tabbing extensions]]. An advantage thereof is that these virtual “tabs” are independent of the terminal emulator.<br />
<br />
#urxvt tab like window switching (-n: no prior escape seq)<br />
bind -n S-down new-window<br />
bind -n S-left prev<br />
bind -n S-right next<br />
bind -n C-left swap-window -t -1<br />
bind -n C-right swap-window -t +1<br />
<br />
Of course, those should not overlap with other applications' hotkeys, such as the terminal's. Given that they substitute terminal tabbing that might as well be deactivated, though.<br />
<br />
It can also come handy to supplement the EOT hotkey {{Keypress|Ctrl+d}} with one for tmux's detach:<br />
<br />
bind-key -n C-j detach<br />
<br />
===Clients simultaneously interacting with various windows of a session===<br />
<br />
In “[http://mutelight.org/articles/practical-tmux Practical Tmux]”, Brandur Leach writes:<br />
<br />
{{Box||Screen and tmux's behaviour for when multiple clients are attached to one session differs slightly. In Screen, each client can be connected to the session but view different windows within it, but in tmux, all clients connected to one session must view the same window.<br />
<br />
This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.}}<br />
<br />
The script “{{Ic|tmx}}” below implements this — the version here is slightly modified to execute “{{Ic|tmux new-window}}” if “1” is its second parameter. Invoked as {{Ic|tmx <base session name> [1]}} it launches the base session if necessary. Otherwise a new “client” session linked to the base, optionally add a new window and attach, setting it to kill itself once it turns “zombie”.<br />
<br />
{{hc|tmx|2=<nowiki><br />
#!/bin/bash<br />
<br />
#<br />
# Modified TMUX start script from:<br />
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html<br />
#<br />
# Store it to `~/bin/tmx` and issue `chmod +x`.<br />
#<br />
<br />
# Works because bash automatically trims by assigning to variables and by <br />
# passing arguments<br />
trim() { echo $1; }<br />
<br />
if [[ -z "$1" ]]; then<br />
echo "Specify session name as the first argument"<br />
exit<br />
fi<br />
<br />
# Only because I often issue `ls` to this script by accident<br />
if [[ "$1" == "ls" ]]; then<br />
tmux ls<br />
exit<br />
fi<br />
<br />
base_session="$1"<br />
# This actually works without the trim() on all systems except OSX<br />
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)<br />
if [[ "$tmux_nb" == "0" ]]; then<br />
echo "Launching tmux base session $base_session ..."<br />
tmux new-session -s $base_session<br />
else<br />
# Make sure we are not already in a tmux session<br />
if [[ -z "$TMUX" ]]; then<br />
echo "Launching copy of base session $base_session ..."<br />
# Session is is date and time to prevent conflict<br />
session_id=`date +%Y%m%d%H%M%S`<br />
# Create a new session (without attaching it) and link to base session <br />
# to share windows<br />
tmux new-session -d -t $base_session -s $session_id<br />
if [[ "$2" == "1" ]]; then<br />
# Create a new window in that session<br />
tmux new-window<br />
fi<br />
# Attach to the new session & kill it once orphaned<br />
tmux attach-session -t $session_id \; set-option destroy-unattached<br />
fi<br />
fi<br />
</nowiki>}}<br />
<br />
A useful setting for this is<br />
<br />
setw -g aggressive-resize on<br />
<br />
added to {{ic|~/.tmux.conf}}. It causes tmux to resize a window based on the smallest client actually viewing it, not on the smallest one attached to the entire session.<br />
<br />
An alternative taken from [http://sourceforge.net/mailarchive/forum.php?thread_name=CAPBqLKEC0MAFR%2BWUYqCuyd%3DKB47HK8CFSuAf%3Dd%3DW2H3F4fpMZw%40mail.gmail.com&forum_name=tmux-users] is to put the following ~/.bashrc:<br />
<br />
{{hc|.bashrc|2=<nowiki><br />
function rsc() {<br />
CLIENTID=$1.`date +%S`<br />
tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-session -t $CLIENTID<br />
}<br />
<br />
function mksc() {<br />
tmux new-session -d -s $1<br />
rsc $1<br />
}<br />
</nowiki>}}<br />
<br />
Citing the author:<br />
{{Box||"mksc foo" creates a always detached permanent client named "foo". It also<br />
calls "rsc foo" to create a client to newly created session. "rsc foo"<br />
creates a new client grouped by "foo" name. It has destroy-unattached<br />
turned on so when I leave it, it kills client.<br />
<br />
Therefore, when my computer looses network connectivity, all<br />
"foo.something" clients are killed while "foo" remains. I can then call<br />
"rsc foo" to continue work from where I stopped.}}<br />
<br />
===Changing the configuration with tmux started===<br />
<br />
By default tmux reads {{ic|~/.tmux.conf}} only if it was not already running. To have tmux load a configuration file afterwards, execute:<br />
<br />
tmux source-file <path><br />
<br />
This can be added to {{ic|~/.tmux.conf}} as e. g.:<br />
<br />
bind r source-file <path><br />
<br />
You can also do ^: and type :<br />
source .tmux.conf<br />
<br />
===Template script to run program in new session resp. attach to existing one===<br />
<br />
This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.<br />
<br />
#!/bin/bash<br />
<br />
PID=$(pidof $1)<br />
<br />
if [ -z "$PID" ]; then<br />
tmux new-session -d -s main ;<br />
tmux new-window -t main -n $1 "$*" ;<br />
fi<br />
tmux attach-session -d -t main ;<br />
tmux select-window -t $1 ;<br />
exit 0<br />
<br />
A derived version to run ''irssi'' with the ''nicklist'' plugin can be found on [[Irssi#irssi_with_nicklist_in_tmux|its ArchWiki page]].<br />
<br />
===Terminal emulator window titles===<br />
If you SSH into a host in a tmux window, you'll notice the window title of your terminal emulator remains to be {{ic|user@localhost}} rather than {{ic|user@server}}. To allow the title bar to adapt to whatever host you connect to, set the following in {{ic|~/.tmux.conf}}<br />
<br />
set -g set-titles on<br />
set -g set-titles-string "#T"<br />
<br />
For {{ic|set-titles-string}}, {{ic|#T}} will display {{ic|user@host:~}} and change accordingly as you connect to different hosts. You can also set many more options here.<br />
<br />
== See also ==<br />
* [http://mutelight.org/articles/practical-tmux Practical Tmux] by Brandur Leach, providing a number of configuration tips<br />
* [http://www.openbsd.org/faq/faq7.html#tmux Tmux tutorial] section from the OpenBSD FAQ<br />
* [http://www.openbsd.org/cgi-bin/man.cgi?query=tmux OpenBSD Reference Manual for tmux]<br />
* [http://www.dayid.org/os/notes/tm.html Screen and tmux feature comparison] page by Dayid Alan<br />
* [http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/ Tmux tutorial Part 1] & [http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2 Part 2] blog posts on Hawk Host<br />
* [https://github.com/kooothor/.dotfiles/blob/master/.tmux.conf tmux.conf] example with CPU bar and shortcut to search man pages and display them vertically<br />
* [https://github.com/erikw/tmux-powerline tmux-powerline] statusbar configuration for tmux that looks like vim-powerline and consist of dynamic segments.<br />
<br />
'''Forum threads'''<br />
* 2009-11-06 - Arch Linux - [https://bbs.archlinux.org/viewtopic.php?id=84157&p=1 Anyone loving Tmux in place of Screen? Info/Tips etc. URLs I've found]</div>Micblayo