如何在 Vi 中修复整个文件的缩进?
在Vim中,纠正所有行缩进的命令是什么?
很多时候,我会将代码复制并粘贴到远程终端中,然后把整个事情弄乱。 我想一下子解决这个问题。
In Vim, what is the command to correct the indentation of all the lines?
Often times I'll copy and paste code into a remote terminal and have the whole thing messed up. I want to fix this in one fell swoop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果你不想使用
:set Paste
,中键单击,set nopaste
,你也可以粘贴剪贴板的内容:这样你就不必离开正常模式。
如果您必须粘贴
+
或*
取决于您选择文本的方式,请参阅:help quoteplus
。if you do not want to use
:set paste
, middle-click,set nopaste
, you can also paste the content of the clipboard:That way you don't have to leave normal mode.
if you have to paste
+
or*
depends on how you selected the text, see:help quoteplus
.:set Paste
是你的朋友,我使用 putty,最终在窗口之间复制代码。 在我开始使用:set Paste
(和:set nopaste
)之前,复制/粘贴就因为这个原因而让我很舒服。:set paste
is your friend I use putty and end up copying code between windows. Before I was turned on to:set paste
(and:set nopaste
) copy/paste gave me fits for that very reason.在 Vim 中,使用
:insert
。 这将保留所有格式并且不进行自动缩进。 有关更多信息帮助:插入
。In Vim, use
:insert
. This will keep all your formatting and not do autoindenting. For more informationhelp :insert
.对于复杂的 C++ 文件,使用 vim 的
=
过滤命令时,vim 并不总是能获得正确的格式。 因此,对于这种情况,最好使用外部 C++ 格式化程序,例如 astyle (或 uncrustify) 例如:Vim 的 '=' 函数默认使用其内部格式化程序(这并不总是正确的),但可以还可以通过按照这个问题。
For complex C++ files vim does not always get the formatting right when using vim's
=
filter command. So for a such situations it is better to use an external C++ formatter like astyle (or uncrustify) e.g.:Vim's '=' function uses its internal formatter by default (which doesn't always gets things right) but one can also set it use an external formatter, like astyle, by setting it up appropriately as discussed in this question.
您可以创建一个映射来为您执行此操作。
这将自动缩进整个文件,并且仍然将光标保持在您所在的位置:
You can create a mapping to do this for you.
This one will auto indent the whole file and still keep your cursor in the position you are:
只需在 vim 中进入可视模式,然后从上到下选择行,选择后按 = ,所有选定的行都会缩进。
Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.
vim-autoformat 使用特定于您的语言的外部程序(例如“rbeautify”)格式化您的源文件gem 用于 Ruby 文件,“js-beautify”npm 包用于 JavaScript。
vim-autoformat formats your source files using external programs specific for your language, e.g. the "rbeautify" gem for Ruby files, "js-beautify" npm package for JavaScript.
对于 XML 文件,我使用此命令
您需要安装 xmllint(软件包 libxml2-utils)
(来源:http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html )
For XML files, I use this command
You need to have xmllint installed (package libxml2-utils)
(Source : http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html )
对于 vi 编辑器,请使用 :insert。 这将保留所有格式,并且不会插入自动缩进。完成后,按转义键查看实际的格式化文件,否则您会看到一些垃圾字符。 就像^我
例如:
For vi Editor, use :insert. This will keep all your formatting and not insert auto-indenting.Once done press escape to view the actual formatted file otherwise you'l see some garbage characters. like ^I
e.g:
=
,缩进命令可以进行动作。 因此,gg
获取文件开头,=
缩进,G
到文件末尾,gg=G
。=
, the indent command can take motions. So,gg
to get the start of the file,=
to indent,G
to the end of the file,gg=G
.在粘贴到终端之前,请尝试
:set Paste
,然后在完成后尝试:set nopaste
。 这将关闭自动缩进、换行和其他弄乱粘贴的功能。编辑:另外,我应该指出,使用外部程序通常可以获得比
=
缩进更好的结果。 例如,我一直运行:%!perltidy
。 也可以使用astyle
、cindent
等。 当然,您可以将它们映射到击键,并根据文件类型将不同的击键映射到相同的击键。Before pasting into the terminal, try
:set paste
and then:set nopaste
after you're done. This will turn off the auto-indent, line-wrap and other features that are messing up your paste.edit: Also, I should point out that a much better result than
=
indenting can usually be obtained by using an external program. For example, I run:%!perltidy
all the time.astyle
,cindent
, etc. can also be used. And, of course, you can map those to a key stroke, and map different ones to the same keystroke depending on file type.所有命令的主控是
gg=G
这会缩进整个文件!
下面是一些用于在 Vim 或 gVim 中快速缩进的简单而优雅的命令。
缩进当前行下方的所有行
缩进当前行 缩进
当前行下方的 n 行
例如,要缩进当前行下方的 4 行
要缩进代码块,请转至大括号之一并使用命令
The master of all commands is
gg=G
This indents the entire file!
And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.
To indent the all the lines below the current line
To indent the current line
To indent
n
lines below the current lineFor example, to indent 4 lines below the current line
To indent a block of code, go to one of the braces and use command
如果您想重新缩进您所在的块而无需输入任何和弦,您可以执行以下操作:
If you want to reindent the block you're in without having to type any chords, you can do:
按 Esc 键,然后快速输入以下组合:
press escape and then type below combinations fast:
您可以使用 tidy 应用程序/实用程序来缩进 HTML 和 HTML。 XML 文件,它在缩进这些文件方面效果很好。
美化 XML 文件
美化 HTML 文件
You can use tidy application/utility to indent HTML & XML files and it works pretty well in indenting those files.
Prettify an XML file
Prettify an HTML file
1G=G
。 这应该缩进文件中的所有行。1G
将带您进入第一行,=
将开始自动缩进,最后的G
将带您进入文件中的最后一行。1G=G
. That should indent all the lines in the file.1G
takes you the first line,=
will start the auto-indent and the finalG
will take you the last line in the file.