vi 中快速缩进多行
它应该是微不足道的,甚至可能在帮助中,但我不知道如何导航它。 如何在vi中快速缩进多行?
It should be trivial, and it might even be in the help, but I can't figure out how to navigate it. How do I indent multiple lines quickly in vi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
例如,
增加“>” 更多选项卡的符号。
例如,
For example,
Increase the '>' symbol for more tabs.
For example,
执行此操作:
并添加此行:
这仅适用于 cpp 文件。 您可以对其他文件类型执行此操作,也只需修改文件类型...
Do this:
And add this line:
This is only for a cpp file. You can do this for another file type, also just by modifying the filetype...
使用
VISUAL MODE
快速完成此操作的过程与注释代码块相同。如果您不想更改
shiftwidth
或使用任何set
指令,并且足够灵活,可以使用 TABS 或 SPACES 或任何其他字符,那么这非常有用。-- VISUAL MODE --
:
切换到提示符Replacing with 3 个前导空格:
:'<,'>s/^/ /g
或替换为前导制表符:
:'<,'>s/^/\t/g
简要说明:
'<,'>
- 在视觉选择的范围内s/^/ /g
- 在整个范围内每行的开头插入 3 个空格(或)
s/^/\t/g
- 在整个范围内每行的开头插入Tab
A quick way to do this using
VISUAL MODE
uses the same process as commenting a block of code.This is useful if you would prefer not to change your
shiftwidth
or use anyset
directives and is flexible enough to work with TABS or SPACES or any other character.-- VISUAL MODE --
:
to switch to the promptReplacing with 3 leading spaces:
:'<,'>s/^/ /g
Or replacing with leading tabs:
:'<,'>s/^/\t/g
Brief Explanation:
'<,'>
- Within the Visually Selected Ranges/^/ /g
- Insert 3 spaces at the beginning of every line within the whole range(or)
s/^/\t/g
- InsertTab
at the beginning of every line within the whole range>}
或>{
从当前行缩进到下一段<}
或<{
同样不缩进>}
or>{
indent from current line up to next paragraph<}
or<{
same un-indent我在评论中没有找到我使用的方法,所以我会分享它(我认为仅限Vim):
这在您不想更改时很有用vimrc 中的缩进/制表符设置或记住它们在编辑时更改它,
我使用相同的 Ctrl + V 块选择来选择。空格并用 D 删除它。
I didn't find a method I use in the comments, so I'll share it (I think Vim only):
This is useful when you don't want to change indentation/tab settings in
vimrc
or to remember them to change it while editing.To unindent I use the same Ctrl + V block select to select spaces and delete it with D.
我喜欢标记文本缩进:
PS:您可以使用 :'a,'z 技术来标记任何操作的范围(d, y, s///,等),您可能会使用行、数字或 %。
I like to mark text for indentation:
PS: you can use the :'a,'z technique to mark a range for any operation (d, y, s///, etc.) where you might use lines, numbers, or %.
对我来说,MacVim(视觉)解决方案是,用鼠标选择并按“>”,但是在将以下行放入“~/.vimrc”后,因为我喜欢空格而不是制表符:
此外,能够调用也很有用MacVim 从命令行(Terminal.app),所以因为我有以下帮助目录“~/bin”,我在其中放置了一个名为“macvim”的脚本:
当然在“~/.bashrc”中:
MacPorts 与“~/.profile”混淆很多,因此 PATH 环境变量可能会变得很长。
For me, the MacVim (Visual) solution was, select with mouse and press ">", but after putting the following lines in "~/.vimrc" since I like spaces instead of tabs:
Also it's useful to be able to call MacVim from the command-line (Terminal.app), so since I have the following helper directory "~/bin", where I place a script called "macvim":
And of course in "~/.bashrc":
MacPorts messes with "~/.profile" a lot, so the PATH environment variable can get quite long.
5==
将从当前光标位置缩进五行。因此您可以在
==
之前输入任何数字。 它将缩进行数。 这是在命令模式下。gg=G
将从上到下缩进整个文件。5==
will indent five lines from the current cursor position.So you can type any number before
==
. It will indent the number of lines. This is in command mode.gg=G
will indent the whole file from top to bottom.我不知道为什么找到这样一个简单的答案是如此困难......
我自己也花了很多功夫才知道这一点。 非常简单:
.vimrc
文件。添加此行
在文件中要正确缩进的位置。
在正常/命令模式下键入
I don’t know why it's so difficult to find a simple answer like this one...
I myself had to struggle a lot to know this. It's very simple:
.vimrc
file under the home directory.Add this line
in your file where you want to indent properly.
In normal/command mode type
:help left
在
ex 模式
中,您可以使用:left
或:le
将行对齐指定的数量。具体来说,
:left
将使[范围]中的行左对齐。 它将行中的缩进设置为 [indent](默认 0)。:%le3
或:%le 3
或:%left3< /code> 或
:%left 3
将通过填充三个空格来对齐整个文件。:5,7 le 3
将通过用三个空格填充第 5 行到第 7 行来对齐它们。:le
没有任何值或:le 0
将左对齐并填充 0。这适用于 Vim 和 gVim。
:help left
In
ex mode
you can use:left
or:le
to align lines a specified amount.Specifically,
:left
will Left align lines in the [range]. It sets the indent in the lines to [indent] (default 0).:%le3
or:%le 3
or:%left3
or:%left 3
will align the entire file by padding with three spaces.:5,7 le 3
will align lines 5 through 7 by padding them with three spaces.:le
without any value or:le 0
will left align with a padding of 0.This works in Vim and gVim.
经常使用 Python,我发现自己经常需要将块移动多个缩进。 您可以使用任何块选择方法来执行此操作,然后只需在
>
之前输入您希望跳转的缩进数,例如,
V5j3>
将将 5 行缩进 3 次 - 如果使用 4 个空格进行缩进,则为 12 个空格。Using Python a lot, I find myself needing frequently needing to shift blocks by more than one indent. You can do this by using any of the block selection methods, and then just enter the number of indents you wish to jump right before the
>
For example,
V5j3>
will indent five lines three times - which is 12 spaces if you use four spaces for indents.我使用块模式视觉选择:
I
(Shift + I) 进入插入模式。这不是一个单一任务者。 它的工作原理:
c
而不是I
)。I use block-mode visual selection:
I
(Shift + I) to enter insert mode.This is not a uni-tasker. It works:
c
instead ofI
).对于代码块,{}:= + %
对于选定的行:Shift + v 使用向上/向下箭头键选择,然后按 = 。
对于整个文件:gg + = + G
注:'gg'表示转到第1行,'='是缩进命令,'G'将光标移动到第1行文件末尾。
For a block of code, {}: = + %
For a selected line: Shift + v select using the up/down arrow keys, and then press =.
For the entire file: gg + = + G
Note: 'gg' means go to line 1, '=' is the indent command, and 'G' moves the cursor to the end of file.
对于喜欢现代编辑器使用
缩进所选行的人 -> Tab 和
-> Shift+Tab:用法:
按
V
进入逐行视觉模式,选择所需的行,然后按 Tab(可能使用 shift),然后缩进应用为您想要和选择仍然存在......For who like modern editors to indent selected line with
<TAB>
-> Tab and<S-TAB>
-> Shift+Tab:Usage:
Hit
V
for line-wise visual-mode, select lines you want, then hit Tab(maybe with shift), then indention applies as you want and selection remains...如何在 vi 中立即将突出显示的代码缩进多个空格:
选项 1:使用可视块模式将 vi 中的代码块缩进三个空格:
选择要缩进的代码块缩进。 在正常模式下使用 Ctrl+V 并向下箭头选择文本即可执行此操作。 选中后,输入
:
向所选文本块发出命令。以下内容将出现在命令行中:
:'<,'>
要将缩进设置为三个空格,请输入
le 3
并按 Enter。 出现的内容如下::'<,'>le 3
所选文本立即缩进三个空格。
选项 2:使用 Visual Line 模式将 vi 中的代码块缩进三个空格:
处于正常模式并按以下键:
<前><代码>Vjjjj:le 3
对您所做操作的解释:
V
表示开始选择文本。jjjj
向下四行箭头,突出显示四行。:
告诉 vi 您将为突出显示的文本输入指令。le 3
表示突出显示的文本缩进三行。所选代码立即增加或减少为三个空格缩进。
选项 3:使用 Visual Block 模式和特殊插入模式来增加缩进:
在正常模式下按以下键:
Ctrl+V
<前><代码>jjjj
(按空格键五次)
Esc
Shift+i
所有突出显示的文本都会额外缩进五个空格。
How to indent highlighted code in vi immediately by a number of spaces:
Option 1: Indent a block of code in vi to three spaces with Visual Block mode:
Select the block of code you want to indent. Do this using Ctrl+V in normal mode and arrowing down to select text. While it is selected, enter
:
to give a command to the block of selected text.The following will appear in the command line:
:'<,'>
To set indent to three spaces, type
le 3
and press enter. This is what appears::'<,'>le 3
The selected text is immediately indented to three spaces.
Option 2: Indent a block of code in vi to three spaces with Visual Line mode:
Be in normal mode and press the following keys:
Interpretation of what you did:
V
means start selecting text.jjjj
arrows down four lines, highlighting four lines.:
tells vi you will enter an instruction for the highlighted text.le 3
means indent highlighted text three lines.The selected code is immediately increased or decreased to three spaces indentation.
Option 3: use Visual Block mode and special insert mode to increase indent:
Be in normal mode press the following keys:
Ctrl+V
(press the spacebar five times)
Esc
Shift+i
All the highlighted text is indented an additional five spaces.
要缩进文件中的每一行,请输入 Esc,然后输入 G=gg。
To indent every line in a file type, Esc and then G=gg.
使用 > 命令。 要缩进五行,5>>。 要标记行块并缩进, Vjj> 缩进三行(仅限 Vim )。 要缩进大括号块,请将光标放在其中一个大括号上并使用 >% 或从块内的任何位置使用 >我B。
如果您要复制文本块并需要在新位置对齐文本块的缩进,请使用 ]p 而不是仅使用 p。 这会将粘贴的块与周围的文本对齐。
此外,
shiftwidth
设置允许您控制缩进多少空格。Use the > command. To indent five lines, 5>>. To mark a block of lines and indent it, Vjj> to indent three lines (Vim only). To indent a curly-braces block, put your cursor on one of the curly braces and use >% or from anywhere inside block use >iB.
If you’re copying blocks of text around and need to align the indent of a block in its new location, use ]p instead of just p. This aligns the pasted block with the surrounding text.
Also, the
shiftwidth
setting allows you to control how many spaces to indent.这个答案总结了这个问题的其他答案和评论,并根据 Vim 文档 和Vim 维基。 为了简洁起见,这个答案不区分 Vi 和 Vim 特定的命令。
在下面的命令中,“重新缩进”的意思是“根据您的缩进设置缩进行”。
shiftwidth
是控制的主要变量缩进。通用命令
您可以将
{
替换为}
或B
,例如=iB
为有效的块缩进命令。 请查看"缩进代码块",获取尝试这些命令的好示例。另外,请记住
,这样可以轻松方便地重复缩进命令。
重新缩进完整文件
另一种常见情况是需要在整个源文件中修复缩进:
您可以将此想法扩展到多个文件:
或多个缓冲区:
在可视模式下
<强>在插入模式
这些命令适用于当前行:
Ex命令
当您想要缩进特定范围的行而不移动您的行时,这些命令非常有用。
光标。
使用标记缩进
另一种方法是通过标记:
...将光标移动到结束位置
控制缩进的变量
您可以在 .vimrc 文件。
Vim 具有基于文件类型的智能缩进。 尝试将其添加到您的 .vimrc 中:
参考
:help =
This answer summarises the other answers and comments of this question, and it adds extra information based on the Vim documentation and the Vim wiki. For conciseness, this answer doesn't distinguish between Vi and Vim-specific commands.
In the commands below, "re-indent" means "indent lines according to your indentation settings."
shiftwidth
is the primary variable that controls indentation.General Commands
You can replace
{
with}
orB
, e.g.=iB
is a valid block indent command. Take a look at "Indent a Code Block" for a nice example to try these commands out on.Also, remember that
, so indentation commands can be easily and conveniently repeated.
Re-indenting complete files
Another common situation is requiring indentation to be fixed throughout a source file:
You can extend this idea to multiple files:
Or multiple buffers:
In Visual Mode
In insert mode
These commands apply to the current line:
Ex commands
These are useful when you want to indent a specific range of lines, without moving your
cursor.
Indenting using markers
Another approach is via markers:
...move cursor to end location
Variables that govern indentation
You can set these in your .vimrc file.
Vim has intelligent indentation based on filetype. Try adding this to your .vimrc:
References
:help =
一个重要的选择是:
它真的很快,而且所有内容都会缩进;-)
A big selection would be:
It is really fast, and everything gets indented ;-)
也可以尝试使用C-indenting缩进。 执行
:help =
获取更多信息:={
这将自动缩进您所在的当前代码块。
或者只是:
==
自动缩进当前行。
Also try this for C-indenting indentation. Do
:help =
for more information:={
That will auto-indent the current code block you're in.
Or just:
==
to auto-indent the current line.
视觉效果更好的人的按键:
进入命令模式:
Escape
移动到要缩进的区域的开头:
hjkl↑↓←→
开始一个区块:
v
移动到缩进区域的末尾:
hjkl↑↓←→
(可选)键入所需的缩进级别数
0..9
在块上执行缩进:
>
Key presses for more visual people:
Enter Command Mode:
Escape
Move around to the start of the area to indent:
hjkl↑↓←→
Start a block:
v
Move around to the end of the area to indent:
hjkl↑↓←→
(Optional) Type the number of indentation levels you want
0..9
Execute the indentation on the block:
>
除了已经给出的答案并接受之外,它是也可以放置一个标记,然后缩进从当前光标到标记的所有内容。
因此,在您想要缩进块顶部的位置输入
ma
,将光标向下移动到您需要的位置,然后输入>'a
(请注意,“a
" 可以替换为任何有效的标记名称)。 这有时比5>>
或vjjj>
更容易。In addition to the answer already given and accepted, it is also possible to place a marker and then indent everything from the current cursor to the marker.
Thus, enter
ma
where you want the top of your indented block, cursor down as far as you need and then type>'a
(note that "a
" can be substituted for any valid marker name). This is sometimes easier than5>>
orvjjj>
.所有命令的主人是
gg=G
这会缩进整个文件!
下面是一些用于在 Vim 或 gVim 中快速缩进的简单而优雅的命令。
缩进当前行
==
缩进当前行以下的所有行
=G
缩进当前行以下的
n
行n==< /code>
例如,要在当前行下方缩进 4 行
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 current line
==
To indent the all the lines below the current line
=G
To indent
n
lines below the current linen==
For example, to indent 4 lines below the current line
4==
To indent a block of code, go to one of the braces and use command
=%
These are the simplest, yet powerful commands to indent multiple lines.
当您选择一个块并使用 > 缩进时,它会缩进然后返回正常模式。 我的 .vimrc 文件中有这个:
它可以让您根据需要缩进您的选择多次。
When you select a block and use > to indent, it indents then goes back to normal mode. I have this in my .vimrc file:
It lets you indent your selection as many time as you want.
转到文本开头,
Go to the start of the text
除了提供的解决方案之外,我还喜欢使用
>}
一次完成一段操作As well as the offered solutions, I like to do things a paragraph at a time with
>}
假设您使用 2 个空格来缩进代码。 类型:
然后:
你明白了。
(空行不会缩进,我认为这很好。)
我在 (g)vim 缩进块文档中找到了答案:
Suppose you use 2 spaces to indent your code. Type:
Then:
You get the idea.
(Empty lines will not get indented, which I think is kind of nice.)
I found the answer in the (g)vim documentation for indenting blocks:
Vim UI 的美妙之处在于它的一致性。 编辑命令由命令和光标移动组成。
光标移动始终相同:
'
。因此,为了使用 vim,您必须学习移动光标并记住一系列命令,例如, > 缩进(以及 < 到“突出”)。
因此,要将行从光标位置缩进到屏幕顶部,您可以执行 >H、>G< /kbd> 缩进到文件底部。
如果您不是输入 >H,而是输入 dH,那么您将删除相同的行块、 cH 用于替换它等。
某些光标移动更适合特定命令。 特别是,% 命令可以方便地缩进整个 HTML 或 XML 块。 如果文件突出显示语法 (
:syn on
),则将光标设置在标记的文本中(例如,在标签,
这就是 Vim 的工作方式:只需记住光标移动和命令,以及如何混合它们。
所以我对这个问题的回答是“转到要缩进的行块的一端,然后键入 > 命令并移动到该块的另一端” if < em>indent 被解释为移动行,如果 indent 被解释为漂亮打印,则 =。
The beauty of Vim's UI is that its consistency. Editing commands are made up of the command and a cursor move.
The cursor moves are always the same:
'
.So, in order to use vim you have to learn to move the cursor and remember a repertoire of commands like, for example, > to indent (and < to "outdent").
Thus, for indenting the lines from the cursor position to the top of the screen you do >H, >G to indent to the bottom of the file.
If, instead of typing >H, you type dH then you are deleting the same block of lines, cH for replacing it, etc.
Some cursor movements fit better with specific commands. In particular, the % command is handy to indent a whole HTML or XML block. If the file has syntax highlighted (
:syn on
) then setting the cursor in the text of a tag (like, in the "i" of <div> and entering >% will indent up to the closing </div> tag.This is how Vim works: one has to remember only the cursor movements and the commands, and how to mix them.
So my answer to this question would be "go to one end of the block of lines you want to indent, and then type the > command and a movement to the other end of the block" if indent is interpreted as shifting the lines, = if indent is interpreted as in pretty-printing.
您可以使用
norm i
命令在行首插入给定文本。 要在第 2-10 行之前插入 10 个空格:请记住,命令末尾必须有一个空格字符 - 这将是我们想要插入的字符。 我们还可以用任何其他文本缩进一行,例如用五个下划线字符缩进文件中的每一行:
或者更奇特的东西:
更实际的例子是用 # 字符注释 Bash/Python/etc 代码:
重新缩进使用x而不是i。 例如,要删除每行的前 5 个字符:
You can use the
norm i
command to insert given text at the beginning of the line. To insert 10 spaces before lines 2-10:Remember that there has to be a space character at the end of the command - this will be the character we want to have inserted. We can also indent a line with any other text, for example to indent every line in a file with five underscore characters:
Or something even more fancy:
More practical example is commenting Bash/Python/etc code with # character:
To re-indent use x instead of i. For example, to remove first 5 characters from every line:
要向左缩进,请按“SHIFT + 逗号”(< 字符)。
来源: https://www .fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html
To indent left press "SHIFT + comma" (< character).
Source: https://www.fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html