取消缩进代码片段

发布于 2024-08-17 23:10:13 字数 731 浏览 2 评论 0原文

有时,我想将代码片段发布到缺陷跟踪器、wiki、论坛等上,但它在代码中缩进得很深。我通常必须突出显示代码片段,点击-直到缩进不太糟糕,然后复制/粘贴,然后恢复代码。会变得有些疼痛。

有没有一种工具可以删除每行前面的选项卡?请注意,我不想删除所有行中的所有选项卡,只需删除每行中前面相同数量的选项卡即可。我想 Emacs 的某些功能可以做到这一点。

这是一个极端的例子:

之前:

                            //This is usually some method in an anonymous inner class' anonymous inner class.
                            @Override
                            public void method(){
                                doSomething();
                            }

之后:

//This is usually some method in an anonymous inner class' anonymous inner class.
@Override
public void method(){
    doSomething();
}

注意 doSomething() 前面仍然有一个选项卡。

There are times that I have a code snippet that I want to post on a defect tracker, wiki, forum, etc. but it's deeply indented in the code. I usually have to highlight the code snippet, hit <shift>-<tab> until the indents aren't so bad, then copy/paste and then revert the code. It gets somewhat painful.

Is there a tool that can remove the tabs in front of the each line? Note that I don't want to remove all tabs from all lines, just same preceding number of tabs from each line. I'm thinking some function of Emacs could do this.

Here's an extreme example:

Before:

                            //This is usually some method in an anonymous inner class' anonymous inner class.
                            @Override
                            public void method(){
                                doSomething();
                            }

After:

//This is usually some method in an anonymous inner class' anonymous inner class.
@Override
public void method(){
    doSomething();
}

Notice how doSomething() still has a single tab in front of it.

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

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

发布评论

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

评论(8

妖妓 2024-08-24 23:10:14

在 Emacs 中执行此操作的实际方法是使用此组合键。

首先选择要从中删除选项卡的部分。

您可以选择整个缓冲区:

CTRL+x h

或者,如果您只想要一个区域,只需设置标记 CTRL+SPACE 您想要开始的位置,然后导航到您想要结束的位置。

然后删除空格,对于 10 个空格字符,执行以下操作:

CTRL+- 10 CTRL+x TAB

由于这个内容相当大,我会为您分解它。

1) 首先,我们给出负参数命令:(

负参数 ARG)

kbd 快捷方式:CTRL+-

2) 接下来提供数字参数,在本例中为数字10:

10

3)然后我们调用 indent-rigidly 命令

(indent-rigidly START END ARG)

kbd 快捷键:CTRL+x TAB

所以我们正在做的是给命令参数 -10 来严格缩进,结果是从每行的开头删除 10 个空白字符我们已经选择了,在这种情况下,我们选择了整个文档(CTRL+x h),因此 10 行空白将被删除从整个文档的每一行的开头。

如果某一行的空白少于 10 个,它只会删除尽可能多的空白,如果超过 10 个空白,那么在完成后可能会留下一些空白。

在您的示例中,看起来您有大约 30 个前导空格,因此这应该可以解决问题:

CTRL+- 30< /kbd> CTRL+x TAB

如果要删除更多,请尝试更大的数字。

The actual method to do this in Emacs is with this key combination.

First select the section you want to remove the tabs from.

You could select the entire buffer:

CTRL+x h

Or, if you just want a region simply set the mark CTRL+SPACE where you want to begin and then navigate to where you want to end.

Then remove whitespace, for 10 whitespace characters do this:

CTRL+- 10 CTRL+x TAB

Since this is quite big, I'll break it down for you.

1) First we give the negative-argument command:

(negative-argument ARG)

kbd shortcut: CTRL+-

2) Next supply the numerical argument, in this case the number 10:

10

3) Then we call the indent-rigidly command

(indent-rigidly START END ARG)

kbd shortcut: CTRL+x TAB

So what we are doing is giving the argument of -10 to the command to indent-rigidly and the result is that 10 whitespace characters will be removed from the beginning of each line which we have selected, in this case we have the entire document selected (CTRL+x h), so 10 lines of whitespace will be removed from the beginning of every line of the entire document.

If a particular line has less than 10 whitespace it will only remove as many whitespace as possible, if there are more than 10 whitespace then some may be left over after it is finished.

In your example it looks like you have about 30 leading whitespace, so this should do the trick:

CTRL+- 30 CTRL+x TAB

Try a larger number if you want to remove more.

嘿看小鸭子会跑 2024-08-24 23:10:14

如果您的 emacs c 风格设置正确,那么只需突出显示该代码片段并按 CM-\ 即可正确缩进(如果您处于 c++ 模式)。

If you have your emacs c-style settings correct, then simply highlighting the snippet and pressing C-M-\ will indent it properly (if you're in c++-mode).

甜尕妞 2024-08-24 23:10:14

我认为最简单的答案。 。将所选区域取消缩进 4:

C-u -4 C-x TAB

这是带有负参数的简单区域缩进命令。
例如,这将区域缩进一:

C-x TAB

https://www .gnu.org/software/emacs/manual/html_node/emacs/Indentation-Commands.html

Simplest answer I think . . to UN-indent selected region by 4:

C-u -4 C-x TAB

That is the simple region indent command with negative argument.
e.g this indents region by one:

C-x TAB

https://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation-Commands.html

紫﹏色ふ单纯 2024-08-24 23:10:14

如果您确切知道每行前缀有多少个制表符(如您所述),您可以使用简单的 query-replace-regex 来替换 "^\t\t\t...\ t"""

If you know exactly how many tabs prefix each line (as you state), you could use a simple query-replace-regex to replace "^\t\t\t...\t" with "".

夜巴黎 2024-08-24 23:10:14

我在Kedit(具有列编辑功能)中使用的方法是用Alt-B标记第一列和第一行的第一个字符,将光标定位到我想要删除的最后一行和字符,再次Alt-B,然后Alt-G。它已经消失了,并且仍然保留了已删除列之外的所有缩进。
在 SQL Server 中,这更容易,标记块并按 Shift-Tab 直到代码位于我想要的位置。

The method I use in Kedit (which has the capability of doing column editing) is to mark the first character of the first column and row with Alt-B, position my cursor to the last row and character that I want to remove, Alt-B again and then Alt-G. It's Gone and still has all of the indenting beyond the removed columns.

In SQL Server it's easier, mark the block and press shift-tab until the code is where I want it.

对岸观火 2024-08-24 23:10:14

使用正则表达式删除前导空格。

在 Emacs 中:

C-M-%^<TAB><TAB><TAB><ENTER><ENTER>!

在 Vim 中:

:%s/^\t\t\t//g

Use a regular expression to remove the leading whitespace.

In Emacs:

C-M-%^<TAB><TAB><TAB><ENTER><ENTER>!

In Vim:

:%s/^\t\t\t//g
蛮可爱 2024-08-24 23:10:14

在 vi 中,只需 << 左移一个缩进宽度,因此要取消缩进 10 行,您需要执行 10<<

In vi, it's just << to left shift by one indent width, so to unindent 10 lines, you would do 10<<.

撩起发的微风 2024-08-24 23:10:13

矩形选择是我的首选方法。

将自己放在第一行的开头,C-space,转到最后一行,以及要删除的缩进的末尾和Cx r k(矩形杀戮)。就可以了。

Rectangle selection is my preferred way of doing this.

Put yourself at the beginning of the first line, C-space, go to the last line, and the end of the indentation you want to remove and C-x r k (rectangular kill). That does it.

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