应该是微不足道的,它甚至可能在帮助中,但我无法弄清楚如何导航它。如何在vi中快速缩进多行?
应该是微不足道的,它甚至可能在帮助中,但我无法弄清楚如何导航它。如何在vi中快速缩进多行?
使用 > 命令。要缩进5行, 五>>。标记一条线并缩进它, VĴĴ> 缩进3行(仅限vim)。要缩进花括号块,请将光标放在其中一个花括号上并使用 >%。
如果要复制文本块并需要在新位置对齐块的缩进,请使用 ]p 而不仅仅是 p。这会将粘贴的块与周围的文本对齐。
而且, shiftwidth
设置允许您控制缩进的空格数。
使用 > 命令。要缩进5行, 五>>。标记一条线并缩进它, VĴĴ> 缩进3行(仅限vim)。要缩进花括号块,请将光标放在其中一个花括号上并使用 >%。
如果要复制文本块并需要在新位置对齐块的缩进,请使用 ]p 而不仅仅是 p。这会将粘贴的块与周围的文本对齐。
而且, shiftwidth
设置允许您控制缩进的空格数。
这个答案总结了这个问题的其他答案和评论,并添加了基于的问题的额外信息 Vim文档 和 Vim wiki。为简明起见,此答案不区分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 3 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
参考
一个很大的选择
gg=G
它真的很快,一切都缩进了;-)
也尝试这个 C-缩进 缩进,做 :help =
了解更多信息:
={
这将自动缩进您当前的代码块。
要不就:
==
自动缩进当前行。
为更多视觉人士提供按键:
进入命令模式:
逃逸
移动到区域的开头以缩进:
HĴķ升↑↓←→
开始一个块:
v
移动到该区域的末尾以缩进:
HĴķ升↑↓←→
(可选)键入所需的缩进级别数
0..9
在块上执行缩进:
>
除了已经给出和接受的答案之外,还可以放置标记然后缩进从当前光标到标记的所有内容。因此,输入 ma
你想要缩进块顶部的位置,根据需要将光标向下,然后键入 >'a
(注意 ”a
“可以代替任何有效的标记名称。”这有时比比较容易 5>>
要么 vjjj>
。
所有命令的主人都是
gg=G
这会缩进整个文件!
下面是一些简单而优雅的命令,用于在Vim或gVim中快速缩进行。
缩进当前行
==
缩进当前行下面的所有行
=G
缩进 n
当前行下方的行
n==
例如,缩进当前行下面的4行
4==
要缩进代码块,请转到其中一个大括号并使用命令
=%
这些是缩进多行的最简单但功能最强大的命令。
转到文本的开头
除了提供的解决方案,我喜欢一次做一个段落 >}
当您选择一个块并使用>缩进时,它会缩进然后返回到正常模式。我在.vimrc中有这个:
vnoremap < <gv
vnoremap > >gv
它允许您根据需要随意缩进选择。