它应该很小,甚至可能在帮助中,但我不知道如何导航它。如何在vi中快速缩进多行?
当前回答
除了提供的解决方案之外,我喜欢用>}一段一段地做事情
其他回答
要缩进文件类型中的每一行,请按Esc,然后按G=gg。
这个答案总结了这个问题的其他答案和评论,并根据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
工具书类
缩进代码块目视移动块缩进源代码:帮助=
我喜欢为缩进标记文本:
转到文本行的开头,然后键入ma(a是“m”标记的标签:可以是任何字母)转到文本的最后一行并键入mz(同样,z可以是任何字母):'a,'z>或:'a,'z<将缩进或突出(这是一个词吗?)瞧!文本被移动(空行保持为空,没有空格)
PS:您可以使用:'a,'z技术标记任何操作(d,y,s////等)的范围,其中可能使用行、数字或%。
可以使用norm i命令在行的开头插入给定的文本。要在第2-10行前插入10个空格:
:2,10norm 10i
请记住,命令末尾必须有一个空格字符——这将是我们想要插入的字符。我们还可以用任何其他文本缩进一行,例如用五个下划线字符缩进文件中的每一行:
:%norm 5i_
或者更花哨的东西:
:%norm 2i[ ]
更实际的例子是用#字符注释Bash/Python/etc代码:
:1,20norm i#
要重新缩进,请使用x而不是i。例如,要从每行中删除前5个字符:
:%norm 5x
如何将vi中突出显示的代码立即缩进许多空格:
选项1:使用Visual block模式将vi中的代码块缩进三个空格:
选择要缩进的代码块。在正常模式下使用Ctrl+V并向下箭头选择文本。选中时,输入:向选定文本块发出命令。命令行中将显示以下内容::“<,”>要将缩进设置为三个空格,请键入le 3并按enter键。这是出现的::“<,”>le 3所选文本立即缩进到三个空格。
选项2:使用Visual Line模式将vi中的代码块缩进三个空格:
在vi中打开文件。将光标放在一些代码上处于正常模式并按下以下键:Vjjj:le 3你所做的解释:V表示开始选择文本。jjjj箭头向下四行,突出显示四行。:告诉vi您将为高亮显示的文本输入说明。le 3表示缩进突出显示的文本三行。所选代码立即增加或减少到三个空格缩进。
选项3:使用视觉块模式和特殊插入模式来增加缩进:
在vi中打开文件。将光标放在一些代码上在正常模式下,按下以下键:Ctrl+V组合键朝觐(按空格键五次)Esc公司Shift+i键所有突出显示的文本将缩进五个空格。