13 ms·
The most useful thing in bash
- yutyut 13y agoCtrl+R lets you search old commands in a similar way. I would say it's more intuitive, too.
- geoka9 13y agoNot to mention that it doesn't require you to take you right hand off the home row.
- raylu 13y agoAnd you get to keep using the up arrow to go to your previous command. Not as cool as zsh's push-line, though: http://www.cs.elte.hu/zsh-manual/zsh_14.html http://www.cs.elte.hu/zsh-manual/zsh_14.html
- geoka9 13y ago> And you get to keep using the up arrow to go to your previous command. Ctrl+P :)
- csmatt 13y agoThis is the truly great part. It's the same reason I use emacs.
- njharman 13y agouse vim mode, then j,k.
- 3JPLW 13y agoBind: Control-p: history-search-backward Control-n: history-search-forward
- sirwitti 13y agoThanks a lot. I never liked Ctrl+R. This is also the way Vim handles history...
- willismichael 13y agoCtrl+R always seems natural to me, but maybe that's because I'm an emacs user.
- felix 13y agoOr there's always "set -o vi" then you can search through your history with /foo and n to find the next match.
- deleted 13y ago[deleted]
- xutopia 13y agoSomething that gives this snippet much more weight. export HISTSIZE=10000 Allows you to have a much bigger history.
- sikhnerd 13y agoYou would also likely want to increase HISTFILESIZE as well. HISTFILESIZE sets the size of the history file, HISTSIZE is per-session
- gusfoo 13y agoalso 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.
- johtso 13y agoAnd if you use ZSH you can go one step further with proper substring search. https://github.com/zsh-users/zsh-history-substring-search https://github.com/zsh-users/zsh-history-substring-search
- svachalek 13y agoOne history trick I use a lot that most people don't seem to know about is the !$ variable, which refers to the last argument of the previous command. It's basically a bash pronoun. !! is the entire previous command which is occasionally useful with find: find . -name foo vi $(!!)
- spc476 13y agoand !N, where N is a number, is the Nth command in your history.
- mturmon 13y agoAnd !N:$ is the last argument of the N'th command in history. Even more useful, !vi:$ is the last argument of the last "vi" you did.
- zimpenfish 13y agoWoah, clearly I need to spend tomorrow re-reading the BASH manual.
- barrkel 13y agoI find alt-. to be more usable - I know for sure what it will do, as I see the effect of the expansion immediately.
- jacobparker 13y agosudo !! Are you feeling lucky?
- mturmon 13y agoA usage pattern with !$ is: % mkdir foo % chmod g+s !$ % ls -ld !$ # (make sure it's correct) % cd !$
- jdf 13y agoAlternatively, you can add ':p' to the third line. This will print out the command rather than executing it. Additionally, it's also added to your bash history, so you can add it by pressing the up arrow. $ chmod 755 foo $ cd !$:p cd foo $ cd foo There are some similar "tricks" here: https://news.ycombinator.com/item?id=5337558 https://news.ycombinator.com/item?id=5337558
- rhizome 13y agoThis tip is default behavior in tcsh.
- alinajaf 13y agoAnd fish.
- yoava 13y agoAnd even our old Windows shell...
- anotherhue 13y agoAfter cutting my teeth on FreeBSD (with tcsh) the absence/non-defaultedness of this feature always irked me.
- bherms 13y agoWhen I paste this and then source the file, I get: -bash: \e[A:: command not found -bash: \e[B:: command not found edit: thanks for the responses below. Learn something new every day :)
- jimrandomh 13y agoIt goes in ~/.inputrc, the configuration file for GNU readline. Those aren't shell commands.
- raylu 13y ago.inputrc. And don't source it, just start a new bash.
- MostAwesomeDude 13y agoinputrc is not to be sourced; it's read by readline when the shell starts. You need to restart your shell.
- sikhnerd 13y agoCTRL-x-r will reload it as well, without having to restart your shell
- myhf 13y agoIf you really want to put them in .bashrc instead of .inputrc, you can source readline directives with the 'bind' command, like so: bind '"\e[A": history-search-backward' # up-arrow bind '"\e[B": history-search-forward' # down-arrow
- jfb 13y ago% exec env zsh
- curtis 13y agoI've always found interactive history UI to be difficult to work with beyond the most trivial cases. Instead, I'd always find myself typing "history | grep ..." over and over again. Eventually I wrote an improved history command with built-in filtering: http://curtisb.posthaven.com/a-better-history-command http://curtisb.posthaven.com/a-better-history-command
- ajross 13y agoIt's funny how personal workflows can be. I do the history|grep thing too, but never felt the need to automate it further. It's a relatively rare thing (maybe once a day), used when I know I did something specific in the past but forget a detail or two. 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.
- mdpye 13y agoCtrl-x e opens the current line in your $EDITOR, smoother than echoing, especially when there are pipes involved. Save it out as a script from there if you like.
- ajross 13y agoIt's C-x C-e in my bash, fwiw. And I tried getting used to this but it breaks locality by dropping the new file into /tmp instead of $PWD. Quoting a line isn't really that hard, or you can always cut and paste with the mouse.
- dllthomas 13y agoI use screens and session variables to manage context, so scripts don't need to be "around my work area" but rather in the appropriate ~/.context/$SESSION/bin directory (or ~/bin if sufficiently general).
- shmerl 13y agoIt's not the same as Ctrl+R - it actually doesn't look for a match in the middle of the string, only from the beginning of it, so while it's better with going back and forth between the matches, it matches less data to begin with.
- alpb 13y agoI am using oh-my-zsh and this is already configured for me.
- michaelmior 13y agoI've been using oh-my-zsh for around a year now and I never realized this.
- xbryanx 13y agoAutoJump is my co-pilot in these situations: https://github.com/joelthelion/autojump https://github.com/joelthelion/autojump
- mbetter 13y agoI would say the single most useful thing in bash is the ability to type in commands.
- blablabla123 13y agoagree, there is (obviously) some key combination that does the same thing without changing the usual behaviour of up/down which I prefer a lot
- pavel_lishin 13y agoTo be fair, if your prompt is empty, up/down behavior doesn't change. 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.
- blablabla123 13y ago> To be fair, if your prompt is empty, up/down behavior doesn't change. Ok, but what if your prompt is empty and you press up once? Will it still continue to maintain the old behaviour?
- pavel_lishin 13y agoYes. 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!
- blablabla123 13y agoThis is indeed very nice.
- deleted 13y ago[deleted]
- vowelless 13y agoMy favorite history trick in bash is "cd -".
- Bill_Dimm 13y agoIt works in tcsh, too.
- philsnow 13y agoyou can also use the previous directory like this: cat ~-/sub/path/to/file (that's tilde-minus-slash, which tilde-expands to the previous directory)
- jamesjporter 13y agoA funny story about "cd -". When I first started learning the command line during a programming course, they basically told us just enough to get by in order to complete the assignments. Somehow, "cd -" was mentioned but not "cd ..", so for a long time I used "cd -" as my only method for getting to the parent directory! (since that was usually where I had just come from). Reading about everyone discovering "cd -" much later in learning bash is very amusing, since I have been using it since day one (albeit in a totally incorrect way!)
- Numberwang 13y agoGood old doskey
- skarlowicz 13y agoI wouldn't be surprised to learn I'm the only living person still using doskey. Yes, in April 2013, and on W7. I autoload a file with macro definitions in cmd prompt window, and this works great for programs I want at my fingertips but don't want to clutter PATH with (or desktop with shortcuts, or create a .bat/.cmd etc; I do realise there are other ways to do that, but my solution works good enough since DOS times). The unfortunate thing with doskey aliases is you cannot use them as pipe recipient, they have to start at the line start. On the other hand, they can accept parameters.
- StavrosK 13y agoThat's why I use fish. Not only does it cycle like that by default, but it autocompletes entire commands, searches for the substring in the middle of words, looks better, is faster, etc etc.
- dsl 13y agoI am really amazed more people on HN aren't using fish. Everyone should check it out: http://ridiculousfish.com/shell/ http://ridiculousfish.com/shell/
- Rovanion 13y agoDoes it run bash scripts or does it have its own scripting language?
- dsl 13y agoYour script should have "#!/bin/bash" at the top, which tells the shell what interpreter you want it ran with. So, yes?
- etanol 13y agoIt is the kernel the one that interprets shebangs, not the shell: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_script.c?id=v3.8 http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.g...
- jfb 13y agoIt's unfortunately on the one hand too beholden to POSIX shell behavior, while still being too different from mainline Bourne derivatives. I've tried it a few times, and the cognitive load is too extreme for my 20+ yrs of POSIX brain-damage to work around.
- sourpoi 13y agoI don't use fish for shell scripting, but I'm enjoying it as my primary environment after making these adjustments.. 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).
- D9u 13y agoI thought that the command history search was default behavior for BASH? (I use zsh)
- aw3c2 13y agoSince this is ugly clickbait, here is the "content" of the linked site: > 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 /".
- saym 13y agoclickbait? Are you suggesting that this would've been better as a Show HN post? I have no problem viewing the site of someone trying to show me something useful.
- deleted 13y ago[deleted]
- 13y ago
- asb 13y agoI also love the history-search-{forward,backward} shortcuts. I took over the Raspberry Pi SD card image generation process back when we moved to Wheezy for the 'official' images and put this shortcut in to the default install with the hopes of leaving it there if no-one complained. Sadly someone did, I caved in, and it is no more.
- deleted 13y ago[deleted]
- mdpye 13y agoWhat does this give you over the default Ctrl-r?
- csmatt 13y agomy thoughts exactly
- dima55 13y agoCtrl-R finds commands that contain the string anywhere. The method the OP posted finds commands that begin with the string. This is an important difference, and if you give it a try, you'll likely agree the latter is what you want most of the time.
- csmatt 13y agoah ok. Thanks for the clarification :)
- EzGraphs 13y agoThanks for the clarification - although depending upon how you work, this is not much of a benefit. For instance - if you type "/path/to/script.sh" you want to search for the name of the script - not the beginning of the path.
- mh- 13y agoC-r in zsh takes regexp patterns (so you could do *^substr to match begins-only).. I hadn't noticed this wasn't the behavior in bash with readline. Anyone know if that can be done in bash/readline?
- mh- 13y agowhoops, errant asterisk that I can't edit out. unclosed attempt-to-italicize
- qu4z-2 13y agoIn my workflow, I almost always want substring match as that allows me to input either the filename (object) or the command (verb).
- babs474 13y agoThis is a killer feature of emacs and M-x shell for me. 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.
- jaredandrews 13y agoM-x shell is one of the first things I just show people when I introduce them to Emacs. I had a databases class a few years ago where the CLI program we used to interact with the database was a basically an input loop where you entered commands. It had absolutely no concept of history though so if you messed up a query you had to retype it or use the clunky copy and paste feature of whatever terminal emulator you were using. Emacs changed everything though be cause I could just kill my last command and paste it into the new prompt. I have since done this with many other commands and I love it.
- callmecosmas 13y agoI'm an emacs lover myself, but for that use-case it seems like all you need is rlwrap: http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap
- spacemanaki 13y agoI've recently been using shells inside Emacs exclusively and not even starting Terminal.app any more. I've got a couple of tips for anyone doing this, based on my (limited) experience: 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/Term-Mode.html#Term-Mode 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-shells-in-emacs-overview/ http://www.masteringemacs.org/articles/2010/11/01/running-sh...
- pajju 13y agoI use fasd: Its the best bash tool out there. Jump, search the previous history based on frequency of usage. It updates your search with its algorithm matching the most predicted one. https://github.com/clvv/fasd https://github.com/clvv/fasd worth mentioning, nothing comes close this one.
- dbbolton 13y agoI use pg up/pg dn for history searching and leave the arrows for simple up and down lines, like so (in zsh): [[ -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-forward
- skcin7 13y agoMy bash does this automatically and I do not have a ~/.inputrc file. I'm on Mac OS X 10.8.2 using the Terminal app (which is a bash command line interface). I assumed this was a default feature of bash but apparently not. If somebody can explain to me why I have this feature by default, I would be grateful.
- drcube 13y agoC-r isn't much harder than up/down arrows (or C-p/C-n). Why is this such a big deal?
- gbog 13y agoWith C-r you need to first type C-r, then type your search key. With the OP's inputrc nice hack, you first type the begining of your command, and then up. To me it is very different.
- buddhistpirate 13y agoWith Ctrl-R it is the same. If you start typing a command, then type Ctrl-R it will use that as the first part of your search.
- yannk 13y agoYou sure? Not in the bash versions (3 & 4) I'm using... In anycase, I'll stick to C-R since I know it works universally. And I depend on C-R quite a bit.
- easytiger 13y agoyea it is worse
- deleted 13y ago[deleted]
- civilian 13y agoAwesome. I wrote another history tool called "list commands" 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 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
- KevinMS 13y agoAnybody know how to get this to work with Ctrl-p and ctrl-n? I try not to touch the arrow keys because they are too small on my new keyboard :(
- coherentpony 13y agoDoes someone care to explain the difference between this and <C-r>?
- gknoy 13y agoC-r will search what you type afterwards, (e.g., "C-r ssh" will then show me the previous command that starts with "ssh") whereas this tip seems to be searching to match what you've already typed. Pretty cool, IMO.
- mitchty 13y agoUmm, .inputrc is readline not bash so technically this can be used in anything linked to readline?
- Nick_C 13y agoYes, including REPLs that use readline. Those that don't you can use rlwrap.
- merraksh 13y agoThis would not allow me to 1) start typing a command; 2) look at or copy+paste stuff from the previous commands I usually do my history search through history|grep.
- rdtsc 13y agoI disagree. 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.
- hobs 13y agoI couldnt agree more. I had a knee jerk reaction to say that it would be someone who works in bash who would say something like this, because constantly sshing in to various boxes and having to constantly tweak your various . files is a fucking pain. 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.
- mixmastamyk 13y agoUse version control on your dotfiles.
- CatMtKing 13y agoIf you do a backward history search with the prompt empty, it's the equivalent of cycling commands historically, isn't it?
- andrewflnr 13y agoYou can still search chronologically by hitting up arrow without typing anything. Ctrl-R still works. The only thing that's frustrating for me is not having it on computers I haven't assimilated yet. ;)
- nshankar 13y agoUseful. Please share if you have such gems.
- DavidSJ 13y agoOne super-useful bash feature is anonymous named pipes, which allows the equivalent of multiple pipe "arguments" to a single command. 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)
- dhamidi 13y agoThat feature is actually named "Process Substitution". "anonymous named pipes" is contradictory, since anonymous means that something has no name.
- DavidSJ 13y agoYeah, poor wording on my part. They do have a name, but normally you don't care about it.
- chrisaycock 13y agoThis feature is known as "process substitution": http://tldp.org/LDP/abs/html/process-sub.html http://tldp.org/LDP/abs/html/process-sub.html
- tehwalrus 13y agoThis doesn't work for me on Mac OS X Mountain Lion. I get: -bash: \e[A:: command not found -bash: \e[B:: command not found when my shell starts, and it doesn't work.
- davemaya 13y agojust use oh-my-zsh https://github.com/robbyrussell/oh-my-zsh https://github.com/robbyrussell/oh-my-zsh this feature is enable by default.
- chmike 13y agoI get "\e[A" and "\e[B" command not found. 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.
- Syssiphus 13y agoOr I could use Ctrl-r.
- james2vegas 13y agoOr just use vi mode 'set -o vi' in sh, 'set editing-mode vi' in ~/.inputrc for bash and other readline clients.