作者:丶Le丨囧囧_832 | 来源:互联网 | 2023-08-18 22:45
SayIhavealineinanemacsbufferthatlookslikethis:假设我在emacs缓冲区中有一行如下所示:foo-option1value
Say I have a line in an emacs buffer that looks like this:
假设我在emacs缓冲区中有一行如下所示:
foo -option1 value1 -option2 value2 -option3 value3 \
-option4 value4 ...
I want it to look like this:
我希望它看起来像这样:
foo -option1 value1 \
-option2 value2 \
-option3 value3 \
-option4 value4 \
...
I want each option/value pair on a separate line. I also want those subsequent lines indented appropriately according to mode rather than to add a fixed amount of whitespace. I would prefer that the code work on the current block, stopping at the first non-blank line or line that does not contain an option/value pair though I could settle for it working on a selected region.
我希望每个选项/值对在一个单独的行上。我还希望那些后续行根据模式适当缩进,而不是添加固定数量的空格。我希望代码在当前块上工作,在第一个非空白行或不包含选项/值对的行停止,但我可以解决它在选定区域上工作。
Anybody know of an elisp function to do this?
有人知道elisp功能吗?
4 个解决方案
1
In this case I would use a macro. You can start recording a macro with C-x (, and stop recording it with C-x ). When you want to replay the macro type C-x e.
在这种情况下,我会使用宏。您可以使用C-x开始录制宏(并使用C-x停止录制)。当您想重播宏类型C-x e时。
In this case, I would type, C-a C-x ( C-s v a l u e C-f C-f \ RET SPC SPC SPC SPC C-x )
在这种情况下,我会键入,C-a C-x(C-s v a l u e C-f C-f \ RET SPC SPC SPC SPC C-x)
That would record a macro that searches for "value", moves forward 2, inserts a slash and newline, and finally spaces the new line over to line up. Then you could repeat this macro a few times.
这将记录一个宏,搜索“值”,向前移动2,插入斜线和换行符,最后将新行空格连接到排队。然后你可以重复这个宏几次。
EDIT: I just realized, your literal text may not be as easy to search as "value1". You could also search for spaces and cycle through the hits. For example, hitting, C-s a few times after the first match to skip over some of the matches.
编辑:我刚刚意识到,你的文字文本可能不像“value1”那样容易搜索。您还可以搜索空格并循环浏览。例如,在第一场比赛后几次击中C-s以跳过一些比赛。
Note: Since your example is "ad-hoc" this solution will be too. Often you use macros when you need an ad-hoc solution. One way to make the macro apply more consistently is to put the original statement all on one line (can also be done by a macro or manually).
注意:由于您的示例是“ad-hoc”,因此该解决方案也是如此。通常在需要临时解决方案时使用宏。使宏应用更一致的一种方法是将原始语句全部放在一行上(也可以通过宏或手动完成)。
EDIT: Thanks for the comment about ( versus C-(, you were right my mistake!
编辑:感谢你的评论(与C-相对(你错了!)
0
Personally, I do stuff like this all the time.
就个人而言,我总是这样做。
But I don't write a function to do it unless I'll be doing it every day for a year.
但我不会写一个函数去做,除非我每天都会这样做一年。
You can easily do it with query-replace, like this:
您可以使用query-replace轻松完成此操作,如下所示:
m-x (query-replace " -option" "^Q^J -option")
m-x(query-replace“-option”“^ Q ^ J -option”)
I say ^Q^J as that is what you'll type to quote a newline and put it in the string.
我说^ Q ^ J因为这是你输入引用换行符并将其放在字符串中的内容。
Then just press 'y' for the strings to replace, and 'n' to skip the wierd corner cases you'd find.
然后按“y”键替换字符串,然后按“n”跳过你找到的奇怪的角落情况。
Another workhorse function is query-replace-regexp that can do replacements of regular expressions.
另一个主力函数是query-replace-regexp,可以替换正则表达式。
and also grep-query-replace, which will perform query-replace by parsing the output of a grep command. This is useful because you can search for "foo" in 100 files, then do the query-replace on each occurrence skipping from file to file.
还有grep-query-replace,它将通过解析grep命令的输出来执行查询替换。这很有用,因为您可以在100个文件中搜索“foo”,然后在每个从文件到文件的跳过中执行查询替换。