7 ms·
Ask HN: Your favorite practical, underutilized programming tip?
Example:
Tip: Bash reverse search, because manually scrolling through your history is a nuisance.
How (OS X): CTRL+R, start typing a word from your command history. Press CTRL+R to cycle through matches, ENTER to execute, TAB to edit. If you want to add forward search to bash, you can re-map your key bindings in your bash profile with the following (maps to CTRL+T): bind "\C-t":forward-search-history
- pjungwir 12y agorails g migration add_foo_to_bar vi db/migrate/*!$* or bundle exec !! or rake db:migrate RAILS_ENV=test !!
- pestaa 12y agoIt's shell syntax. !! is replaced by the whole previous command, !$ is replaced by the last argument in the previous command. There are tons of others, but these two I use the most. Feels great to yell sudo !!.
- pjungwir 12y agoJust stumbled on this one recently, I forget where: foo | awk '{ print $NF }' That prints the last column. $NF = number of fields. I use it a lot like this: git status | grep deleted: | awk '{ print $NF }' | xargs foo I actually just realized, though, that I don't understand why this works. It seems like it should be '{ print ${$NF} }' or something, and the command as-given should just print "3" or "6" or whatever. I'm not an awk expert, so I'd love to understand what's going on here.
- pbnjay 12y agoBare NF is number of fields, so the $ must be pulling the value first. Also, awk is 1-based, not 0, so you get the last field :)
- drothlis 12y agocat > asdf.txt <<EOF a b c d e f g h i j EOF cat asdf.txt | awk '{print $1}' cat asdf.txt | awk '{print $2}' cat asdf.txt | awk '{print $(3-1)}' cat asdf.txt | awk '{print NF}' cat asdf.txt | awk '{print $NF}'
- stevekemp 12y agoThis always bugs me: git status | grep deleted: | awk '{ print $NF }' | xargs foo Instead of using both grep and awk just use awk: git status | awk '/deleted:/ {print $NF}' | xargs foo
- mesozoic 12y agocontinue;
- loumf 12y ago1. Using a copy-paste manager to get more than 1 buffer (like Keyboard Maestro, but there are free ones) 2. In a lot of editors (not vi), if you need to get back to the point of your last edit (because you scrolled away) use the keyboard shortcuts for UNDO, REDO. So in a lot of editors, CTRL-Z, SHIFT-CTRL-Z -- this jumps you right back to where you last changed something.
- zem 12y agoif by vi you mean vim, this works perfectly (and indeed i use it a lot; it's more reliable than hitting ''). if by vi you mean justplain vi, then why are you not using vim in the first place?!
- palcu 12y agoIn Sublime Text, you can put bookmarks in code using Cmd+F2 and navigate between them using F2. It's really helpful if you work with big files.
- arh68 12y agoI'll cheat and throw out a tip I haven't used yet: apparently if you read man bash long enough, you get to the built-in TCP/UDP sockets! [1] bash$ cat </dev/tcp/time.nist.gov/13 53082 04-03-18 04:26:54 68 0 0 502.3 UTC(NIST) * [1] http://hacktux.com/bash/socket http://hacktux.com/bash/socket
- oweiler 12y agoDefine an old function in your .bashrc old() { mv "$1"{,.old} } I use this as a cheap way to backup old files (alternatively use cp instead of mv).