在文本编辑器中删除代码行 [英] Deleting lines of code in a text editor

查看:70
本文介绍了在文本编辑器中删除代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题被标记为托尔斯泰",以感谢我的写作质量和长度:)仅阅读第一和最后一段就足够了:)如果您倾向于使用鼠标选择并移动代码,那么中间的内容可能会让您很感兴趣.

This question had been tagged "Tolstoy" in appreciation of the quality and length of my writing:) Just reading the first and the last paragraph should be enough:) If you tend to select and move code with the mouse, the stuff in middle could be interesting to you.

这个问题是关于您一般如何使用文本编辑器的.我正在寻找删除多行代码的最佳方法(无意申请专利:)这扩展到转置行,即删除并将它们添加到某处别的.最重要的是,我不想创建任何必须单独删除的空白行.有点像Visual Studio的SHIFT + DELETE功能,但是一次可以处理多行.

This question is about how you use text editors in general. I’m looking for the best way to delete a plurality of lines of code (no intent to patent it:) This extends to transposing lines, i.e. deleting and adding them somewhere else. Most importantly, I don’t want to be creating any blank lines that I have to delete separately. Sort of like Visual Studio's SHIFT+DELETE feature, but working for multiple lines at once.

假设您要从以下代码中删除第3行(也显示了标签和换行符).天真的方法是在尖括号之间选择文本:

Say you want to delete line 3 from following code (tabs and newlines visualized as well). The naïve way would be to select the text between angle brackets:


if (true) {\n
\t int i = 1;\n
\t <i *= 2;>\n
\t i += 3;\n
}\n

然后按退格键.这将创建一个空行.再按两次退格键删除\ t和\ n.

Then hit backspace. This creates a blank line. Hit backspace twice more to delete \t and \n.

您最终得到:


if (true) {\n
\t int i = 1;\n
\t i += 3;\n
}\n

当您尝试选择整行时,Visual Studio不允许您选择结尾的换行符.例如,将光标放在一行上并按SHIFT + END不会在末尾选择换行符.如果您使用鼠标,也不会选择换行符,即单击一行的中间并将光标一直拖动到最右边.您只有做出选择跨越至少两行选择尾随换行符.我使用的大多数编辑器都是这样做的.Microsoft WordPad和Word是反示例(在其中删除文本时,我经常会出现换行错误;至少Word可以显式显示行尾和段落末尾字符).

When you try to select a whole line, Visual Studio doesn't let you select the trailing newline character. For example, placing the cursor on a line and hitting SHIFT+END will not select the newline at the end. Neither will you select the newline if you use your mouse, i.e. clicking in the middle of a line and dragging the cursor all the way to the right. You only select the trailing newline characters if you make a selection that spans at least two lines. Most editors I use do it this way; Microsoft WordPad and Word are counter-examples (and I frequently get newlines wrong when deleting text there; at least Word has a way to display end-of-line and end-of-paragraph characters explicitly).

通常使用Visual Studio和其他编辑器时,以下是目前最适合我的解决方案:

When using Visual Studio and other editors in general, here’s the solution that currently works best for me:

使用鼠标,选择放在尖括号之间的字符:

Using the mouse, I select the characters that I put between angle brackets:


if (true) {\n
\t int i = 1;<\n
\t i *= 2;>\n
\t i += 3;\n
}\n

现在点击退格键,即可一次性删除该行,而不必删除其他任何字符.这一次可用于多个连续行.另外,它可以用于换位线.您可以将尖括号之间的选择拖动到插入符号标记的点:

Hitting backspace now, you delete the line in one go without having to delete any other characters. This works for several contiguous lines at once. Additionally, it can be used for transposing lines. You could drag the selection between the angle brackets to the point marked with a caret:


if (true) {\n
\t int i = 1;<\n
\t i *= 2;>\n
\t i += 3;^\n
}\n

这使您拥有:


if (true) {\n
\t int i = 1;\n
\t i += 3;<\n
\t i *= 2;>\n
}\n

第3行和第4行已切换位置.

where lines 3 and 4 have switched place.

此主题有所不同.当您要删除第3行时,还可以选择以下字符:

There are variations on this theme. When you want to delete line 3, you could also select the following characters:


if (true) {\n
\t int i = 1;\n
<\t i *= 2;\n
>\t i += 3;\n
}\n

实际上,如果您告诉Visual Studio选择完整的行,这就是Visual Studio所做的.通过单击代码和红色圆圈指示断点的列之间的边距来执行此操作.鼠标指针在该区域进行了镜像,以更好地区分它,但是我认为它太窄了,并且与我要选择的代码在物理上相距太远.

In fact, this is what Visual Studio does if you tell it to select a complete line. You do this by clicking in the margin between your code and the column where the red circles go which indicate breakpoints. The mouse pointer is mirrored in that area to distinguish it a little better, but I think it's too narrow and physically too far removed from the code I want to select.

也许该方法对其他人也有用,即使它只是使他们知道选择/删除文本时如何处理换行符:)它对于大多数非专业的文本编辑器也很好用.但是,鉴于Visual Studio的大量功能和插件(我最常使用),我相信有更好的方法来使用它删除和移动代码行.在不同块之间移动代码时自动正确地使缩进效果很好(即,无需点击设置文档格式/选择格式").我期待提出建议;请不要对微优化提出任何要求:)

Maybe this method is useful to other people as well, even if it only serves to make them aware of how newlines are handled when selecting/deleting text:) It works nicely for most non-specialized text editors. However, given the vast amount of features and plugins for Visual Studio (which I use most), I'm sure there is better way to use it to delete and move lines of code. Getting the indentation right automatically when moving code between different blocks would be nice (i.e. without hitting "Format Document/Selection"). I'm looking forward to suggestions; no rants on micro-optimization, please:)

答案摘要

Summary of Answers

关于Visual Studio:使用光标键可以很好地导航.

With respect to Visual Studio: Navigating well with the cursor keys.

最适合我的翻阅和编辑代码风格的解决方案是 Eclipse 方式:

The solution that would best suit my style of going over and editing code is the Eclipse way:

您可以选择几行连续的代码行,其中第一行和最后一个选定的行可能仅被部分选择.按下ALT + {up,down}上下移动整行(不只是所选内容),并随即固定缩进.按CTRL + D可以完全删除行(而不仅仅是选择行),而不会留下任何不需要的空白行.我很想在Visual Studio中看到这个!

You can select several consecutive lines of code, where the first and the last selected line may be selected only partially. Pressing ALT+{up,down} moves the complete lines (not just the selection) up and down, fixing indentation as you go. Hitting CTRL+D deletes the lines completely (not just the selection) without leaving any unwanted blank lines. I would love to see this in Visual Studio!

推荐答案

在Emacs中:

  • 杀死线C-k
  • 转置线C-x C-t

C-a C-k C-k-杀死包括换行符在内的整行(或由C-S-backspace终止 kill-whole-line ).

C-a C-k C-k -- kill whole line including newline (or kill-whole-line by C-S-backspace).

C-u< number> C-k-杀死< number>行(包括换行符).

C-u <number> C-k -- kill <number> of lines (including newlines).

C-y-撤消最近杀死的文本(又名粘贴)

C-y -- yank back the most recently killed text (aka paste)

这篇关于在文本编辑器中删除代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆