Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textmate: How do I enter a repeated sequence of characters?

Tags:

emacs

textmate

I often need to enter text (consisting of repeated characters) like this:

------------------------------------
 TODO
------------------------------------

In emacs, I can do a

C-u 60 - 

that's a Ctrl+U followed by a "60" followed by a "-", which makes entering a repeated sequence of characters easy.

Is there any way to do something like this in TextMate?

like image 863
Debajit Avatar asked Feb 06 '09 21:02

Debajit


1 Answers

In TextMate, open the Bundle Editor and select the language you'd like to do this in. (If you'd like to have this functionality in all languages, use the Source bundle) Click the plus symbol at the bottom left, and choose "New Command." Chose "Nothing" for the Save field and "Selected Text or Line" for the two input fields. Then paste this into the Commands field:

#!/usr/bin/python
import sys
commandLine = raw_input("")
tmArgs = commandLine.split()
numberOfArgs = len(tmArgs)
for i in range(eval(tmArgs[0])):
    for j in range(1, numberOfArgs):
        sys.stdout.write(tmArgs[j])

You can then choose a keyboard shortcut to activate this with in the Activation field. The way it works is very similar to that emacs command: type the number of characters you want followed by the character. Then select both of them (this step is unnecessary if they're the only text on the line) and press the shortcut key. My script allows you to specify multiple characters to print, delimited by spaces. So if you typed

10 - =

and hit the shortcut key, you'd get

-=-=-=-=-=-=-=-=-=-=

Edit: After thinking about it...here's another version. This one will print the string after the number. So for example

6 -= (space)

prints

-= -= -= -= -= -= 

Here's that version:

#!/usr/bin/python
import sys
import string
commandLine = raw_input("")
timesToPrint = eval(commandLine.split()[0])
firstSpace = string.find(commandLine, " ")
for i in range(timesToPrint):
        sys.stdout.write(commandLine[firstSpace + 1:])
like image 176
Kevin Griffin Avatar answered Nov 15 '22 07:11

Kevin Griffin