

Ask HN: Your favorite practical, underutilized programming tip? - dpmehta02

Example:<p>Tip: Bash reverse search, because manually scrolling through your history is a nuisance.<p>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 &quot;\C-t&quot;:forward-search-history
======
pjungwir
Just 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
Bare 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 :)

------
pjungwir

        rails g migration add_foo_to_bar
        vi db/migrate/*!$*
    

or

    
    
        bundle exec !!
    

or

    
    
        rake db:migrate
        RAILS_ENV=test !!

~~~
pestaa
It'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 !!.

------
arh68
I'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)

------
loumf
1\. 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
if 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?!

~~~
creature
You can use g; in vim to go back to the last edited place. It's using the
changelist to navigate; see :changes for a list.

~~~
zem
nice, thanks! it amazes me how much there is to learn about vim, and how
useful some of the little random features are.

------
oweiler
Define 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).

------
palcu
In 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.

------
mesozoic

      continue;

