GEEK: talmudic notes in vim?

When reading new code, especially code in a language I’m unfamiliar with, I sometimes have to translate each symbol/method very methodically in order to understand what the code is trying to do. Often, I’ll put my notes in the code as comments. While this helps me, this can make the code more bulky and difficult to read for those who are familiar with the code/language. Ideally, I’d like a vim plugin in which I can keep notes on a file, without actually changing it. Anyone know of such a plugin?

GEEK: Serial key recovery on Mac OS X?

Anyone know how to recover the serial numbers for software installed on a Mac? I have some software installed on one Mac, which I would like to install on another Mac. However, I’ve since lost the documentation and serial number. There are several programs that recover serial numbers from installed software on Windows, but none that I’ve found on the Mac side. Any suggestions?

Thanks!

GEEK: tips collection

This post is for small snippets of code that don’t warrant a post of their own.

Use imagemagick’s convert command to reduce an image to 640×427

convert -geometry 640x427 -quality 75 _MG_9954.thumb.jpg _MG_9954.reduced.thumb.jpg

Copy a file located on a remote machine (target) to the local directory (.) via an ssh tunnel through a firewall machine (firewall).

By default, rsync will delete any partially transferred file if the transfer is interrupted. Using the –partial option tells rsync to keep the partial file which shouldmake a subsequent transfer of the rest of the file much faster.

The –append option causes rsync to update a file by appending data onto the end of the file, which presumes that the data that already exists on the receiving side is identical with the start of the file on the sending side. Note that the append option isn’t present in rsync v2.6.3 and below.

rsync -av –partial –append -e “ssh firewall ssh” user@target:/Users/crasch/file .

Find any files in the current directory and below which have 2009050 in the filename, and save them as a gzipped tar file in the /Users/crasch home directory:

sudo find . -type f -name ‘*2009050*’ -print | xargs tar -zcvf /Users/crasch/`date ‘+%d%m%Y_archive4.tgz’`

cat /etc/resolv.conf will display the IP addresses of nameservers available to a host.

To execute a sql query save in a file named [name].sql from within sql92 (the FrontBase command line):

script ‘[name].sql’;

screen -S allows you to name a session (“screen -S processes”)
screen -x allows you to attach to a session (“screen -x processes”)
screen -r allows you to attach to someone else’s screen session (“screen -r aks/”)

To set the title of a terminal to the name of the file being edited in vim, add the following to your .vimrc:

set titlestring=%t%(\ %M%)\ \ \ \ %{hostname()}

if &term == “screen”
set t_ts=^[k
set t_fs=^[\
set titlestring=%t
endif

if &term == "screen" || &term == "xterm" || &term == "xterm-color"
set title
endif

To set the name of the terminal to the name of your current directory, add the following to your .bashrc file:

case $TERM in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;[`basename ${PWD}`]\007″‘
;;
screen*)
PROMPT_COMMAND=’echo -ne “\033k\033\134\033k[`basename ${PWD}`]\033\134″‘
;;
*)
;;
esac


Search and replace in vim:

File contains:

abcabc
abcabc
abcabc

Assuming cursor is at that beginning of the first line:

:s/a/X/ – “replace first a on the current line”:

Xbcabc
abcabc
abcabc

:s/a/X/g – “replace all a’s on the current line”:

XbcXbc
abcabc
abcabc

:2,$s/a/X/ – “from line 2 to end replace all first found a’s on line”:

abcabc
Xbcabc
Xbcabc

:2 s/a/X/ – “replace first A on line 2″

How to change the default shell from tcsh to bash on Mac OS X:

niutil -createprop . /users/crasch shell /bin/bash

abcabc
Xbcabc
abcabc

Line ranges are:
only on line where cursor is on.
5,15 => line 5 up to and including line 15
2,$ => line 2 to end
.,$ => from line where cursor is on to end.
% => whole file; short for 1,$ (or 0,$ if you like)

Display non-printing characters

:set list

Set tabstop to 8:

:set ts=8

Search for tabs:

/\t

When key is pressed, insert a series of spaces:

set expandtab

Setting ‘expandtab’ does not affect any existing tabs. In other words, any
tabs in the document remain tabs. To convert existing ‘s to spaces:

:set expandtab
:%retab

Now Vim will have changed all indents to use spaces instead of tabs. However,
all tabs that come after a non-blank character are kept. If you want these to
be converted as well, add a !:

:%retab!

This is a little bit dangerous, because it can also change tabs inside a
string. To check if these exist, you could use this:

/”[^"\t]*\t[^"]*”

It’s recommended not to use hard tabs inside a string. Replace them with
“\t” to avoid trouble.

The other way around works just as well:

:set noexpandtab
:%retab!

Here’s the trick: First, set your vi(m) session to allow pattern matching with special characters (ie: newline). It’s probably worth putting this line in your .vimrc or .exrc file.

:set magic

Next, do:

:s/,/,^M/g

To get the ^M character, type ctrl-v and hit enter. Under Windows, do ctrl-q enter.

\r matches

\n matches an end-of-line – When matching in a string instead of buffer text a literal newline character is matched.

Format the output of a FrontBase sql query:

set format ‘%#,%0c,%1c,%2c,%3c’;

Perl recursive, in-place search and replace:

find /path/to/start/from/ -type f | xargs perl -pi -e ‘s/applicationX/applicationY/g’

How to pass the debug flag to xcodebuild:

xcodebuild -project testtool.xcodeproj clean && xcodebuild -project testtool.xcodeproj ‘OTHER_CPLUSPLUSFLAGS=-g’

How to start up gdb with a program that takes command line arguments:

gdb –args /Marketocracy/bin/testtool actionsFromDateToDate -key 51E82C2B3976FB66C0A801E0 -start 2001-01-01 -end 2009-06-22

How to execute an Objective-C command in GDB:

p [_outputField setStringValue:@”123456”]

How to set a breakpoint on a method that takes two arguments:

b –[NSArray removeObject:atIndex:]

Often, you will want to switch from one directory to another and back again. To make it easier to switch and return, use a directory stock.

To push a directory onto the stack, type:

pushd ~/Projects/trunk/lib/ruby/

To display the directories in the directory stack:

dirs -v

…which should display all of the directories in the directory stack:

$ dirs -v
0 ~/Projects/trunk/lib/ruby/Marketocracy
1 ~/Projects/trunk/Scripts/build/stock-data

To swap the top two directories (directory 0 with directory 1), type pushd with no arguments.

To switch to a numbered directory, type pushd +[NUMBER]. For example, to switch to the directory #1 (~/Projects/trunk/Scripts/build/stock-data), type pushd +1.

To remove a directory from the stack

——–

How to uninstall all gems:

sudo gem uninstall –a –ignore-dependencies .+

How to uninstall a particularly long list of gems:

sudo gem uninstall –a –ignore-dependencies merb.+

——–

Via neeraj:

Where is ruby installed on my machine?

irb can help you.
view sourceprint?
1.> irb -rrbconfig
2.>> Config::CONFIG["bindir"]
3.=> “/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin”

A much easier way is:

> gem env
02.RubyGems Environment:
03. – RUBYGEMS VERSION: 1.3.1
04. – RUBY VERSION: 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
05. – INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
06. – RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
07. – EXECUTABLE DIRECTORY: /usr/bin
08. – RUBYGEMS PLATFORMS:
09. – ruby
10. – universal-darwin-9
11. – GEM PATHS:
12. – /Library/Ruby/Gems/1.8
13. – /Users/nkumar/.gem/ruby/1.8
14. – /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
15. – GEM CONFIGURATION:
16. – :update_sources => true
17. – :verbose => true
18. – :benchmark => false
19. – :backtrace => false
20. – :bulk_threshold => 1000
21. – :sources => ["http://gems.rubyforge.org/", "http://gems.github.com", "http://gems.github.com"]
22. – REMOTE SOURCES:
23. – http://gems.rubyforge.org/
24. – http://gems.github.com
25. – http://gems.github.com

If you are interested in finding where a particular gem is installed then run following command. It lists the location of the gem also.

> gem list -d rails
02.
03.*** LOCAL GEMS ***
04.
05.rails (2.2.2, 2.2.1, 2.1.2, 2.1.1, 2.1.0, 2.0.2, 2.0.1, 1.2.6, 1.2.5)
06. Author: David Heinemeier Hansson
07. Rubyforge: http://rubyforge.org/projects/rails
08. Homepage: http://www.rubyonrails.org
09. Installed at (2.2.2): /Library/Ruby/Gems/1.8
10. (2.2.1): /Library/Ruby/Gems/1.8
11. (2.1.2): /Library/Ruby/Gems/1.8
12. (2.1.1): /Library/Ruby/Gems/1.8
13. (2.1.0): /Library/Ruby/Gems/1.8
14. (2.0.2): /Library/Ruby/Gems/1.8
15. (2.0.1): /Library/Ruby/Gems/1.8
16. (1.2.6): /Library/Ruby/Gems/1.8
17. (1.2.5): /Library/Ruby/Gems/1.8

http://evaluation.nbu.bg/amitko/Library/O%27Reilly%2077%20Books/unix3/mac/ch01_05.htm

Mac OS X’s default shell, tcsh, lets you move your cursor around in the command line, editing the line as you type. There are two main modes for editing the command line, based on the two most commonly used text editors, Emacs and vi. Emacs mode is the default; you can switch between the modes with the following commands:

bindkey -e Select Emacs bindings
bindkey -v Select vi bindings

The main difference between the Emacs and vi bindings is that the Emacs bindings are modeless (i.e., they always work). With the vi bindings, you must switch between insert and command modes; different commands are useful in each mode. Additionally:

* Emacs mode is simpler; vi mode allows finer control.
* Emacs mode allows you to yank cut text and set a mark; vi mode does not.
* The command-history-searching capabilities of the two modes differ.

—–
To force Thunderbird to send all attachments as true attachments:

Click on Tools > Options.
Select the Advanced pannel and then the General tab.
Click on the Config Editor button.
Set mail.content_disposition_type to 1

GEEK: prune directories, files from find output

Sometimes you want to exclude a directory and/or files from a search. This alias:

cfind=’find . \( -type d -name .svn -prune -o -path ./stock-data/tests/data -prune \) -o \( -type f \! -name “*.csv” -print0 \) | xargs -0 grep’

…means:

“Recursively loop through all of the files and directories in the current directory and below. If it’s a directory named .svn or it’s in the stock-data path, or it ends in .csv ignore it. Otherwise, search for your keyword in the file.”

GEEK: what the directories on my camera’s memory card mean

Really boring stuff below, of likely interest only to me.

(more…)

GEEK: how to add a user to the admin group on Mac OS X

Here’s how to add a user to the admin group using dscl:

dscl . append /Groups/admin GroupMembership crasch

Remove a user:

dscl . delete /Groups/admin GroupMembership crasch

Reading the membership of the admin group:

dscl . read /Groups/admin GroupMembership

GEEK: Running a ruby script via crontab

So, I’m trying to set up a ruby script that screenscrapes another website, writes the results to a local file, then rsyncs the file to another machine behind a firewall. This post details the problems I ran into while trying to get cron to run the ruby script and rsync to the remote host.

(more…)

GEEK: nofollow tag

What if you want to link to a website for the purposes of discussion, but you don’t want to thereby boost the Google rank of the site? Add the no follow tag to the link. Google, MSN, and Yahoo have all announced that they will respect the tag, and won’t allow the link to boost the ranking of the linked site.

GEEK: nationwide craiglist search

Suppose you want to find a listing of all Craigslist ads featuring a 2005 Chevy Astro Vans with AWD between $5000 and $15000 dollars. You could do this search.

Craizedlist.org might also be of interest to you.

GEEK: shell script question

Suppose you want pass in a complex query to a command line sql interpreter from within a shell script. You can do so with a command like this:

sql92 < connect to db1 as LASTTRADES user eouser;
set format row;
select p.fundkey as fundkey,
max(t.closed) as last_trade
from mposition p, mtrade t
where p.primarykey=t.positionkey
and t.createdbycorporateaction=0
group by fundkey;
EOF

There’s a name for the syntax for the argument: <

But for the life of me, I can’t recall it, or find it on the web. Anyone know this off hand?