它应该很小,甚至可能在帮助中,但我不知道如何导航它。如何在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
工具书类
缩进代码块目视移动块缩进源代码:帮助=
其他回答
一个很大的选择是:
gg=G
它真的很快,所有东西都会缩进;-)
更多视觉人的按键:
进入命令模式:逃跑移动到要缩进的区域的开头:hjkl公司↑↓←→启动块:v移动到要缩进的区域末端:hjkl公司↑↓←→(可选)键入所需的缩进级别数0..9在块上执行缩进:>
对我来说,MacVim(Visual)解决方案是用鼠标选择并按“>”,但在“~/.vimrc”中输入以下行后,因为我喜欢空格而不是制表符:
set expandtab
set tabstop=2
set shiftwidth=2
此外,能够从命令行(Terminal.app)调用MacVim也是很有用的,因为我有以下助手目录“~/bin”,我在其中放置了一个名为“MacVim”的脚本:
#!/usr/bin/env bash
/usr/bin/open -a /Applications/MacPorts/MacVim.app $@
当然,在“~/.bashrc”中:
export PATH=$PATH:$HOME/bin
MacPorts经常使用“~/.profile”,因此PATH环境变量可能会很长。
我没有在评论中找到我使用的方法,所以我将分享它(我认为只有Vim):
按Esc键进入命令模式移动到要缩进的最后一行的第一个字符Ctrl+V开始块选择移动到要缩进的第一行的第一个字符Shift+I进入特殊插入模式键入需要缩进的空格/制表符(例如两个按Esc键,所有行中将显示空格
当您不想在vimrc中更改缩进/制表符设置或在编辑时记住它们来更改时,这非常有用。
为了取消缩进,我使用相同的Ctrl+V块选择来选择空间,然后用D删除它。
假设您使用2个空格缩进代码。类型:
:set shiftwidth=2
键入v(进入视觉块编辑模式)使用箭头键(或使用h/j/k/l)移动光标以高亮显示要缩进或不缩进的行。
然后:
键入>以缩进一次(2个空格)。键入2>以缩进两次(4个空格)。键入3>以缩进三次(6个空格)。...键入<以取消缩进一次(2个空格)。类型2<取消缩进两次(4个空格)。类型3<至无凹痕三次(6个空格)。...
你明白了。
(空行不会缩进,我觉得这有点不错。)
我在(g)vim文档中找到了缩进块的答案:
:help visual-block
/indent
如果要给命令计数,请在键入之前执行此操作运算符字符:“v{movearound}3>”(将第3行缩进到右侧)。