bash:按命令历史记录。它是如何工作的?


7

好。我有一台运行Leopard的旧G5和一台在家运行Ubuntu 10.04的戴尔,以及一台运行Leopard的MacPro。

我经常使用Terminal.app/bash。在我的家G5上它展示了一个很好的功能:使用 ↑ 导航历史记录我从我输入的几个字母开始,得到最后一个命令。

这就是我的意思( | 代表插入符号):

$ ssh user@server
$ vim /some/file/just/to/populate/history
$ ss|

所以,我输入了两个首字母“ssh”,点击 ↑ 结果如下:

$ ssh user@server

而不是这个,这是我在其他地方得到的行为:

$ vim /some/file/just/to/populate/history

如果我一直打 ↑ 要么 ↓,我可以浏览历史 ssh 像这样:

$ ssh otheruser@otherserver
$ ssh user@server
$ ssh yetanotheruser@yetanotherserver

它对任何命令都一样 cat, vim 管他呢。

太棒了。除了我不知道如何在我的其他机器上模仿这种行为。

这是我的 .profile:

export PATH=/Developer/SDKs/flex_sdk_3.4/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/sw/bin:/sw/sbin:/bin:/sbin:/bin:/sbin:/usr/bin:/usr/sbin:$HOME/Applications/bin:/usr/X11R6/bin
export MANPATH=/usr/local/share/man:/usr/local/man:opt/local/man:sw/share/man
export INFO=/usr/local/share/info
export PERL5LIB=/opt/local/lib/perl5
export PYTHONPATH=/opt/local/bin/python2.7
export EDITOR=/opt/local/bin/vim
export VISUAL=/opt/local/bin/vim
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home

export TERM=xterm-color
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=1
export LS_COLORS='no=00:fi=00:di=01;34:ln=target:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.deb=00;31:*.rpm=00;31:*.TAR=00;31:*.TGZ=00;31:*.ARJ=00;31:*.TAZ=00;31:*.LZH=00;31:*.ZIP=00;31:*.Z=00;31:*.Z=00;31:*.GZ=00;31:*.BZ2=00;31:*.DEB=00;31:*.RPM=00;31:*.jpg=00;35:*.png=00;35:*.gif=00;35:*.bmp=00;35:*.ppm=00;35:*.tga=00;35:*.xbm=00;35:*.xpm=00;35:*.tif=00;35:*.png=00;35:*.fli=00;35:*.gl=00;35:*.dl=00;35:*.psd=00;35:*.JPG=00;35:*.PNG=00;35:*.GIF=00;35:*.BMP=00;35:*.PPM=00;35:*.TGA=00;35:*.XBM=00;35:*.XPM=00;35:*.TIF=00;35:*.PNG=00;35:*.FLI=00;35:*.GL=00;35:*.DL=00;35:*.PSD=00;35:*.mpg=00;36:*.avi=00;36:*.mov=00;36:*.flv=00;36:*.divx=00;36:*.qt=00;36:*.mp4=00;36:*.m4v=00;36:*.MPG=00;36:*.AVI=00;36:*.MOV=00;36:*.FLV=00;36:*.DIVX=00;36:*.QT=00;36:*.MP4=00;36:*.M4V=00;36:*.txt=00;32:*.rtf=00;32:*.doc=00;32:*.odf=00;32:*.rtfd=00;32:*.html=00;32:*.css=00;32:*.js=00;32:*.php=00;32:*.xhtml=00;32:*.TXT=00;32:*.RTF=00;32:*.DOC=00;32:*.ODF=00;32:*.RTFD=00;32:*.HTML=00;32:*.CSS=00;32:*.JS=00;32:*.PHP=00;32:*.XHTML=00;32:'

export LC_ALL=C
export LANG=C

stty cs8 -istrip -parenb
bind 'set convert-meta off'
bind 'set meta-flag on'
bind 'set output-meta on'

alias ip='curl http://www.whatismyip.org | pbcopy'
alias ls='ls -FhLlGp'
alias la='ls -AFhLlGp'
alias couleurs='$HOME/Applications/bin/colors2.sh'
alias td='$HOME/Applications/bin/todo.sh'
alias scale='$HOME/Applications/bin/scale.sh'
alias stree='$HOME/Applications/bin/tree'
alias envoi='$HOME/Applications/bin/envoi.sh'
alias unfoo='$HOME/Applications/bin/unfoo'
alias up='cd ..'
alias size='du -sh'
alias lsvn='svn list -vR'
alias jsc='/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc'
alias asl='sudo rm -f /private/var/log/asl/*.asl'
alias trace='tail -f $HOME/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt'
alias redis='redis-server /opt/local/etc/redis.conf'

source /Users/johncoltrane/Applications/bin/git-completion.sh
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWUPSTREAM="verbose git"
export GIT_PS1_SHOWDIRTYSTATE=1

export PS1='\n\[\033[32m\]\w\[\033[0m\] $(__git_ps1 "[%s]")\n\[\033[1;31m\]\[\033[31m\]\u\[\033[0m\] $ \[\033[0m\]'

mkcd () {
  mkdir -p "$*"
  cd "$*"
}

function cdl {
  cd $1
  la
}

n() {
  $EDITOR ~/Dropbox/nv/"$*".txt
}

nls () { 
  ls -c ~/Dropbox/nv/ | grep "$*"
}

copy(){
  curl -s -F 'sprunge=<-' http://sprunge.us | pbcopy
}

if [ -f /opt/local/etc/profile.d/cdargs-bash.sh ]; then
  source /opt/local/etc/profile.d/cdargs-bash.sh
fi

if [ -f /opt/local/etc/bash_completion ]; then
  . /opt/local/etc/bash_completion
fi

任何的想法?


1
和 ~/.inputrc 和 /etc/inputrc?
— Ignacio Vazquez-Abrams

这是我的第4-13行 ~/.inputrc:#默认情况下,up / down分别绑定到previous-history#和next-history。以下是#相同但提供了额外的功能,如果你#输入任何文本(或者更准确地说,如果在行的开头和光标之间有任何文本#),#从该文本开始的历史子集搜索#(例如4dos)。 #注意只删除一行,只需按Ctrl-C“\ e [B”:history-search-forward“\ e [A”:history-search-backward感谢Ignacio。
— romainl

Answers:


7

添加以下内容 ~/.inputrc:

"\e[B": history-search-forward
"\e[A": history-search-backward

我现在已经使用了很长一段时间,因此不幸的是我不能相信这个来源。


我现在只是看到我的.inputrc中的这些说明的前缀与您在(不可读)评论中发布的注释相同,因此它可能是相同的来源。
— Daniel Beck

是的,对不起的评论感到抱歉。伊格纳西奥已经指出了我正确的方向,但它在评论中。我想我会接受你的回答。不知道这些设置来自哪里。
— romainl

在openSUSE上, 历史搜索落后 和 历史搜索前进 被映射到 上一页 和 向下翻页 默认情况下
— Edward
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.