Home

Mastering Bash and Terminal

638 pointsby blockloopover 8 years ago

42 comments

Normal_gaussianover 8 years ago
Rather than temporarily suspend vim to use the terminal you can get vim to suspend and resume itself with the exclamation mark command. This has the benefit of not wreaking havoc on your vim session and allowing you to read data into vim by prefixing with r.<p>For example<p><pre><code> :!ls </code></pre> will execute ls and show you the result (press enter to return)<p><pre><code> :r!ls </code></pre> will read the result of ls in for you<p>More usefully<p><pre><code> :r!sed -n5,10p that&#x2F;other&#x2F;file </code></pre> will read lines 5-10 from that other file.<p>However you will most often want to<p><pre><code> :!make :!up build :!git status :!git commit -am &quot;Fixed #23&quot;</code></pre>
评论 #13400892 未加载
评论 #13400654 未加载
评论 #13401235 未加载
评论 #13402048 未加载
评论 #13400833 未加载
评论 #13401312 未加载
评论 #13402543 未加载
评论 #13401884 未加载
评论 #13400628 未加载
评论 #13400608 未加载
评论 #13401986 未加载
评论 #13400594 未加载
评论 #13400676 未加载
评论 #13401556 未加载
评论 #13400695 未加载
评论 #13400692 未加载
评论 #13400590 未加载
评论 #13403386 未加载
评论 #13401756 未加载
greenspotover 8 years ago
Great post &amp; thread, a tl;dr:<p><pre><code> Ctrl-r search history - then Ctrl-r again to show next match - then Tab to show all options Ctrl-p previous command or arrow up Ctrl-n next command or arrow down export HISTCONTROL=ignoreboth:erasedups Add to .bashrc to avoid duplicate entries Ctrl-a to beginning of line Ctrl-e to end of line Alt-b one word back Alt-f one word forward Ctrl-k delete to end of line Ctrl-u delete to beginning of line Alt-d delete to end of word Ctrl-w delete to beginning of word Alt-Backspc same cd - change to last dir pushd &lt;dir&gt; mark current dir and go to &lt;dir&gt; popd go to marked dir z fuzzy cd, install from https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z j fuzzy cd and more, install via autojump Ctrl-z to background &amp; suspend bg recent background app continue running fg bring recent background app to front disown -h remove recent background app from current tty fg %n bring nth app to front, e.g.: fg %2 for second less better than cat, doesn&#x27;t flood screen, same keys find find files, e.g. find &#x2F; -name &lt;filename&gt; ag install via the_silver_searcher, faster grep tree shows dir like a GUI app, install !! last command, e.g. sudo !! fish bash alternative with more sensible defaults man bash read more about bash</code></pre>
评论 #13404262 未加载
评论 #13403747 未加载
评论 #13405363 未加载
oblioover 8 years ago
The article is nice but a small part of it rubs me the wrong way:<p>&gt; I know there are some cool newcomers out there like zsh and fish, but after trying others out I always found that some of my utilities were missing or ill-replaced.<p>First of all bash was first released in 1989 and zsh arrived just 1 year later so zsh is in no way a newcomer.<p>Secondly zsh is almost strictly a bash superset so I don&#x27;t know what he was missing (or what he found &quot;ill-replaced&quot;).
评论 #13400707 未加载
评论 #13400821 未加载
teddyhover 8 years ago
Regarding macOS and bash, it is slightly remiss of the article to not at least mention that the version of bash in macOS is<p>1. Ancient.<p>2. Will most likely never be updated by Apple<p>(Most GNU- and Linux-based systems, and also Windows, on the other hand, continue to use the latest versions.)
评论 #13400734 未加载
评论 #13400865 未加载
whackover 8 years ago
Personally, I found the following to be extremely easy and powerful. A no-brainer that should be a bash default really.<p><pre><code> if [ -t 1 ] then # search for commands that start off with the same characters already typed bind &#x27;&quot;\e[A&quot;:history-search-backward&#x27; bind &#x27;&quot;\e[B&quot;:history-search-forward&#x27; fi </code></pre> One of my friends also recommended version-controlling your config files and storing them on gitlab, which I&#x27;m only sad I didn&#x27;t do sooner. It&#x27;s been such a help in keeping my aliases and configs in sync, as I make changes across numerous different machines.
评论 #13405001 未加载
评论 #13403506 未加载
评论 #13403177 未加载
评论 #13402012 未加载
评论 #13400917 未加载
评论 #13404362 未加载
h2hnover 8 years ago
I actually resolved most of that common issues with just bash: :)<p><i>Substring history search, so you can use just a substring to look for a argument,command. Binded to ctr+r&#x2F;s by default. ;)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;asyncBash#use" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;asyncBash#use</a><p></i>Changing directories: Last n directories, transparent popd&#x2F;pushd.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dirStack" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dirStack</a><p><i>Movements: vim-surround for your cli, so you can do ysiw&quot; o whatever... ;)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;bash-surround" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;bash-surround</a><p></i>Control-n right: So just type the start and control+n to search for the arguments&#x2F;commands starting with whatever. And the classical up&#x2F;down to look up for a complete history line:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bash&#x2F;.inputrc#L234" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bash&#x2F;.inputr...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bash&#x2F;.inputrc#L170" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;liloman&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bash&#x2F;.inputr...</a><p>There&#x27;re a ton of hidden functionality and customization behind the classical bash instalation. :)
评论 #13401959 未加载
wyclifover 8 years ago
<i>I also assume you&#x27;re using bash. I know there are some cool newcomers out there like zsh and fish</i><p>While there is much useful in this post, I always find comments like this one odd. bash was released back in 1989, zsh was released one year later, in 1990. One year difference in age almost thirty years ago means that you can&#x27;t really call zsh a newcomer. Maybe he&#x27;s talking about adoption, though.
评论 #13402047 未加载
评论 #13401950 未加载
评论 #13402054 未加载
alphast0rmover 8 years ago
For changing directories, you can also use something like z (<a href="https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z</a>) to jump around:<p><pre><code> Tracks your most used directories, based on &#x27;frecency&#x27;. After a short learning phase, z will take you to the most &#x27;frecent&#x27; directory that matches ALL of the regexes given on the command line, in order. For example, z foo bar would match &#x2F;foo&#x2F;bar but not &#x2F;bar&#x2F;foo.</code></pre>
评论 #13401241 未加载
评论 #13402296 未加载
TheAceOfHeartsover 8 years ago
I&#x27;ve tried bash, zsh, and fish. After trying all three, I&#x27;m staying with fish. bash and zsh don&#x27;t have sensible defaults, and configuring them is tedious. fish works great out of the box, and it&#x27;s really fast. When I picked up zsh I used oh-my-zsh and later prezto, but it was slow and figuring out what everything all the framework did was complicated.<p>With fish I have a setup.fish script that defines all my universal exports, for when I setup a new computer. This is for private tokens, like HOMEBREW_GITHUB_API_TOKEN. For aliases and utilities, I wrote a fisherman [0] plugin. It has a functions folder and a fishfile for the few other plugins I use.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;fisherman&#x2F;fisherman" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fisherman&#x2F;fisherman</a>
评论 #13403702 未加载
评论 #13403532 未加载
why-elover 8 years ago
In the spirit of sharing: one of my favorite lines in my bashrc is the alias of `rm` to `rm -i`. This prompts for a confirmation. You might not need it but I deleted some important, not-yet-checked-in-git files in the past by accident.<p>If you want to delete everything and don&#x27;t want to keep typing yes just do `yes | rm bla`.
评论 #13402314 未加载
lxeover 8 years ago
The one tool that saved me quite possibly a few months of typing paths over my lifetime is autojump: <a href="https:&#x2F;&#x2F;github.com&#x2F;wting&#x2F;autojump" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;wting&#x2F;autojump</a>. I can&#x27;t imaging a terminal workflow without it.
评论 #13403241 未加载
xolbover 8 years ago
One that I learned from Mr. Robot: put a space before the command to avoid being registered in the history.
评论 #13401657 未加载
alkonautover 8 years ago
&gt; &quot;tool that every developer uses regardless of language, platform, or framework it&#x27;s the terminal&quot;<p>I&#x27;m not sure I agree with that. If you work in a decent IDE and your vcs is not git then you can do pretty well without a terminal. Especially on Windows.
llasramover 8 years ago
Worth it just for finding out about `stty -ixon`. I never would have guessed from the `stty` man page description that this option would give me back C-s and C-q to bind to something actually useful.
评论 #13402494 未加载
pmoriartyover 8 years ago
bash (and shells in general) are so hacky (in a bad way) that I wouldn&#x27;t want to waste my time mastering them. Whenever a shell script grows beyond 10 or 20 lines, I try to rewrite it in a &quot;real&quot; programming language. Fancy shell tricks are a code smell to me, and clarity and simplicity are of far greater importance.<p>For me, as far as shells go it&#x27;s usually enough to know the basics and be able to look stuff up when debugging other people&#x27;s shell scripts.
评论 #13402162 未加载
评论 #13402501 未加载
johnchristopherover 8 years ago
I love `set -o vi`. It gives you vim motion in the terminal.
gigatexalover 8 years ago
Instead of ctrl-z and bg why not just do # &lt;cmd&gt; &amp; to push it to the background.
评论 #13413427 未加载
gogolbover 8 years ago
On macOS, instead of<p><pre><code> ip addr show en0 | grep -inet\ | awk &#x27;{ print $3 }&#x27; | awk -F&#x2F; &#x27;{print $1}&#x27; | pbcopy </code></pre> you can use:<p><pre><code> ipconfig getifaddr en0 </code></pre> If you wanna stick with ip addr, a more compact command is:<p><pre><code> ip addr show en0 | awk &#x27;&#x2F;inet&#x2F;{split($2,a,&quot;&#x2F;&quot;); print a[1]}&#x27;</code></pre>
Scea91over 8 years ago
One thing I am struggling with is that when I use several terminal windows then the history is not recorded immediately and history from one window is not available in other windows. Some time ago I was looking up how to solve this but didn&#x27;t find a solution that would work for me.
评论 #13405277 未加载
hiqover 8 years ago
About bindings, it would have been better to redirect to:<p>LESS=+&#x2F;&quot;DEFAULT KEY BINDINGS&quot; man readline<p>assuming it exists on mac.<p>About suspend, what&#x27;s the benefit of suspending vim using C-z? Shouldn&#x27;t you use a terminal multiplexer instead, or even terminal tabs if you don&#x27;t want to learn how to use tmux or screen (which I find weird if you already spent time to learn how to use vim but alright)?<p>I only ever suspend a program when I want it to stop, for instance because it slows other programs and I realize I would rather resume it when I&#x27;m not in front of my computer. Even in that case, I often just renice the program instead. Stopping a program just because you want to run some bash commands looks like an anti-pattern to me, but maybe there are better motives I&#x27;m not aware of.
评论 #13401486 未加载
danielrm26over 8 years ago
Rather than trying to remember all those commands for moving around in your command, I recommend turning on vim mode for the command line, and remapping ESC to &quot;jk&quot;.<p>bindkey -v bindkey -M viins &#x27;jk&#x27; vi-cmd-mode<p>Then you can edit your command line the same way you would edit a line in vim.
评论 #13404137 未加载
blunteover 8 years ago
Those Emacs commands you mention for moving around on the line also work in a lot of other text fields, in a lot of other programs.<p>Notably they don&#x27;t work in MS Office, but they work in web browsers and practically every other app I have on my Mac.
评论 #13403842 未加载
santaclausover 8 years ago
I&#x27;m curious about the popularity of Bash vis a vis, say, Csh. Is Bash more popular due to superior features, or is simply because it is the default on quite a few *nix distros (and macOS)?
评论 #13401473 未加载
评论 #13402557 未加载
coldteaover 8 years ago
&gt;<i>If there is one tool that every developer uses regardless of language, platform, or framework it&#x27;s the terminal.</i><p>Sounds like the author never heard of Windows development.
Graziano_Mover 8 years ago
&gt; Now that we know we don&#x27;t need the up and down arrow keys, what about the left and right? Unfortunately, these keys are still needed for single character movements...<p>What? No. Use Ctrl-f and Ctrl-b. I use this probably more than any other readline shortcut.<p>Ctrl-h for backspace, Ctrl-d for delete. Half my keyboards don&#x27;t even have arrow keys and I don&#x27;t miss them.
teddyhover 8 years ago
&gt; <i>8. alt-w - delete the word behind of the cursor</i><p>He means “ctrl-w”. But since that only works in bash, not in Emacs or other tools with Emacs key bindings, it makes more sense to use (in his terminology) “alt-backspace”. This does the same thing, and works <i>both</i> in the shell and in Emacs-like environments.
评论 #13401367 未加载
评论 #13400901 未加载
评论 #13404120 未加载
评论 #13400685 未加载
nurbover 8 years ago
Another trick is to use &quot;!!&quot; which is the last command launched, especially useful if you forgot a &quot;sudo&quot; in front of your command, just write<p><pre><code> sudo !! </code></pre> In the same way, &quot;!*&quot; is all the arguments of your previous command, and &quot;!$&quot; only the last one.
评论 #13404037 未加载
RJIb8RBYxzAMX9uover 8 years ago
Regarding history, another simple trick is to increase $HISTSIZE and $HISTFILESIZE, for the defaults are woefully tiny for 2017. I set mine to a million. You could have 100 terminals open and they would still consume less RAM than &lt;insert &quot;native&quot; Electron app(s) of your choice&gt;.
wodenokotoover 8 years ago
Personally I found &quot;Learn Enough Command Line to Be Dangerous&quot; to be really helpful to get started with actually using the command line at all.<p><a href="https:&#x2F;&#x2F;www.learnenough.com&#x2F;command-line-tutorial" rel="nofollow">https:&#x2F;&#x2F;www.learnenough.com&#x2F;command-line-tutorial</a>
ivanhoeover 8 years ago
It&#x27;s missing my favourite: CTRL + _ for Undo.<p>Also there&#x27;s a big difference between Alt + Backspc and Ctrl + w. The first will delete a word consisting of only alphanumerics, while Ctrl + w also deletes the word, but word can be made of any characters other than space.
mr_donkover 8 years ago
&gt; Now that we know we don&#x27;t need the up and down arrow keys, what about the left and right? Unfortunately, these keys are still needed for single character movements<p>Don&#x27;t ctrl-f and ctrl-b work?
rofrolover 8 years ago
I would suggest <a href="https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep</a> over ag
ndesaulniersover 8 years ago
When going past something with ctrl+r, use backspace rather than ctrl+s. less has movements similar to vim. In less, hit v to open the file in your editor.
anneteeover 8 years ago
Also:<p>Alt-l converts next word to lowercase<p>Alt-u converts next work to uppercase
thiagofover 8 years ago
A very usefull command is Alt-m. This command repeat the last digited string in the current line. It only works in ZSH.
cel1neover 8 years ago
Tipp for OSX-Terminal:<p>Option + Cursor-Left&#x2F;Cursor-Right to jump words
rerxover 8 years ago
I never knew of `cd -` before -- very useful!
philonoistover 8 years ago
Kindly help me out on how to learn this tool in Windows10 without using VM?
greenspotover 8 years ago
tl:dr<p><pre><code> ctrl-r search in history, ctrl-r again to skip- ctrl-p previous command (instead of arrow up) ctri-n</code></pre>
lisivkaover 8 years ago
I recommend to install mc (Midnigth Commander) and bash-completion (if bash is your shell of choice) as first terminal tools.<p>mc allows to explore system efficiently while not standing in my way, because I can always press ctrl-O and get my shell back.<p>bash-completion saves time on typing of commands.<p>Other tools I install often are htop (better ps) and strace.
评论 #13400805 未加载
krzykover 8 years ago
It would be good to mention that this article is MacOS centric.
评论 #13401233 未加载
SFJulieover 8 years ago
Seriously you can get on fi(r)st page of HN with 5% of man bash?
评论 #13401791 未加载
评论 #13401851 未加载