将单个 sed 命令转换为可重用的 Textmate 命令

发布于 2024-07-19 07:49:05 字数 336 浏览 3 评论 0原文

我有 7 行文本:

a
b
c
d
e
f
g

现在我想在每行末尾添加字符,最终得到:

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

我发现我可以使用“sed”命令并使用 Textmate 中的“Filter through command”通过 sed 运行我的选择

sed 's/$/,/'

现在,仍然存在一个问题:如何将其转换为以某种方式接受输入的 Textmate 命令(以便它知道要附加什么文本)?

(我的尝试被证明是不成功的)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩余の解释 2024-07-26 07:49:05

将其弹出到文本包中的命令中,它将把剪贴板中的任何内容附加到已选择的所有行的末尾:

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

当前仅限于附加一行文本,如果剪贴板包含多于一行,则它会附加一行文本。将默认为所选行末尾的逗号。

编辑:

为了更进一步,这里的版本提供了一个对话框,提示输入将附加到所选内容中的每一行的字符串:

#!/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/"

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/"
寄风 2024-07-26 07:49:05

文本菜单中已经有一个命令“编辑选择中的每一行”正是执行此操作。 它将把光标放在第一行,并且您在每一行上重复输入的内容。

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.

请你别敷衍 2024-07-26 07:49:05

在捆绑包编辑器中创建新命令

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

在输入下拉列表中选择所选文本

在输出中选择替换现有文本

我刚刚测试了它,它工作正常。
您还可以选择键盘快捷键以提高效率。

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.

凶凌 2024-07-26 07:49:05

如果您愿意避免使用命令路径而仅使用“查找/替换”对话框,只需执行以下操作:

  • 突出显示/选择要附加的行以
  • 打开“查找”对话框
  • 选中“正则表达式”
  • 在“查找”字段中 添加 '$' (表示行尾)
  • ,在“替换”字段中
  • ,添加 ',' (您想要附加的内容)按住 Option,这会将“全部替换”更改为“选择中”。

此技术可以应用于许多其他有用的方式。 例如,如果您想为每行添加前缀,请将 '$' 更改为 '^'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文