使用 Bash 时您最喜欢的命令行技巧是什么? [英] What is your single most favorite command-line trick using Bash?
问题描述
我们都知道如何使用<ctrl>-R
来反向搜索历史,但是你知道你可以使用<ctrl>-S
来转发搜索是否设置了 stty stop ""
?另外,您是否尝试过运行 bind -p 来查看列出的所有键盘快捷键?默认情况下,Mac OS X 上有超过 455 个.
We all know how to use <ctrl>-R
to reverse search through history, but did you know you can use <ctrl>-S
to forward search if you set stty stop ""
? Also, have you ever tried running bind -p to see all of your keyboard shortcuts listed? There are over 455 on Mac OS X by default.
您最喜欢使用 bash 的最不为人知的技巧、键盘快捷键或 shopt 配置是什么?
What is your single most favorite obscure trick, keyboard shortcut or shopt configuration using bash?
推荐答案
在运行命令时,有时我想运行一个带有先前参数的命令.为此,您可以使用此快捷方式:
When running commands, sometimes I'll want to run a command with the previous ones arguments. To do that, you can use this shortcut:
$ mkdir /tmp/new
$ cd !!:*
有时,如果我需要在文件列表上运行一堆命令,我会中断单行循环而不是使用 find.
Occasionally, in lieu of using find, I'll break-out a one-line loop if I need to run a bunch of commands on a list of files.
for file in *.wav; do lame "$file" "$(basename "$file" .wav).mp3" ; done;
在我的 .bash_login(或 .bashrc)中配置命令行历史选项非常有用.以下是我在 Macbook Pro 上使用的一组设置.
Configuring the command-line history options in my .bash_login (or .bashrc) is really useful. The following is a cadre of settings that I use on my Macbook Pro.
设置以下内容会使 bash 删除历史记录中的重复命令:
Setting the following makes bash erase duplicate commands in your history:
export HISTCONTROL="erasedups:ignoreboth"
我也将我的历史记录大小设置得相当高.为什么不?在今天的微处理器上,它似乎并没有减慢任何速度.
I also jack my history size up pretty high too. Why not? It doesn't seem to slow anything down on today's microprocessors.
export HISTFILESIZE=500000
export HISTSIZE=100000
我做的另一件事是忽略历史记录中的一些命令.无需记住退出命令.
Another thing that I do is ignore some commands from my history. No need to remember the exit command.
export HISTIGNORE="&:[ ]*:exit"
你肯定想设置 histappend.否则,bash 会在您退出时覆盖您的历史记录.
You definitely want to set histappend. Otherwise, bash overwrites your history when you exit.
shopt -s histappend
我使用的另一个选项是 cmdhist.这使您可以将多行命令作为一个命令保存到历史记录中.
Another option that I use is cmdhist. This lets you save multi-line commands to the history as one command.
shopt -s cmdhist
最后,在 Mac OS X 上(如果您不使用 vi 模式),您需要重置 <CTRL>-S,使其不再滚动停止.这可以防止 bash 将其解释为前向搜索.
Finally, on Mac OS X (if you're not using vi mode), you'll want to reset <CTRL>-S from being scroll stop. This prevents bash from being able to interpret it as forward search.
stty stop ""
这篇关于使用 Bash 时您最喜欢的命令行技巧是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!