Run last command as root
blog.hardikr.com
blog.hardikr.com
!$ takes the last argument from the previous command, instead of the whole thing.
chmod 664 site/* - changes the permissions for all the files in the site directory. * is a wildcard in bash for specifying directories.
Another useful feature of bash is brace expansion: mv site{,-old} => mv site site-old The brace expansion expands the argument with the values in a comma separated list: college pic_00{1,4,3}.jpeg college.jpeg yields pic_001 pic_004, pic_003
> ls -a tmp
file1
file2
.file3
> ls tmp/*
file1
file2
> ls tmp/.*
.file3Just run mcd foobar and you (try) to create the directory and enter it. IMO a nice shortcut.
mcd dir1 dir2 dir3
That function will run: mkdir "dir1" "dir2" "dir3" cd "dir1" "dir2" "dir3"
(Surrounding $@ with double quotes, puts quotes around each item instead of quotes around the whole string: command "directory1/sub directory" "directory2"
$@ => directory1/sub directory directory2
"$@" => "directory1/sub directory" "directory2"
"$*" => "directory1/sub directory directory2"
) man ssh | grep -A 30 tildeIt's usually because I've accidentally fumbled ctrl-s which means 'play dead'. Why that's important enough to take a two-key home row shortcut, I don't know. The cure is to press ctrl-q. Same wonder about the cure.
"sudo make me a sandwich"
man bash, and search for "^HISTORY EXPANSION" for all the details.
I often find myself running commands like
cp !:2 !:1 # reverses the order of the last two args
cd /somedir && !! # run the last command in a better directory
It's pretty much the same number of keystrokes aside from having to hold down the Ctrl key. However, I can now go back in my history and run the command again. Also, if I mistyped something, I can easily hit Ctrl-P and fix it without having to retype sudo.
ls ./t*
# looks good...
rm !$This often works in unexpected places. For example: testing it right now, I can click the address bar on google chrome, hit ctrl-a (not cmd-a) and the caret is at the start of the url.
BTW, when using screen, ctrl-a is its default action key, so use ctrl-a ctrl-a to send a real ctrl-a, or change the default screen action key in your .screenrc.
This ends up annoying me on a regular basis. Let's say I want to make a directory and then go to it. I would type:
mkdir foo<Ctrl-p><Ctrl-a><Alt-d>cd<Enter>
Except when using screen, <Ctrl-a><Alt-d> (really just <Ctrl-a>d) detaches the session. It's solved easily enough ("screen -r"), but it always yanks me out of my groove. escape "^^^^"
That will set the escape character to ctrl-carat instead of ctrl-a. To type a literal ctrl-carat, type it twice.This is the same as:
screen -e ^^^^ `q
Now I don't even have to use Ctrl. This takes a little getting used to, but I prefer to use $() instead of `` for shell scripts (I use bash/zsh) so I don't make use of ` that often.I do have bindings on F11 and F12 to toggle the escape character between ` and C-o. (Just in case I need to paste something into a terminal with ` characters.)
(Note: This whole setup is ripped off from some StackOverflow or HN post.)
I use ctrl-a and ctrl-e too, but sometimes you're using remote desktop (or synergy) with a Windows machine...
It works like this: You're doing 3 things over and over again, but you need to see each step finished before you want to go on. Something like:
>wget buildserver/artifact.jar >java -j artifact.jar > result.txt >grep "Stupid bug" result.txt >vi config.xml >git commit -a; git push
Something like that. Having done it once, up-arrow into your history to the wget, hit "Ctrl-O" and it'll execute the wget, and now the java command is ready. You can just keep cycling through with Ctrl-O, since executing the command puts it in the end of your history. If you need to update the command, the fix is available for later. One of those "use it so that you remember, 'cause boy-howdy it's handy"
sudo-command-line() {
[[ -z $BUFFER ]] && zle up-history
[[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
}
zle -N sudo-command-line
bindkey "^Os" sudo-command-line
into your zshrc, enter the command and press ctrl+o s et voila ;0For more cool stuff regarding zsh have a look at http://grml.org/zsh/ or for other nice keybindings http://grml.org/zsh/grmlzshrc.html
(I doesn't use read-line it has it's own line editor)
alias r='sudo '
Note the space after sudo in the quotes. Cuts down on keystrokes when you have to spend a lot of time sudo-ing.The reason is that you might not execute the command you were thinking of. The HISTIGNORE variable contains a quasi-regex of commands that are not stored in history. If the previous command matched that regex, you will end up executing the previous command to that one, not what you wanted at all.
Why use HISTIGNORE?. So that things like 'exit' or common commands like 'ls' don't pollute your history file.
Ranking = (votes)^(1/time) with some other bells an whistles.
I really, really, really hope that the author doesn't think "sudo !!" is some special kind of magic, and that he knows "echo !!" works the same way.
Also, there are keyboards with a "Home" key? Most people just press C-a.
grep ".jpg"
^C
ls | !!
...that worked too. Cool command to learn.
Thanks for the clarification, though. Original article was lacking.
Edit: Typos
# Insert double quotes & set cursor between them
"\C-x\"": "\"\"\eb"
#
# Insert single quotes & set cursor between them
"\C-x'": "''\eb"
See also the Bash manual:http://www.gnu.org/software/bash/manual/bashref.html#Command...
C-a beginning of line
C-k kill text from cursor to end of line
C-e end of line
M-b move cursor backwards by a word
M-f move cursor forwards by a word
C-w delete backwards by a word
C-h backspace (great when real backspace doesn't work)
C-r reverse search through command history
(C-w works in Vim(insert mode) and Emacs, but kills me when I try to type it in a browser window to delete a word... Good-bye window and/or tab)In zsh I actually have M-w bound to the normal backspace-by-a-word and C-w bound to the vim version which will do things like count '/' as a word separator so that I can easily backspace through sections of a path rather than be required to delete the whole thing (or nothing).
It is the NSTextField that accepts those commands. The prefix NS stands for NeXTSTEP, so the functionality existed long before Cocoa.
Control-Shift-T usually solves that problem for me in Firefox.