The most useful thing in bash
coderwall.com
coderwall.com
> Create ~/.inputrc and fill it with this:
"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
> This allows you to search through your history using the up and down arrows … i.e. type "cd /" and press the up arrow and you'll search through everything in your history that starts with "cd /".I have no problem viewing the site of someone trying to show me something useful.
1. I've definitely seen this before, perhaps on reddit
2. You press ctrl-r <type> to search back through history. Fucking around with up key mapping is retarded.
Now my command history is searchable, just like any other file or buffer I have open.
I use the same idea for any repl, R,python,ruby,node,clojure. Bash is just another repl.
The major advantage of the emacs buffer method is that a lot of tasks I do on the computer span many commands. I can search for one command I remember executing 3 months ago, and then review the series of steps I took before and after, along with the output.
Typically finding a single command I've executed before doesn't help a whole lot. I need context.
1) Try eshell which allows you to use Emacs Lisp for shell scripting, as well as override some common programs to use Emacs instead (grep, man, etc). I'm someone who has never really bothered to learn shell scripting and am much more comfortable using Lisp, even Emacs Lisp which is sort of the crummiest of the popular Lisps (don't get me wrong I love Emacs) Obviously YMMV and if you know shell scripting this is a non-issue, but for me it comes down to being constantly surprised by the little bit of shell scripting I've had to do, and having Emacs Lisp work basically the way I expect (dynamic scoping, Lisp-2 and all)
2) Both eshell and M-x shell are not actually terminal emulators, so anything that requires a terminal emulator, like pagers, git log (ok this is a bad example since you should just be using magit, but anyway...) or some programs that update the terminal with their status act totally goofy and sometimes just break altogether. In this case, you want term-mode, which is a proper terminal emulator. Be sure to read up on the different modes as otherwise you'll get stuck inside that buffer ;) http://www.gnu.org/software/emacs/manual/html_node/emacs/Ter...
Hope this helps another Emacs novice... most of this I learned from here:
http://www.masteringemacs.org/articles/2010/11/01/running-sh...
Not as cool as zsh's push-line, though: http://www.cs.elte.hu/zsh-manual/zsh_14.html
Ctrl+P :)
Control-p: history-search-backward
Control-n: history-search-forward C-a C-k C-pfind . -name foo
vi $(!!)
Are you feeling lucky?
% mkdir foo
% chmod g+s !$
% ls -ld !$ # (make sure it's correct)
% cd !$ $ chmod 755 foo
$ cd !$:p
cd foo
$ cd foo
There are some similar "tricks" here:So, when you press ! $ <space>, it expands out to "foo" in place, so you can be sure that's what you wanted, and edit it in place.
I think :p predates magic-space. They're both decades old at this point though.
It's the "Event Designators" in the bash manpage for those who are interested.
As I tried to indicate, you should ideally use whichever will make more sense in your history later. If the command you're typing should always be applied to that file, which happened to be the last argument of the previous command this time, then use !$; if the command you're typing should always be applied to the last argument of the previous command, and it just happened to be that file this time, use $_.
hg diff -b <filename>
hg commit -m "comments" !$
I use 'hg st' so much that I've aliased it as 'st'. It's just about muscle memory now.
$ Make me a sandwich
What? Make it yourself
$ sudo !!
Okayhttp://geekblog.oneandoneis2.org/index.php/2013/04/03/protip...
Instead, I generally drop little shell scripts around my work area for specific tasks. Working on iterating a build/install to chase a specific bug? Stuff it into a little script. Writing a find command to munge a bunch of files with xargs or -exec or whatnot and realize I'm into the third line or editting? Stop and echo it into a file, then finish it in emacs.
The idea of doing all that junk and then relying on the bash history file (instead of, y'know, the actual filesystem) to store it for me seems weird.
Which is not to take away from your "it comes down to personal preference" by any means - just trying to make mine a bit clearer.
Very much so; much like the GP, I would "history | grep" and even had it aliased to 'hg' (which is one of the reasons I don't like Mercurial, just to start another flame war), but then I was reminded of C-r, which I should have known better (as an Emacs user (that's two flamewars . . . )), but oddly hadn't been using as I was just too lazy to really learn bash in depth.
Instead of:
echo a b c > /tmp/foo
echo d e f > /tmp/bar
diff /tmp/foo /tmp/bar
Just do:
diff <(echo a b c) <(echo d e f)
"anonymous named pipes" is contradictory, since anonymous means that something has no name.
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history
[[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" history-search-backward
[[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" history-search-forwardhttp://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.g...
Status codes and argument lists:
$status # replaces $?
$argv # replaces $@
Aliases are just functions (configure your startup in
~/.config/fish/config.fish): # alias ls="ls -lrth"
function lr; ls -lrth $argv; end
For-loops are familiar: # for n in one two three; do echo $n; done
for n in one two three; echo $n; end
The lack of '!$' confounded my hard-wired fingers until I settled on Alt-u to avoid up-arrows (see /usr/share/fish/functions/fish_default_key_bindings.fish): bind \eu history-token-search-backward # replaces !$
bind \ei history-token-search-forward # bonus fun
The remaining price for fish's pseudo-telepathic assistance is the occasional frustration with applications that rely on the value of a user's default shell (/etc/passwd).It has a build in git status script you can include in your prompt that's awesome. Here's my fish prompt (note __fish_git_prompt):
function fish_prompt
set last_status $status
set_color $fish_color_cwd
printf '%s' (prompt_pwd)
set_color normal
printf '%s ' (__fish_git_prompt)
set_color normal
end
Sample: ~/G/Calendar (branch_name * ↩ =)Anyone know if that can be done in bash/readline?
The idea is that: many commands are location-specific. When you type `lc`, it will only give you commands that were executed in that directory.
https://github.com/pconerly/lc-listcommands-bash
I would love to get some up/down arrow ability for `lc`. Right now I do a lot of:
$ lc | grep X
export HISTSIZE=10000
Allows you to have a much bigger history.
export HISTCONTROL=ignoreboth
so that repeated commands are only stored once. Handy for those, like me, who reflexively type commands like "ps aux" when thinking about what to do next.
cat ~-/sub/path/to/file
(that's tilde-minus-slash, which tilde-expands to the previous directory) -bash: \e[A:: command not found
-bash: \e[B:: command not found
when my shell starts, and it doesn't work.1) If you work on multiple machines avoid heavy configuration and remapping of keys. It will frustrate you to no end. "Oh I forgot on this machine it is the up arrow on the other it is Ctrl+R" etc.
2) Up/Dn Arrows already do a useful thing -- they cycle commands in historical order. That is also useful in addition to the Ctrl+R history search.
But after thinking it over, I definitely know those who are awesome sysadmins, and require every bit of the setup to match their preferences before they will deign to work in that system.
Otherwise it works. But ... ctrl U to clear the line, doesn't clear it completely. It only clears text from the start of the line to the prompt. What has been added from the history is left there one the line. I have to first move to the end of the line then type ctrl U to clear it.
worth mentioning, nothing comes close this one.
-bash: \e[A:: command not found -bash: \e[B:: command not found
edit: thanks for the responses below. Learn something new every day :)
bind '"\e[A": history-search-backward' # up-arrow
bind '"\e[B": history-search-forward' # down-arrow1) start typing a command;
2) look at or copy+paste stuff from the previous commands
I usually do my history search through history|grep.
I guess I re-run commands fairly often, so for me the history-search-backward thing is very useful; when I just need to use the default up/down binding, I just hit Ctrl-E Ctrl-U (go to end of line, erase everything to beginning of line) and I'm good to go.
Ok, but what if your prompt is empty and you press up once? Will it still continue to maintain the old behaviour?
To expand on this, it searches based on what's on the left side of your cursor. So if your cursor is all the way on the left side, it acts as it did before. You lose nothing!
I prefer to map it to esc-p which coincides with the setting in tcsh. Like that:
"\M-p": history-search-backward
So you basically type something and then press esc p to have the shell complete it.