Friday, january 29th, 2010

“Repeat command” in TextMate revisited

Warning: this entry is old. It may contain outdated information and may not reflect my opinion anymore.

As I said in a previous post (“Repeat command” in TextMate), one of the things I use a lot in jed editor, my favorite terminal editor, is the “repeat command”: pressing ESC, typing a number and then something will repeat something number times. This also works in emacs and bash..

It can also be used with other commands like delete, yank (paste) and such. I don’t do that often, but I do use this for lines of '-' or '=' quite often.

The previous solution I posted was a “snippet”. It implied typing a number, “:”, what to repeat, select that text and then hit some hotkey.

Slow, and annoying because we have to toy with the shift + arrow keys to select the text.

Here’s a new version as a “command”.

The Script

Menu “Bundles” → “Bundle Editor” → “Show Bundle Editor”.

Create a bundle using the “+” below the list. I place my own things in a bundle named “_Serge”.

In that bundle, create a new “Command” by using the “+” button again. Use the following values:

  • “Save”: Nothing
  • “Input”: “Selected Text” or “Word”
  • “Output”: “Insert as Snippet”

And in the “Command(s)” text area:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; coding: utf-8 -*-

import os
import sys
import subprocess

textmate_path = '/Applications/TextMate.app'
cocoadialog_exe = os.path.join(
    textmate_path,
    'Contents/SharedSupport/Support/bin/CocoaDialog.app',
    'Contents/MacOS/CocoaDialog'
)

input = sys.stdin.read().decode('utf-8')
if ':' in input:
    # Quick entry:  "6:-" repeats "-" 6 times
    input = input.split(':')
    r_num = int(input.pop(0))
    r_what = u':'.join(input)
    sys.stdout.write(r_what * r_num)
    sys.exit(0)

r_num = int(input)

help_text = u"Enter the string to repeat %d times" % r_num
initial_text = u"-"

cmd = [
    cocoadialog_exe,
    'standard-inputbox',
    '--informative-text', help_text,
    '--float',
    '--text', initial_text,
]

sp = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = sp.communicate()

lines = stdout.split('\n')
if lines[0] != '1':
    # Cancel
    exit(0)

r_num = int(input)
r_what = lines[1]

sys.stdout.write(r_what * r_num)

Assign a “Key Equivalent” if you wish. I use something like “Command-ESC” (⌘-⎋).

Usage

That small script has two modes of operation.

The first way

To repeat “=” 7 times, type “7:=” in TextMate’s buffer area, select it, and then call the command.

The second way

To repeat “=” 7 times, type “7” in TextMate’s buffer area, call the command (e.g. ⌘-⎋), and type in the text box “=” and press “enter”.

Examples

Text Result
10:* **********
10:: ::::::::::
10:' ''''''''''
10:-*- -*--*--*--*--*--*--*--*--*--*-
Posted at 21:14
 
Tags: osx · TextMate

Comments

Comment RSS

There are no comments for this entry.

Drop a comment

Comments are closed for this entry.