它应该很小,甚至可能在帮助中,但我不知道如何导航它。如何在vi中快速缩进多行?


当前回答

这个答案总结了这个问题的其他答案和评论,并根据Vim文档和Vim wiki添加了额外信息。为了简洁起见,这个答案没有区分Vi和Vim特定命令。

在下面的命令中,“reindent”表示“根据缩进设置缩进行”。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

工具书类

缩进代码块目视移动块缩进源代码:帮助=

其他回答

这个答案总结了这个问题的其他答案和评论,并根据Vim文档和Vim wiki添加了额外信息。为了简洁起见,这个答案没有区分Vi和Vim特定命令。

在下面的命令中,“reindent”表示“根据缩进设置缩进行”。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

工具书类

缩进代码块目视移动块缩进源代码:帮助=

当您选择一个块并使用>缩进时,它将缩进,然后返回正常模式。我的.vimrc文件中有这个:

vnoremap < <gv

vnoremap > >gv

它使您可以根据需要多次缩进所选内容。

对于喜欢使用<TAB>->TAB和<S-TAB>->Shift+TAB缩进选定行的现代编辑器:

vnoremap <TAB> >gv
vnoremap <S-TAB> <gv

用法:按V键进入逐行视觉模式,选择所需的行,然后按Tab键(可能有移位),然后按需应用缩进,选择仍保留。。。

Vim UI的优点在于其一致性。编辑命令由命令和光标移动组成。光标移动始终相同:

H到屏幕顶部,L到底部,M到中间nG到第n行,G单独到文件底部,gg到顶部n移动到下一个搜索匹配,n移动到上一个}至段落末尾%到下一个匹配的括号,括号或标记类型进入下一行“x标记x,其中x是字母或其他字母”。还有很多,包括单词的w和w、行尖的$或0等,这些都不适用于这里,因为它们不是行移动。

因此,为了使用vim,您必须学会移动光标并记住一系列命令,例如,>to indent(和<to“outdent”)。

因此,要将光标位置的行缩进到屏幕顶部,请执行>H,>G以缩进到文件底部。

如果不是键入>H,而是键入dH,则删除相同的行块,cH替换它,等等。

某些光标移动更适合特定命令。特别是,%命令可以方便地缩进整个HTML或XML块。如果文件突出显示了语法(:syn on),则在标记的文本中设置光标(例如,在<div>的“i”中,输入>%将缩进到最后的</div>标记。

这就是Vim的工作原理:只需要记住光标移动和命令,以及如何混合它们。因此,我对这个问题的回答是:如果缩进被解释为移动行,=如果缩进被理解为在漂亮的打印中,那么“转到要缩进的行块的一端,然后键入>命令并移动到块的另一端”。

使用VISUAL MODE的快速方法使用与注释代码块相同的过程。

如果您希望不更改shiftwidth或使用任何set指令,并且足够灵活,可以使用TABS或SPACE或任何其他字符,则此选项非常有用。

将光标定位在块的开头v切换到--视觉模式--选择要缩进的文本类型:切换到提示替换为3个前导空格::'<,'>s/^//g或替换为前导制表符::'<,'>s/^/\t/g简要说明:“<,”>-在可视选定范围内s/^//g-在整个范围内的每行开头插入3个空格(或)s/^/\t/g-在整个范围内每行的开头插入制表符