vim中如何删除非空白字符?
我正在寻找一个命令来删除从光标到同一行上的第一个非空白字符。我用谷歌搜索了一段时间并尝试了几种可能性。没有喜悦。有人知道该怎么做吗?
I'm looking for a command to delete from the cursor to the first non-whitespace character on the same line. I've googled for a while and tried several possibilities. No joy. Does someone out there know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
序列 dw 将删除光标处直到下一个单词的所有字符。这意味着,如果您站在单词中间执行该命令,它将删除该单词的其余部分以及后续的空格。可能是也可能不是您正在寻找的东西。
The sequence dw will delete all characters from the cursor until the next word. This means that if you execute the command while standing in the middle of a word, it will delete the remainder of that word and subsequent whitespaces. May or may not be what you're looking for.
您可能想尝试dW。这将按“WORD”移动(请参阅 WORD 和 word 的帮助),这看起来更适合您的问题。
dw
无法满足您的需求,例如:在位于
a
中时点击dw
将为您留下:但是使用
dW
将导致:You may want to try dW. This will move by "WORD" (see help for WORD and word), which looks more appropriate for your question.
The
dw
won't meet your needs in, for example:Hitting
dw
while positioned in thea
will leave you with:But using
dW
will result in:这里的许多答案并没有真正直接解决问题。提问者想知道如何删除最多第一个非空白字符。有些答案在技术上是可行的,但让我们看看如何明确地做到这一点。
以下示例演示了如何在正常模式下执行此操作,并考虑了光标起始位置的变化。下划线字符表示光标位置:
dw:
foo_ 酒吧
→
foob̲ar
其他答案中描述的 delete word 命令可以很好地在光标移动时删除下一个非空白字符位于目标之前。
db:
foo b̲ar
→
b̲ar
当然,我们想尝试 dw 的逆操作来删除向后到之前<的第一个非空白字符/em> 光标。然而,如上所示,delete back-word 命令删除的内容超出了我们的预期,它还删除了前一个单词。对于这种情况,我们应该使用:
dT:
foo b̲ar
→
foob̲ar
...其中 是光标之前的第一个非空白字符。 delete back-unTil 命令会删除直到但不包括字符 < 的所有内容;?>,在本例中是“foo”末尾的字符 o。
dt:
foo_ 酒吧
→
foob̲ar
与前面的命令类似,我们可以使用delete until(带有小写“t”)来删除字符向前直到下一个 字符(本例中为“bar”中的 b)。就本问题而言,这实现了与 dw 相同的结果。
diw:
foo _ bar
→
foob̲ar
如果我们的光标位于空白的中间,我们可以使用 delete inner word 命令删除两侧的空格。
d/↵:
foo_
\n
栏→
foob̲ar
如果我们要删除的空白包含换行符,我们可以使用上面显示的命令d删除直到匹配模式< /em>,本例中的模式只是第一个非空白字符。如图所示,按 Enter 完成此命令。
当第一个非空白字符出现在换行后的行首时,Vim 将删除空白,但将目标保留在下一行。我们需要将 J 添加到上面的命令中,以 Join 行(大写的“j”)。
d/↵J:
foo_
\n
栏→
foob̲ar
Many of the answers here don't really address the question directly. The asker wants to know how to delete up to the first non-whitespace character. Some of the answers will technically work, but let's take a look at how to do this explicitly.
The following examples demonstrate how to do this in normal mode with variations that account for the starting position of the cursor. The u̲nderlined c̲haracters indicate the cursor position:
dw:
foo_ bar
→
foob̲ar
The delete word command, described in other answers, works just fine to delete up to the next non-whitespace character when our cursor is positioned before the target.
db:
foo b̲ar
→
b̲ar
Naturally, we'd want to try the inverse of dw to delete backwards to the first non-whitespace character before the cursor. However, as shown above, the delete back-word command deletes more than we expect—it erases the previous word as well. For this case, we should use:
dT<?>:
foo b̲ar
→
foob̲ar
...where <?> is the first non-whitespace character before the cursor. The delete back-unTil command erases everything up to but not including the character <?>, which, in this case, is the character o at the end of "foo".
dt<?>:
foo_ bar
→
foob̲ar
Similar to the previous command, we can use delete until (with a lowercase "t") to delete characters forward until the next <?> character (the b in "bar", for this example). This achieves the same result as dw for the purpose of this question.
diw:
foo _ bar
→
foob̲ar
If our cursor is positioned in the middle of the whitespace, we can use the delete inner word command to remove the whitespace from both sides.
d/<?>↵:
foo_
\n
bar→
foob̲ar
If the whitespace we want to remove includes line-breaks, we can use the command shown above to delete until matched pattern of <?>, where the pattern in this case is just the first non-whitespace character. As shown, press Enter to complete this command.
When the first non-whitespace character occurs at the beginning of the line after the break, Vim will remove the whitespace, but leave the target on the next line. We need to add J to the above command to Join the lines (an uppercase "j").
d/<?>↵J:
foo_
\n
bar→
foob̲ar
要删除单词,无论光标位于哪个字母,请使用 daw(助记符“删除单词”)也可与其他命令一起使用,例如 caw“更改单词” ”。 f 和 t 是其他优秀的命令,可以与 d 一起使用。例如,要从光标处删除并包括第一次出现的字母“t”,请使用dft。要保持“t”完整,请使用 dtt 代替。
To delete a word regardless on which letter the cursor is on, use daw (mnemonic "delete a word") works with other commands as well, e.g. caw "change a word". f and t are other excellent command that can be used together with d. E.g. to delete from cursor to and including first occurrence of e.g. the letter "t", use dft. To leave the "t" intact, use dtt instead.
dw 应该可以。
dw should work.
d
W
将删除包含最后一个空格的单词。如果您要删除的单词位于行尾,您可以优先使用d
e
。因为如果您使用d
W
,它可以将您的下一行向上移动。我总是使用
d
i
W
。d
W
will delete word include the last space. If the word you want to delete is at the end of a line, you can prefer used
e
. because if you used
W
, it can shift your next line up.I always use
d
i
W
.