jemnotesversion 2 / featuring use-this-cmd or see all/search

Jun 6
From here come some interesting hints.
  • In bash, type alt + ., or esc + . to append the last argument from the last command. Repeat to go back up the command stack. You can also use alt + 1 alt + . to get the first command, etc.
  • Use du to summarize disk usage for directories only, via du -h –max-depth=1.
  • Use readline to edit a command in an editor: ctrl-x ctrl-e. Make sure you set $EDITOR. Can also use fc (fix command) to do this for the last command.
  • To create or wipe a file, use > filename. Compare: touch filename.
  • Quickly look at ascii character codes with man ascii.
  • Nice simple stopwatch with time read (then enter). Or for a countdown timer, read -t 5 for 5 seconds.
  • Use pushd /path/to/dir instead of cd, then popd to get back.
  • Use alt + b, alt + f to move forwards/ by a word. You can use numeric arguments as well.
  • Kill a word backwards with ctrl + w, then ctrl + y to yank. Rotate kill ring with alt + y. Kill word forwards with alt + d.
  • Kill backwards to the start of the line with ctrl + u.
  • Use alt + t to transpose the last two words.
  • alt + \ will remove all whitespace around the cursor.
A nice trick to copy your ssh key to a remote machine:
ssh remote-machine 'cat >> .ssh/authorized_keys' < .ssh/identity.pub
Tunnel ssh via an intermediate host, or otherwise use a pseudo-tty:
ssh -t reachable_host ssh unreachable_host
Jan 29
I use this constantly to disable beeps produced by X. They usually come through loud and painful through my headphones. Even matlab’s massive gui produces beeps that are fixed by this. Just type this in any terminal.
xset b off
Jan 26
If a file is stored with ANSI color escape codes, you can page the file and preserve the colors using
ls -R filename
Jan 14
For example, to see functions defined in the matlab library libeng,
nm ./bin/maci/libeng.dylib
Nov 28
I couldn’t get script/console to load the right version of Ruby. Turns out you can use
script/console --irb=irb1.9
Oct 17
Some nice shortcuts from here.
cd -                  # go back to previous directory.
pushd /new/directory  # go to new dir; add current dir to stack.
popd                  # retrieve dir from stack; go there.
Sep 23
Download a file, much like wget, but with curl:
curl -O http://example.com/file.sty
# equivalent to
wget http://example.com/file.sty
This is especially appropriate on mac, because curl comes with mac, unlike wget.
Sep 15
Converting a pdf to a png and making it 1000x1000 pixels:
convert -density 300x300 -resize 1000x1000 infile.pdf outfile.png
Sep 11
Change the hostname reported by mac, permanently:
sudo scutil --set HostName servername.example.com
Jun 19
Sick of messages like this when logging in?
Last login: Thu May 21 23:31:00 2009
Simply
touch .hushlogin
and they’ll be all gone.
May 7
For some reason, Terminal.app appeared to be completely ignoring my request to disable the audible bell. This worked perfectly instead: xset b off.
Mar 9
To immediately view the old version of a file tracked by git, use, for example,
git show HEAD^:filename
Mar 3
To create a new group on mac with a specific gid, then add a user, type
dseditgroup -o create -i 499 groupname
dseditgroup -o edit -a username groupname
Jan 30
Let’s say we want to remove the prefix one from a series of files. Use this:
for FILE in one* ; do mv $FILE `echo $FILE | sed 's/one//'` ; done
Or, use +rename+. Thanks to the reader who pointed out that, in Debian GNU Linux (for example), you can also use rename, as in
rename 's/^one//' one*
Much cleaner, if rename is available.
Nov 6
I was shown an optimization solver that ran fine with certain parameter sizes, yet started segfaulting with larger problem sizes. It turned out that a desired single large, amorphous array could not be allocated within the available stack size limit. Increasing the stack size limit via ulimit -s 50000 (for example) fixed the problem.
You can view a shell’s resource limits using, for example,
> ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) 6144
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 266
virtual memory          (kbytes, -v) unlimited
Sep 2
Expire a user’s password, requiring them to change it immediately upon their next login:
passwd -e username
Sep 2
Leopard irritatingly prevents you from creating an ssh tunnel to another computer and then connecting a samba share to your localhost. Get around this by first creating an alias for your localhost interface:
sudo ifconfig lo0 alias 127.0.0.2 up
Then, forward the ports via ssh, eg with
sudo ssh -N -L 127.0.0.2:139:localhost:139 -L 127.0.0.2:445:localhost:445 user@server.com
Finally, connect using Finder to smb://127.0.0.2/sharename.
Aug 21
Check for common memory errors in C with valgrind. Also profile cache misses and branch mispredictions.
Sample usage:
# Check memory accesses.
valgrind --tool=memcheck ./prog

# View cache and branch profiling information.
valgrind --tool=cachegrind ./prog
For more detailed output from valgrind, add the -g tag to your gcc compilation flags to add debugging information to the binary.
Aug 11
A reminder of how tee works comes from hunterdavis.com.
./prog | tee >(gzip > logfile.tgz)
Aug 11
find and sed operate slightly differently on a mac. Here’s some code that replaces 4.3 with 4.4 in all Makefiles, recursively.
find . -name 'Makefile' -exec sed -i -e 's/4\.3/4.4/g' {} \;
Jul 29
I’ve often tried to use find, but have never really got over its syntax. It seems that it’s one of those powerful tools that’s not necessarily obvious to use at first.
Here’s one way to use it. This:
find . -name '*.sw*' -print
prints filename which have .sw in them (say, leftover vim swapfiles).
You can combine this with grepping. This:
find . -name "*.bib" -exec echo {}; grep "tribute to" '{}'
looks for the words ‘tribute to’ in files with the extension .bib.