Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

TextMate - Convert Tabs to 2 Spaces and Strip Whitespace

Command for TextMate (or any other editor really) to strip whitespace and convert tabs...
I only ever use 2 spaces so i've gone with that.

#!/usr/bin/env ruby

STDIN.read.each do |line|
  line.sub!(/^(\t+)/) { |c| '  ' * c.length }
  line.sub!(/(\s+)$/, "\n")
  puts line
end

Single-Line commenting in Actionscript with Textmate

- open the bundle editor and select the Actionscript bundle
- use the add button to make a new preference item, give it a scope of source.actionscript
- name it whatever
- paste in the following:

{   shellVariables = (
        {   name = 'TM_COMMENT_START';
            value = '// ';
        },
    );
}


- that's it! got these instructions on IRC from Infininight: http://pastie.textmate.org/private/clmfldbv2sexjcd7u6qjw

TextMate snippet for load_model methods in Rails controllers

With nested routes, I find I create a lot of controller methods like:

def load_user
  @user = User.find(params[:user_id]) if params[:user_id]


Here's a TextMate snippet, so you can just type: defmodel, TAB, user, TAB, and you're done.

Snippet:
def load_${1:model}
	@$1 = ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g}.find(params[${2::$1_}id])${3: if params[:$1_id]}
end


Activation: defmodel
Scope: source.ruby.rails

I have this in the Ruby on Rails bundle, but you can put it anywhere. Its the scope that is important.

Git-diff last two commits through TextMate

I believe this is how svn diff operates... but anyway, this alias will allow you to diff the last two commits in Git through TextMate. If you have a theme that supports Diff's then you'll see the differences clearly. Remove the last pipe statement (the mate part) to expose your diff in the console.

alias gitdiff='git log|grep commit|cut -d " " -f2|head -n 2|xargs -n 2 git diff -R|mate'

Auto-populate socket value in rails database.yml using TextMate snippet


When using mysql for rails apps, you may need a
socket:
value. I can never remember mine. So I added a TextMate snippet to find it. (read below for non-TextMate)

Snippet text:

socket: `mysql_config --socket`


Activation:
socket:


Scope selector:
source.yaml - string


<h3>Usage:</h2>

In your database.yml, go to the line
socket: 
, delete any blank spaces til the cursor is at the colon, then press TAB and wait. The line will be updated with the socket location.


<h3>Non-TextMate</h3>

If you don't have textmate, you can get your socket location using:

mysql_config --socket


And paste the result into your database.yml

Obfuscate email addresses in TextMate

// Textmate / e command for obfuscating email addresses

#!/usr/bin/env ruby

email = STDIN.read
url_email = email.gsub(/./) { |c| '%' + c.unpack('H2' * c.size).join('%').upcase }
html_email = url_email[1..-1].split(/%/).collect { |c| sprintf("&#%03d;", c.to_i(16)) }.join

print "<a href=\"mailto:#{url_email}\">#{html_email}</a>"

Exclude vendor/rails from Textmate projects

If you have a rails project you load up in textmate, and you have some version of Rails in vendor/rails, you probably know that doing a 'find in project' takes a lot longer because it scans all the rails source files. You also get a lot more crap to wade thru when doing a go-to type (command t). To exclude vendor rails, click on the project root in the drawer, click on the "i" icon, and change the folder pattern to this:

!.*/(\.[^/]*|CVS|vendor/rails|_darcs|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$$


Note the addition of "vendor/rails" to the regex. You can also add this to preferences -> advanced to make it the default for new text mate projects.

Open current Finder window folder in TextMate

// Opens the current Finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.

-- Opens the current finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.
-- adopted from a script I found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/open in TextMate
-- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a file or folder onto it.

-- Another nice touch is to give the saved script the same icon as TextMate.
-- To do this, in the finder, Get info (Command-I) of both TextMate and this saved script.
-- Click the TextMate icon (it will highlight blue) and copy it by pressing Comand-C.
-- Click on this script's icon and paste by pressing Command-V.

-- Another way to give it the same icon as TextMate is to save the script as an application bundle (instead of an application),
--  then copy the icon by entering these commands in Terminal:
-- $ cd ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app/Contents/Resources/
-- $ rm droplet.icns
-- $ cp /Applications/TextMate.app/Contents/Resources/TextMate.icns droplet.icns
-- $ touch ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	CD_to(theList, false)
end open

-- open the file/folder in TextMate
on CD_to(theDir, newWindow)
	try
		set theDir to quoted form of POSIX path of theDir as string
		do shell script "open -a TextMate " & theDir
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end CD_to

I &lt;3 TextMate and Quicksilver

(in quicksilver)

.tm ~/path/to/project && logout <TAB> term


Executes in terminal, opens the folder or file with textmate, and closes.

open remote file with local textmate

Run this on a remote server to open the file on your local map with ssh. Written by danp, I'm just saving this for a rainy day.

#!/bin/sh

host=`echo $SSH_CLIENT | cut -d' ' -f1`
path=`pwd | sed "s|^$HOME|/Volumes/chaos|"`
app="/Applications/TextMate.app"
file=$1

if [ ! -e $file ]; then
	echo -n "create $file? [Yn] "
	read input
	if [ -z "$input" -o "$input" = "Y" -o "$input" = "y" ]; then
		touch $file
	else
		exit
	fi
fi

ssh $host "open -a $app $path/$file; exit"
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS