TextMate: Search a Current Folder


/ Published in: Bash
Save to your folder(s)

This is a script for finding a string in files under a current folder in TextMate. It can be very convenient if you know what you are looking is in a particular folder by saving your time searching each file individually or going through a long list of the search result produced by ‘Find in Project’ function.

Improved version of http://snipplr.com/view/16541/search-text-in-files-under-the-current-folder-in-textmate/


Copy this code and paste it in your HTML
  1. # Search text in files under the current folder
  2. # by Saophalkun Ponlu
  3. # 15 Jun 2009 | Last Changed 12 Oct 2009
  4.  
  5. # input dialog box
  6. res=$(CocoaDialog inputbox --title "Search Folder" \
  7. --informative-text "Enter a search string:" \
  8. --button1 "Okay" --button2 "Cancel")
  9.  
  10. # get the actual search string
  11. SEARCH=$(tail -n1 <<<"$res")
  12.  
  13. # search and replace regular expression
  14. MATCH="^\([a-zA-Z/._-]*\):\([0-9]*\):\(.*\)"
  15. REPLACE='<li><a href=\"txmt:\/\/open\/?url=file:\/\/\/\1\&line=\2\">\1 (\2)<\/a> <p>\3<\/p><\/li>'
  16.  
  17. # output styles
  18. echo "
  19. <style>
  20. body { font-size: 12px; }
  21. li { list-style: none; border-bottom: 2px solid #eee; padding: 10px 5px 5px }
  22. li p { color: #333; margin-left: 1.5em; margin-top: .6em; margin-bottom: 1.2em; }
  23. </style>
  24. "
  25.  
  26. SELECTED_DIR=$TM_SELECTED_FILE
  27.  
  28. # if the selected item (path) is a file then get its directory instead
  29. if [ -f "$SELECTED_DIR" ]; then
  30. SELECTED_DIR=`dirname $SELECTED_DIR`;
  31. fi
  32.  
  33. echo $SELECTED_DIR
  34. echo "<h3>You searched for: <em style=\"color: red\">$SEARCH</em></h3>"
  35.  
  36. # the search process starts
  37. grep -rn --exclude=*.svn-base "$SEARCH" $SELECTED_DIR | sed "s/$MATCH/$REPLACE/g"

URL: http://phalkunz.com/2009/10/17/textmate-search-a-current-folder/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.