Your Bash Prompt Needs This
jonisalonen.com
jonisalonen.com
The simpler fix is just to notice that your prompt has shifted over (it's almost impossible not to notice) and just hit return before doing anything.
zsh prompt:
PROMPT='
%{$prompt_default_color%} %~ %* %{${fg[$prompt_user]}%}%n%{$prompt_default_color%}@%{${fg[$prompt_host]}%}%M%{$prompt_default_color%} ${vcs_info_msg_0_}_
$ %{${fg[default]}%}'
It seems like most of the features listed as better than bash are either a) covered by bash 4.0, b) bad ideas (shared history among sessions), or c) only controversially better (searching around, people seem to argue back/forth about which programmable completion implementation is faster/better).
1) it's a drop in replacement for bash. All those scripts you have around have #!/bin/bash at the top, they will still work exactly as they always have.
2) You can selectively go back through your command history. You can arrow-up to go to previous commands just like in bash. But if you start a command (say, ssh) and then arrow-up you will then be cycling back through the last ways you entered that command. Super convenient.
Like Ctrl+R?
Or, if you'd like to do this in Bash, and any program that uses readline: In ~/.inputrc
# Bind the up arrow to history search, instead of history step
"\e[A": history-search-backward
# And the reverse (down)
"\e[B": history-search-forward git clone
git rebase master
git log
in my history, if I typed git r<UP>
I get 'git log', not 'git rebase master' as I'd expect. bindkey "^[[A" history-beginning-search-backwardIf you don't want to use it, I don't see how that is any of my business. I was just trying to help out somebody who does, so there's hardly any reason to be an ass.
All I'm saying is that most people won't like something that offers no immediate major benefit and has annoying defaults that they have to find an obscure setting to fix. This is probably why so few use emacs, even though it is awesome :)
Of course, a script that has #!/bin/bash at the top will keep working regardless of which shell you execute it on anyway, even a shell with a completely different syntax such as tcsh, because that shebang line will tell the OS to always use bash to execute the script--no matter what shell you're using to invoke the script.
It's different when you source a script (usually done using the eponymous shell built-in) since then the shebang line will be treated as a comment and ignored, and all other lines in the script will be executed in the context of the current shell.
1. Learn bash better. Lots of low-hanging fruit there to increase your productivity with minimal effort.
2. Learn how to customize your environment in bash. Know what every line in your dotfiles does. Add to your dotfiles gradually as you learn.
3. If you learn about some cool features in zsh, see if bash has the feature already. If it doesn't, see if you can mimic it easily with your own functions or scripts.
4. If you still want more features, make the switch to zsh and repeat steps 1 and 2 with zsh.
I think the worst is when people see a novel trick and switch to zsh and slam in oh-my-zsh* without knowing any shell basics. Now you have another shell and a big pile of code and you don't understand any of it. This usually won't cause problems, but you almost certainly won't be getting the most out of your shell. You'd be better off in most cases just digging into bash a little deeper.
I use zsh now, but I only switched after learning what bash could do and what its limitations are. I'll reevaluate zsh vs. bash 4.0 at some point, but for now I see zsh installed by default more often than bash 4.0 and it seems to me that most users interested in the best interactive shell experience are still on zsh and I like to learn from them.
* - I think oh-my-zsh is a great project - I'm not knocking it. I've borrowed lots of great stuff from it. But I think you should understand every line of code you are dumping into your environment, whether you are using oh-my-zsh or not.
While a senior in high school (~15 years ago), I was also enrolled at the local university and taking a few 1000- and 2000-level Math and CS courses. About halfway into the spring semester, my account on the CS department's crusty old Sun box was locked, and my CS professor that semester told me to report to the system administrator's office after class.
When I arrived at the system administrator's office, the department's system administrator and the department head were waiting for me. The sysadmin immediately (and angrily) said that if this were anywhere but a university, he'd see that I was thrown in jail. When I asked what he was talking about, he turned to his computer, banged on the keyboard for a bit, and turned the massive CRT so I could read it.
On his screen was an e-mail I had sent him the semester before asking that he change my shell from /bin/csh to /usr/local/bin/zsh. (Solaris of that vintage didn't have chsh, so I couldn't do it myself.)
The sysadmin was absolutely convinced that zsh gave me "elevated system privileges", even though he was the one that changed my passwd entry and presumably was the one who compiled and installed zsh in the first place.
When I argued that /usr/local/bin/zsh didn't have the suid or sgid bits and therefore gave me no more access to the system than any other shell, the department head scolded me for arguing with the (experienced, wise) sysadmin.
The sysadmin changed my shell back to csh, and the department head made me sign a document stating that I wouldn't change my shell back to zsh or try to "further gain administrative access" to the system.
On the drive home, I idly wondered whether going to (a different!) college full-time in the fall was really such a hot idea...
You should have never signed that. Even if you struck the word "further", it'd probably be seen as an admission of guilt in court.
The little itch that drove me crazy was command-line editing: in bash (and all readline apps) when I have
command long/path/to/some/file
I can press Alt-Backspace to delete 'file', or I can press Ctrl-W to delete 'long/path/to/some/file'.I tried various hacks to make zsh do the same, then gave up.
bindkey '^[^?' backward-kill-word
might do the trick? (possibly '^[^h' depending on what your backspace key sends. Ctrl-v <backspace> should give you something you can copy)Edit: I know I can customize the word style using $WORDCHARS, or 'autoload select-word-style'; my problem is that I'm used to having the capability to use two different word styles at once.
I'm looking through the command list in zshzle(1), and I'm a bit confused about the differences between backward-delete-word and backward-kill-word. Some short experiments I did failed to notice any differences (foo/bar/baz M-x backward-delete-word deletes foo/bar/baz). There's also vi-backward-kill-word which deletes 'baz', then '/', then 'bar', then '/', then 'foo'. I want a key to delete 'baz', then 'bar/', then 'foo', and I've no idea how to get it...
The other alternative is to try:
autoload -U select-word-type
select-word-type bash
which should override C-w to do what you want. I haven't looked at it enough to see if you can get both behaviours simultaneously though (and it requires a fairly recent zsh iirc)Internet Explorer is the default on Windows because Microsoft makes it, not because it's the best option for a default.
Bash is the default because it's the best shell to be used as a default. Nobody is twisting the arm of any distribution, but they set it as the default anyway. There's a reason for that.
How often is this decision re-evaluated?
...personally I just hit Ctrl+C again to reset my prompt.
Also, because it's GNU. Just as IE is the Microsoft browser, bash is the GNU shell, and most Linux distributions are built around GNU userland.
But that's just Linux. The BSDs don't see bash as the best default, where it tends to only be available as an optional package.
Bash has come a long way since then and it's just a "brew install bash" away. There's absolutely no good reason to stick with the version that ships with OSX for your personal shell.
I'm not sure which zsh OSX ships with, but it should be much more up to date since it uses a BSD-like license.
"Legally capable" is one thing, "completely out of any possible problems in the future" is another.
GPL3 is toxic.
1) Patents, i.e. you can't circumvent GPL3 by using GPL software as something you build your software on, but then don't share it because while source code may be available, you patent the code. It was possible with GPL2.
2) DMCA, i.e. you can't circumvent GPL3 with using DMCA which makes circumventing DRM illegal in US. It was possible with GPL2.
3) Tivoization, i.e. you can't circumvent GPL3 using digital encryption, when you release the source code, but not the necessary cryptographic keys.
In other ways, GPL3 is pretty much the same as GPL2. Using GPL3 bash would not make Apple release source code of Mac OS X any more than using GPL2 bash. The only reason not to use GPL3 (1) is when you don't want the three things I listed above taken from you. I would suggest that makes any company with "no GPL3" policy toxic, not GPL3.
Should you want to educate yourself, here's a link that explains it pretty well: http://www.linuxfordevices.com/files/misc/GPLv2_vs_GPLv3.pdf
(1): It's very different with AGPL
Power beyond need or capability is complexity (the bad kind).
Part of this is because I try to automate anything I do more than once or twice and I use a real language to do so. For anything non-trivial the differences between zsh and bash are an order of magnitude less significant than either compared with Python, Ruby, etc.
In brief, bash, like mysql (or Windows) is always there. Most people know how to use bash and wouldn't notice any difference by switching (except for a possible unnecessary learning curve).
Lets turn it around. Assuming (and a big assumption) that zsh is more powerful, why does this matter to someone who probably spends most of their time at a terminal thinking and puts anything half way repetitive into a script? Its like telling a carpenter than a brand X chisel is more powerful than a brand Y chisel.
Show me how a skilled zsh user is 'more powerful' than a skilled bash user at anything that matters. Perhap some people spend too much time typing and not enough time thinking.
cout >> "Hello world"; // with no endl.
Prepending the prompt with a new line might be a better idea than over-writing the current line. PS1="\[\e[32;1m\]\[\e[37;1m\]\`if [ \$? = 0 ]; then echo \[\e[33m\] :\)\[\e[0m\]; else echo \[\e[31m\] X\(\[\e[0m\]; fi\`\[\e[32;1m\] \[\e[32;1m\](\[\e[37;1m\]\h:\u\[\e[32;1m\])-(\[\e[37;1m\]\j\[\e[32;1m\])-(\[\e[37;1m\]\w\[\e[32;1m\])\n\[\e[0m\]"
Here's how it appears: :) (seas456:narayana)-(0)-(~/personal)
[this line is where your cursor blinks; notice no symbol at the start of this line]
You can't see the colors here. The smiley face turns into a bright red frowny face if the exit code of the last job is nonzero. [\[\033[1;32m\]\D{%Y-%m-%d %H:%M:%S}\[\033[0m\]] \[\033[1;32m\]\u\[\033[0m\]\[\033[1;32m\]@\h\[\033[0m\]:\[\033[1;36m\]\w\[\033[0m\]\n#
It's actually dynamically generated by my .bashrc so that different servers have different colors; at a glance, I can generally tell whether I'm on one of my machines, or on a work build server, or an amazon ec2 instance.https://github.com/dylanahsmith/dotfiles/blob/master/.config...
specifically your function just sort of sits there doing nothing when i try it....
This does break if the terminal has auto wrap turned off (echo -e "\033[?7l"), but otherwise it's very clever.
rpatterson@rpatterson-tba 04/27/2012 01:24 PM ~
$
This gives me a full row to type a command, and lets me see how long ago the last command finished. Each of those components is bold and colored differently. [prakash@kaveri 16:39:05 ~ :) ]
>> sleep 10
^C
[prakash@kaveri 16:39:18 ~ :( ] PS1="\[\033[G\]$PS1"
instead of $PS1="\[\033[G\]$PS1"A) When I hit ^C, I am always seeing a newline occur after the ^C and before my prompt is redrawn. I have tried this with cat (blocked on read) and ssh (blocked on password).
B) When I purposely attempt to cause this (using echo -n and interpreting "a very long command" to mean "long enough that it wrapped to the next line"), hitting up redraws and overwrites part of the prompt in order to guarantee that the position of the prompt will edit correctly.
But terminal corruption used to be far more common (line noise, etc...) and bash has a built-in escape for this. Ctrl-L (chosen for the analogy with form-feed/"vertical tab" on a printer) will blank the screen and redraw your current edit environment at the top.
BTW, it's the readline library that handles the Ctrl-L key binding to clear the screen rather than bash per se.
$ bind -p | grep '\\C-l'
"\C-l": clear-screen
$It doesn't really help me "maximise my processor usage" I find.
Do I have this right?
I just went through the following steps recently:
1) Put more things in the background with '&' - but if they have a lot of output they make your terminal usuable.
2) I could ' >t 2&>1 & ' , but then I have to remember to check t when the process finishes, and end up with lots of tiny files everywhere.
3) I could just send output to /dev/null , but then maybe I care what it was if the process fails.
alias myfunc "errorchecker >& /dev/null"
[command] | myfunc
What it does with those errors is up to you, though your main options are "print them" (in which case, just don't redirect stderr), "log them for fetching" (and write a little command to access & manage the failure reports), or "email them to yourself".I guess basically what I'm getting at is, look at distributed processing backends such as LSF or GRD for inspiration.
Or maybe opens them in $EDITOR...like this (I can't get it to work when $* has a pipe in it, unfortunately)
#!/bin/bash
out1=`tempfile -d $HOME/logs`
out2=`tempfile -d $HOME/logs`
$* >$out1 2>$out2
$EDITOR $out1 $out2 # Replace this with call to notification app and/or logic based on $?/size of $out2the path to "user-friendly" shell nirvana (i.e. interactive use, not scripts) can be achieved through use of a library like editline, without installing a huge, "kitchen sink" package like zsh... if size is an issue. make the shell cmd line behave like the editor you know well.
But the first $ is wrong, it should read PS1="\[\033[G\]$PS1"
export PS1="\[\033[G\]$PS1" PS1='\['"$(tput cr)$(tput ed)"'\]$ '
is better since it clears to the end of the screen, e.g. try seq 1000 | fmt; tput cup 10 20
with and without it.But the thing is, with either that approach or the one in the article, you're wiping out what may be useful; that unfinished line causing PS1 to be pushed along may have vital data on it, data that's hard to re-create.
Worth double-checking, anyway. I've occasionally pondered some sort of prompt-lint/validator given the reams of dubious examples out there, and problems people face when customising their own.
turns out i'm willing to tolerate a lot of pain to have emacs operations in a shell buffer, haha
As far as colors go, shell mode does its own highlighting. I used to have problems with ANSI code colors, but they have been working properly recently. (I just tried ls --color=always which worked as expected, unexpectedly.)
Which version of Emacs are you on?
Note that this is all without any configuration. The only thing I did was add a command for opening a new shell in the current directory which turned out to be extremely useful. It even works for remote files, so now I use shell mode for almost everything.
(one of the readline features i rediscover every three months or so and promptly forget)
$if lisp
set comment-begin ;
M-;: insert-comment
$endifI set mine to standard for successful output "[user@host:truncated path]$ ".
Having my shell smile at me all the time makes me think it's up to something.
export PS1="\n\W\$(parse_git_branch) $ "
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'; }Is there any way of getting bash to only output a '\n' if the cursor isn't already at the start of a line?
$ echo -n foo bar
zob ~ ⚡ cat
^C zob ~ ⚡
default bash settings real termina, like xterm this is bash of course.
and its better than ^C is not erased.
saurik ~ ö bob
saurik ~ ö cat
^C
saurik ~ öthe lightning bolt is just an utf8 fancy char
this is my PS1 if you like it: \[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;33m\]⚡\[\e[m\] \[\e[1;37m\]