Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial add

  • Loading branch information...
commit 8ae6aaaa83cbe8dd4865f9722ccb9a3abdb60efa 0 parents
Mark Fowler authored

Showing 2 changed files with 86 additions and 0 deletions. Show diff stats Hide diff stats

  1. +58 0 README.markdown
  2. +28 0 ReplaceIt.py
58 README.markdown
Source Rendered
... ... @@ -0,0 +1,58 @@
  1 +ReplaceIt
  2 +---------
  3 +
  4 +This is ReplaceIt, a plugin for [Sublime Text 2][subl] that gives
  5 +you a way to bind find and replaces to a keyboard shortcut.
  6 +
  7 +Usage
  8 +-----
  9 +
  10 +To use this you'll need to place something like this inside your key bindings:
  11 +
  12 + {
  13 + "keys": ["ctrl+alt+super+g"],
  14 +
  15 + "command": "replace_it",
  16 + "args": {
  17 + "find": "old text",
  18 + "replace": "new text"
  19 + }
  20 + },
  21 +
  22 +The command takes the following arguments:
  23 +
  24 +### find
  25 +
  26 +The string the plugin will search for and replace. This argument is mandatory.
  27 +
  28 +### replace
  29 +
  30 +The string that the plugin will replace found text with. This argument is mandatory
  31 +
  32 +### regex
  33 +
  34 +A boolean indicating that we should treat the the string we're searching for as a regular expression
  35 +to match instead of a string. This argument is optional, and by default this feature is off.
  36 +
  37 +### ignore_case
  38 +
  39 +A boolean indicating that we should ignore the case when we're searching for text. This argument is optional, and by default this feature is off.
  40 +
  41 +### only_next
  42 +
  43 +A boolean indicating that we shouldn't do a global search and replace but just replace the next occurrence after the start of the current selection / cursor position. This argument is optional, and by default this feature is off.
  44 +
  45 +Installation
  46 +------------
  47 +
  48 +Assuming you've got git installed (you don't? shame on you!)
  49 +
  50 + cd ~/Library/Application Support/Sublime Text 2/Packages
  51 + git co git://github.com/2shortplanks/ReplaceIt.git
  52 +
  53 +To update
  54 +
  55 + cd ~/Library/Application Support/Sublime Text 2/Packages/ReplaceIt
  56 + git pull
  57 +
  58 +[subl]: http://www.sublimetext.com/2
28 ReplaceIt.py
... ... @@ -0,0 +1,28 @@
  1 +import sublime, sublime_plugin
  2 +
  3 +class ReplaceItCommand(sublime_plugin.TextCommand):
  4 + def run(self, edit, find, replace, regex = False, ignore_case = False, only_next = False):
  5 + flags = 0;
  6 + if not regex:
  7 + flags = flags | sublime.LITERAL
  8 + if ignore_case:
  9 + flags = flags | sublime.IGNORECASE
  10 +
  11 + if only_next:
  12 + point = self.view.sel()[0]
  13 + regions = [ self.view.find(find, point.begin(), flags) ]
  14 + else:
  15 + regions = self.view.find_all(find, flags)
  16 +
  17 + offset = 0
  18 + length = len(replace)
  19 + for region in regions:
  20 + adjusted_region = sublime.Region( region.a + offset, region.b + offset)
  21 + self.view.replace( edit, adjusted_region, replace)
  22 + offset = offset - region.size() + length
  23 +
  24 + def is_visible():
  25 + return False
  26 +
  27 + def description():
  28 + "A version of find and replace that can be bound to a keyboard shortcut"

0 comments on commit 8ae6aaa

Please sign in to comment.
Something went wrong with that request. Please try again.