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