views:

366

answers:

4

I have 7 lines of text:

a
b
c
d
e
f
g

Now I want to add characters to the end of each line, to end up with:

a,
b,
c,
d,
e,
f,
g,

I found that I can use the "sed" command and run my selection through sed using "Filter through command" in Textmate

sed 's/$/,/'

Now, one question remains: how do I turn this into a Textmate command that takes input in some sort of way (so it knows what text to append)?

(My tries in doing this have proven unsuccessful)

+2  A: 

In Text menu there is already a command "Edit each line in selection" exactly do this. It will put cursor on first line and what you type there repeated on each line.

nexneo
I know; but it doesn’t seem to be able to prepend. If I place my cursor at the beginning of the line, hit the shortcut, then it jumps to the end.I could use column selections but then I'm using the mouse again, which isn't speedy enough.
Wolfr
column selection doesn't need mouse. use page up + shift and press option when done.I don't know about sed. but in textmate you can write bundle command very easily that replace selection with sed processed text if you know sed/ruby/regex+ruby etc.
nexneo
+2  A: 

Create a new command in the bundle editor

#!/bin/bash
sed 's/$/,/'

On the input dropdown select Selected text or Nothing
On the output select Replace existing text

I just tested it and it works fine.
You can also choose a keyboard shortcut to make it more efficient.

andi
+1  A: 

If you're willing to avoid the command route and simply use the Find/Replace dialog simply do as follows:

  • highlight/select the lines you'd like to append to
  • open the Find dialog
  • check 'Regular Expressions'
  • in the 'Find' field, add '$' (to indicate the end of the line)
  • in the 'Replace' field, add ',' (what you want appended)
  • hold Option, this will change "Replace All" to "In Selection"

This technique can be applied in a number of other useful ways. For example, changing '$' to '^' if you want to prefix each line.

Devin Reams
+3  A: 

Pop this into a command within the Text bundle, it'll append whatever is in the clipboard to the end of all lines that have been selected:

#!/bin/bash
if [[ $(pbpaste|wc -l) -eq 0 ]]
    then r=`pbpaste`
    sed 's/$/'$r'/'
    else sed 's/$/,/'
fi

It's currently limited to appending one line's worth of text, if the clipboard contains more than one line it will default to a comma at the end of the selected lines.

Edit:

To take this a little further, here's a version that provides a dialog box which prompts for the input of the string that will be appended to each line in the selection:

#!/bin/bash
r=$(CocoaDialog inputbox --title "String to be appended to EOL" \
   --informative-text "Enter string:" \
   --button1 "Okay" --button2 "Cancel")

[[ $(head -n1 <<<"$r") == "2" ]] && exit_discard

r=$(tail -n1 <<<"$r")

sed "s/$/$r/"
Greg Annandale