这应该很简单,甚至可能在帮助文档中提到,但我不知道如何在vi中快速缩进多行。请问应该怎么做?
使用 > 命令缩进内容。要缩进五行,输入 5>>。要标记一段文本并缩进,可以用 Vjj> 缩进三行(仅适用于 Vim)。要缩进花括号(curly-braces)块,将光标放在一个花括号上,使用 >% 或从块内任何位置使用 >iB。
如果您需要复制文本块并需要在新位置对齐缩进,则使用 ]p 而不是只使用 p。这将使粘贴的文本块与周围的文本对齐。
此外,shiftwidth
设置可让您控制缩进的空格数。
这篇答案总结了该问题的其他答案和评论,并根据Vim文档和Vim维基提供了额外信息。为了简洁起见,此答案不区分Vi和Vim特定命令。
在下面的命令中,“重新缩进”意味着“根据您的缩进设置缩进行。”shiftwidth
是控制缩进的主要变量。
常规命令
>> Indent line by shiftwidth spaces
<< De-indent line by shiftwidth spaces
5>> Indent 5 lines
5== Re-indent 5 lines
>% Increase indent of a braced or bracketed block (place cursor on brace first)
=% Reindent a braced or bracketed block (cursor on brace)
<% Decrease indent of a braced or bracketed block (cursor on brace)
]p Paste text, aligning indentation with surroundings
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
>i{ Increase inner block indent
<i{ Decrease inner block indent
你可以用 }
或 B
替换 {
, 比如说 =iB
是一个有效的块缩进命令。请查看 "缩进代码块"来试用这些命令的好例子。. Repeat last command
因此缩进命令可以轻松方便地重复使用。
重新缩进整个文件
另一种常见情况是需要在整个源文件中修复缩进:
gg=G Re-indent entire buffer
你可以将这个想法扩展到多个文件:" Re-indent all your C source code:
:args *.c
:argdo normal gg=G
:wall
或者是多个缓冲区:
" Re-indent all open buffers:
:bufdo normal gg=G:wall
在可视化模式下
Vjj> Visually mark and then indent three lines
在插入模式下
这些命令适用于当前行:
CTRL-t insert indent at start of line
CTRL-d remove indent at start of line
0 CTRL-d remove all indentation from line
Ex命令
当你想要缩进一定范围的行但不想移动光标时,这些命令很有用。
:< and :> Given a range, apply indentation e.g.
:4,8> indent lines 4 to 8, inclusive
使用标记进行缩进
另一种方法是通过标记实现:
ma Mark top of block to indent as marker 'a'
...将光标移动到末尾位置
>'a Indent from marker 'a' to current location
控制缩进的变量
您可以在您的.vimrc文件中进行设置。
set expandtab "Use softtabstop spaces instead of tab characters for indentation
set shiftwidth=4 "Indent by 4 spaces when using >>, <<, == etc.
set softtabstop=4 "Indent by 4 spaces when pressing <TAB>
set autoindent "Keep indentation from previous line
set smartindent "Automatically inserts indentation in some cases
set cindent "Like smartindent, but stricter and more customisable
Vim会根据文件类型智能缩进。尝试将以下内容添加到你的 .vimrc 文件中:
if has ("autocmd")
" File type detection. Indent based on filetype. Recommended.
filetype plugin indent on
endif
参考资料
>}
时,这非常有用。非常感谢。 - Amit一个很大的选择会是:
gg=G
它真的很快,而且所有东西都会缩进;-)
set cindent
应该在vimrc中设置,或者在运行该命令之前运行:set cindent
。 - Amanuel Nega针对C语言代码的缩进,请尝试以下方法。更多信息请键入:help =
:
={
这将自动缩进您当前所在的代码块。
或者只需输入:
==
以自动缩进当前行。
视觉型人群的按键:
进入命令模式:
Esc
移动到要缩进的区域开头:
hjkl↑↓←→
开始一个块:
v
移动到要缩进的区域结尾:
hjkl↑↓←→
(可选)键入您想要的缩进级别数字:
0..9
对该块进行缩进:
>
ma
,向下移动光标直到需要的位置,然后键入>'a
(注意,“a
”可以替换为任何有效的标记名称)。这有时比5>>
或vjjj>
更容易。最强大的命令是
gg=G
这会给整个文件进行缩进!
以下是在Vim或gVim中快速缩进行的一些简单而优雅的命令:
对当前行进行缩进
==
对当前行下面所有行进行缩进
=G
对当前行下方n行进行缩进
n==
例如,要对当前行下面的4行进行缩进
4==
要缩进代码块,请移动到其中之一的括号并使用命令
=%
这些是缩进多行的最简单但强大的命令。
vim
,不适用于 vi
。 - rojomokevi
的 vim
吗? - rojomoke当您选择一个块并使用>进行缩进时,它会缩进然后返回正常模式。我在我的.vimrc文件中有以下内容:
vnoremap < <gv
vnoremap > >gv
它允许您将所选内容缩进任意次数。
回到文本开头
除了提供的解决方案外,我喜欢逐段进行操作,并使用>}
代码。
shiftwidth
设置以更改缩进方式。 - Greg Hewgill