vi 中快速缩进多行

发布于 2024-07-07 12:15:53 字数 52 浏览 7 评论 0原文

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

It should be trivial, and it might even be in the help, but I can't figure out how to navigate it. How do I indent multiple lines quickly in vi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

撞了怀 2024-07-14 12:15:54
:line_num_start,line_num_end>

例如,

14,21> shifts line number 14 to 21 to one tab

增加“>” 更多选项卡的符号。

例如,

14,21>>> for three tabs
:line_num_start,line_num_end>

For example,

14,21> shifts line number 14 to 21 to one tab

Increase the '>' symbol for more tabs.

For example,

14,21>>> for three tabs
往昔成烟 2024-07-14 12:15:54

执行此操作:

$vi .vimrc

并添加此行:

autocmd FileType cpp setlocal expandtab shiftwidth=4 softtabstop=4 cindent

这仅适用于 cpp 文件。 您可以对其他文件类型执行此操作,也只需修改文件类型...

Do this:

$vi .vimrc

And add this line:

autocmd FileType cpp setlocal expandtab shiftwidth=4 softtabstop=4 cindent

This is only for a cpp file. You can do this for another file type, also just by modifying the filetype...

韵柒 2024-07-14 12:15:54

使用VISUAL MODE 快速完成此操作的过程与注释代码块相同。

如果您不想更改 shiftwidth 或使用任何 set 指令,并且足够灵活,可以使用 TABS 或 SPACES 或任何其他字符,那么这非常有用。

  1. 将光标置于块 v 的开头处
  2. ,切换到 -- VISUAL MODE --
  3. 选择要缩进的文本
  4. 输入 : 切换到提示符
  5. Replacing with 3 个前导空格:

    :'<,'>s/^/ /g

  6. 或替换为前导制表符:

    :'<,'>s/^/\t/g

  7. 简要说明:

    '<,'> - 在视觉选择的范围内

    s/^/ /g - 在整个范围内每行的开头插入 3 个空格

    (或)

    s/^/\t/g - 在整个范围内每行的开头插入 Tab

A quick way to do this using VISUAL MODE uses the same process as commenting a block of code.

This is useful if you would prefer not to change your shiftwidth or use any set directives and is flexible enough to work with TABS or SPACES or any other character.

  1. Position cursor at the beginning on the block
  2. v to switch to -- VISUAL MODE --
  3. Select the text to be indented
  4. Type : to switch to the prompt
  5. Replacing with 3 leading spaces:

    :'<,'>s/^/ /g

  6. Or replacing with leading tabs:

    :'<,'>s/^/\t/g

  7. Brief Explanation:

    '<,'> - Within the Visually Selected Range

    s/^/ /g - Insert 3 spaces at the beginning of every line within the whole range

    (or)

    s/^/\t/g - Insert Tab at the beginning of every line within the whole range

过去的过去 2024-07-14 12:15:54

>}>{ 从当前行缩进到下一段

<}<{同样不缩进

>} or >{ indent from current line up to next paragraph

<} or <{ same un-indent

快乐很简单 2024-07-14 12:15:54

我在评论中没有找到我使用的方法,所以我会分享它(我认为仅限Vim):

  1. Esc 进入命令模式
  2. 移动到你想要的最后一行的第一个字符缩进
  3. Ctrl + V 开始块选择
  4. 移动到要缩进的第一行的第一个字符
  5. Shift + I 进入特殊插入模式
  6. 根据需要缩进输入尽可能多的空格/制表符(例如两个
  7. Esc ,空格将出现在所有行中

这在您不想更改时很有用vimrc 中的缩进/制表符设置或记住它们在编辑时更改它,

我使用相同的 Ctrl + V 块选择来选择。空格并用 D 删除它。

I didn't find a method I use in the comments, so I'll share it (I think Vim only):

  1. Esc to enter command mode
  2. Move to the first character of the last line you want to indent
  3. Ctrl + V to start block select
  4. Move to the first character of the first line you want to indent
  5. Shift + I to enter special insert mode
  6. Type as many space/tabs as you need to indent to (two for example
  7. Press Esc and spaces will appear in all lines

This is useful when you don't want to change indentation/tab settings in vimrc or to remember them to change it while editing.

To unindent I use the same Ctrl + V block select to select spaces and delete it with D.

灰色世界里的红玫瑰 2024-07-14 12:15:54

我喜欢标记文本缩进:

  1. 转到文本行的开头,然后输入 maa 是“m”中的标签ark:可以是任何字母)
  2. 转到文本末尾行并输入 mz (同样,z 可以是任何字母)
  3. :'a,'z>:'a,'z< 将缩进或突出(这是一个单词吗?)
  4. 瞧! 文本被移动(空行保持为空,没有空格)

PS:您可以使用 :'a,'z 技术来标记任何操作的范围(d, y, s///,等),您可能会使用行、数字或 %

I like to mark text for indentation:

  1. go to beginning of line of text then type ma (a is the label from the 'm'ark: it could be any letter)
  2. go to end line of text and type mz (again, z could be any letter)
  3. :'a,'z> or :'a,'z< will indent or outdent (is this a word?)
  4. Voila! The text is moved (empty lines remain empty with no spaces)

PS: you can use the :'a,'z technique to mark a range for any operation (d, y, s///, etc.) where you might use lines, numbers, or %.

安静 2024-07-14 12:15:54

对我来说,MacVim(视觉)解决方案是,用鼠标选择并按“>”,但是在将以下行放入“~/.vimrc”后,因为我喜欢空格而不是制表符:

set expandtab
set tabstop=2
set shiftwidth=2

此外,能够调用也很有用MacVim 从命令行(Terminal.app),所以因为我有以下帮助目录“~/bin”,我在其中放置了一个名为“macvim”的脚本:

#!/usr/bin/env bash
/usr/bin/open -a /Applications/MacPorts/MacVim.app $@

当然在“~/.bashrc”中:

export PATH=$PATH:$HOME/bin

MacPorts 与“~/.profile”混淆很多,因此 PATH 环境变量可能会变得很长。

For me, the MacVim (Visual) solution was, select with mouse and press ">", but after putting the following lines in "~/.vimrc" since I like spaces instead of tabs:

set expandtab
set tabstop=2
set shiftwidth=2

Also it's useful to be able to call MacVim from the command-line (Terminal.app), so since I have the following helper directory "~/bin", where I place a script called "macvim":

#!/usr/bin/env bash
/usr/bin/open -a /Applications/MacPorts/MacVim.app $@

And of course in "~/.bashrc":

export PATH=$PATH:$HOME/bin

MacPorts messes with "~/.profile" a lot, so the PATH environment variable can get quite long.

篱下浅笙歌 2024-07-14 12:15:54

5== 将从当前光标位置缩进五行。

因此您可以在 == 之前输入任何数字。 它将缩进行数。 这是在命令模式下。

gg=G 将从上到下缩进整个文件。

5== will indent five lines from the current cursor position.

So you can type any number before ==. It will indent the number of lines. This is in command mode.

gg=G will indent the whole file from top to bottom.

爱殇璃 2024-07-14 12:15:54

我不知道为什么找到这样一个简单的答案是如此困难......

我自己也花了很多功夫才知道这一点。 非常简单:

  • 编辑主目录下的 .vimrc 文件。
  • 添加此行

    设置 cindent 
      

    在文件中要正确缩进的位置。

  • 在正常/命令模式下键入

    10==(这将从当前光标位置缩进 10 行) 
      gg=G(完整文件将正确缩进) 
      

I don’t know why it's so difficult to find a simple answer like this one...

I myself had to struggle a lot to know this. It's very simple:

  • Edit your .vimrc file under the home directory.
  • Add this line

    set cindent
    

    in your file where you want to indent properly.

  • In normal/command mode type

    10==   (This will indent 10 lines from the current cursor location)
    gg=G   (Complete file will be properly indented)
    
指尖凝香 2024-07-14 12:15:54

:help left

ex 模式 中,您可以使用 :left:le 将行对齐指定的数量。
具体来说,:left 将使[范围]中的行左对齐。 它将行中的缩进设置为 [indent](默认 0)。

:%le3:%le 3:%left3< /code> 或 :%left 3 将通过填充三个空格来对齐整个文件。

:5,7 le 3 将通过用三个空格填充第 5 行到第 7 行来对齐它们。

:le 没有任何值或 :le 0 将左对齐并填充 0。

这适用于 Vim 和 gVim。

:help left

In ex mode you can use :left or :le to align lines a specified amount.
Specifically, :left will Left align lines in the [range]. It sets the indent in the lines to [indent] (default 0).

:%le3 or :%le 3 or :%left3 or :%left 3 will align the entire file by padding with three spaces.

:5,7 le 3 will align lines 5 through 7 by padding them with three spaces.

:le without any value or :le 0 will left align with a padding of 0.

This works in Vim and gVim.

_畞蕅 2024-07-14 12:15:54

经常使用 Python,我发现自己经常需要将块移动多个缩进。 您可以使用任何块选择方法来执行此操作,然后只需在 > 之前输入您希望跳转的缩进数,

例如,V5j3> 将将 5 行缩进 3 次 - 如果使用 4 个空格进行缩进,则为 12 个空格。

Using Python a lot, I find myself needing frequently needing to shift blocks by more than one indent. You can do this by using any of the block selection methods, and then just enter the number of indents you wish to jump right before the >

For example, V5j3> will indent five lines three times - which is 12 spaces if you use four spaces for indents.

梓梦 2024-07-14 12:15:54

我使用块模式视觉选择:

  • 转到块的前面进行移动(在顶部或底部)。
  • Ctrl + V 进入可视块模式。
  • 导航以选择行前面的列。
  • I (Shift + I) 进入插入模式。
  • 键入一些空格。
  • Esc。 所有线路都会移动。

这不是一个单一任务者。 它的工作原理:

  • 在线条中间。
  • 在所有行上插入任意字符串。
  • 更改列(使用 c 而不是 I)。
  • 猛拉、删除、替换等...

I use block-mode visual selection:

  • Go to the front of the block to move (at the top or bottom).
  • Press Ctrl + V to enter visual block mode.
  • Navigate to select a column in front of the lines.
  • Press I (Shift + I) to enter insert mode.
  • Type some spaces.
  • Press Esc. All lines will shift.

This is not a uni-tasker. It works:

  • In the middle of lines.
  • To insert any string on all lines.
  • To change a column (use c instead of I).
  • yank, delete, substitute, etc...
夏日浅笑〃 2024-07-14 12:15:54
  • 对于代码块,{}:= + %

  • 对于选定的行:Shift + v 使用向上/向下箭头键选择,然后按 =

  • 对于整个文件:gg + = + G

注:'gg'表示转到第1行,'='是缩进命令,'G'将光标移动到第1行文件末尾

  • For a block of code, {}: = + %

  • For a selected line: Shift + v select using the up/down arrow keys, and then press =.

  • For the entire file: gg + = + G

Note: 'gg' means go to line 1, '=' is the indent command, and 'G' moves the cursor to the end of file.

悲凉≈ 2024-07-14 12:15:54

对于喜欢现代编辑器使用 缩进所选行的人 -> Tab -> Shift+Tab

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

用法:
V 进入逐行视觉模式,选择所需的行,然后按 Tab(可能使用 shift),然后缩进应用为您想要和选择仍然存在......

For who like modern editors to indent selected line with <TAB> -> Tab and <S-TAB> -> Shift+Tab:

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

Usage:
Hit V for line-wise visual-mode, select lines you want, then hit Tab(maybe with shift), then indention applies as you want and selection remains...

娇柔作态 2024-07-14 12:15:54

如何在 vi 中立即将突出显示的代码缩进多个空格:

选项 1:使用可视块模式将 vi 中的代码块缩进三个空格:

  1. 选择要缩进的代码块缩进。 在正常模式下使用 Ctrl+V 并向下箭头选择文本即可执行此操作。 选中后,输入 : 向所选文本块发出命令。

  2. 以下内容将出现在命令行中: :'<,'>

  3. 要将缩进设置为三个空格,请输入 le 3 并按 Enter。 出现的内容如下: :'<,'>le 3

  4. 所选文本立即缩进三个空格。

选项 2:使用 Visual Line 模式将 vi 中的代码块缩进三个空格:

  1. 在 vi 中打开文件。
  2. 将光标放在某些代码上
  3. 处于正常模式并按以下键:

    <前><代码>Vjjjj:le 3

    对您所做操作的解释:

    V 表示开始选择文本。

    jjjj 向下四行箭头,突出显示四行。

    : 告诉 vi 您将为突出显示的文本输入指令。

    le 3 表示突出显示的文本缩进三行。

    所选代码立即增加或减少为三个空格缩进。

选项 3:使用 Visual Block 模式和特殊插入模式来增加缩进:

  1. 在 vi 中打开文件。
  2. 将光标放在某些代码上
  3. 在正常模式下按以下键:

    Ctrl+V

    <前><代码>jjjj

    (按空格键五次)

    Esc
    Shift+i

    所有突出显示的文本都会额外缩进五个空格。

How to indent highlighted code in vi immediately by a number of spaces:

Option 1: Indent a block of code in vi to three spaces with Visual Block mode:

  1. Select the block of code you want to indent. Do this using Ctrl+V in normal mode and arrowing down to select text. While it is selected, enter : to give a command to the block of selected text.

  2. The following will appear in the command line: :'<,'>

  3. To set indent to three spaces, type le 3 and press enter. This is what appears: :'<,'>le 3

  4. The selected text is immediately indented to three spaces.

Option 2: Indent a block of code in vi to three spaces with Visual Line mode:

  1. Open your file in vi.
  2. Put your cursor over some code
  3. Be in normal mode and press the following keys:

    Vjjjj:le 3
    

    Interpretation of what you did:

    V means start selecting text.

    jjjj arrows down four lines, highlighting four lines.

    : tells vi you will enter an instruction for the highlighted text.

    le 3 means indent highlighted text three lines.

    The selected code is immediately increased or decreased to three spaces indentation.

Option 3: use Visual Block mode and special insert mode to increase indent:

  1. Open your file in vi.
  2. Put your cursor over some code
  3. Be in normal mode press the following keys:

    Ctrl+V

    jjjj
    

    (press the spacebar five times)

    Esc
    Shift+i

    All the highlighted text is indented an additional five spaces.

眼中杀气 2024-07-14 12:15:54

要缩进文件中的每一行,请输入 Esc,然后输入 G=gg

To indent every line in a file type, Esc and then G=gg.

画离情绘悲伤 2024-07-14 12:15:53

使用 > 命令。 要缩进五行,5>>。 要标记行块并缩进, Vjj> 缩进三行(仅限 Vim )。 要缩进大括号块,请将光标放在其中一个大括号上并使用 >% 或从块内的任何位置使用 >B

如果您要复制文本块并需要在新位置对齐文本块的缩进,请使用 ]p 而不是仅使用 p。 这会将粘贴的块与周围的文本对齐。

此外, shiftwidth 设置允许您控制缩进多少空格。

Use the > command. To indent five lines, 5>>. To mark a block of lines and indent it, Vjj> to indent three lines (Vim only). To indent a curly-braces block, put your cursor on one of the curly braces and use >% or from anywhere inside block use >iB.

If you’re copying blocks of text around and need to align the indent of a block in its new location, use ]p instead of just p. This aligns the pasted block with the surrounding text.

Also, the shiftwidth setting allows you to control how many spaces to indent.

抱着落日 2024-07-14 12:15:53

这个答案总结了这个问题的其他答案和评论,并根据 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

参考

This answer summarises the other answers and comments of this question, and it adds extra information based on the Vim documentation and the Vim wiki. For conciseness, this answer doesn't distinguish between Vi and Vim-specific commands.

In the commands below, "re-indent" means "indent lines according to your indentation settings." shiftwidth is the primary variable that controls indentation.

General Commands

>>   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

You can replace { with } or B, e.g. =iB is a valid block indent command. Take a look at "Indent a Code Block" for a nice example to try these commands out on.

Also, remember that

.    Repeat last command

, so indentation commands can be easily and conveniently repeated.

Re-indenting complete files

Another common situation is requiring indentation to be fixed throughout a source file:

gg=G  Re-indent entire buffer

You can extend this idea to multiple files:

" Re-indent all your C source code:
:args *.c
:argdo normal gg=G
:wall

Or multiple buffers:

" Re-indent all open buffers:
:bufdo normal gg=G:wall

In Visual Mode

Vjj> Visually mark and then indent three lines

In insert mode

These commands apply to the current line:

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 commands

These are useful when you want to indent a specific range of lines, without moving your
cursor.

:< and :> Given a range, apply indentation e.g.
:4,8>   indent lines 4 to 8, inclusive

Indenting using markers

Another approach is via markers:

ma     Mark top of block to indent as marker 'a'

...move cursor to end location

>'a    Indent from marker 'a' to current location

Variables that govern indentation

You can set these in your .vimrc file.

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 has intelligent indentation based on filetype. Try adding this to your .vimrc:

if has ("autocmd")
    " File type detection. Indent based on filetype. Recommended.
    filetype plugin indent on
endif

References

站稳脚跟 2024-07-14 12:15:53

一个重要的选择是:

gg=G

它真的很快,而且所有内容都会缩进;-)

A big selection would be:

gg=G

It is really fast, and everything gets indented ;-)

陈甜 2024-07-14 12:15:53

也可以尝试使用C-indenting缩进。 执行 :help = 获取更多信息:

={

这将自动缩进您所在的当前代码块。

或者只是:

==

自动缩进当前行。

Also try this for C-indenting indentation. Do :help = for more information:

={

That will auto-indent the current code block you're in.

Or just:

==

to auto-indent the current line.

剧终人散尽 2024-07-14 12:15:53

视觉效果更好的人的按键:

  1. 进入命令模式:
    Escape

  2. 移动到要缩进的区域的开头:
    hjkl

  3. 开始一个区块:
    v

  4. 移动到缩进区域的末尾:
    hjkl

  5. (可选)键入所需的缩进级别数
    0..9

  6. 在块上执行缩进:
    >

Key presses for more visual people:

  1. Enter Command Mode:
    Escape

  2. Move around to the start of the area to indent:
    hjkl

  3. Start a block:
    v

  4. Move around to the end of the area to indent:
    hjkl

  5. (Optional) Type the number of indentation levels you want
    0..9

  6. Execute the indentation on the block:
    >

空宴 2024-07-14 12:15:53

除了已经给出的答案并接受之外,它是也可以放置一个标记,然后缩进从当前光标到标记的所有内容。

因此,在您想要缩进块顶部的位置输入 ma,将光标向下移动到您需要的位置,然后输入 >'a (请注意,“a" 可以替换为任何有效的标记名称)。 这有时比 5>>vjjj> 更容易。

In addition to the answer already given and accepted, it is also possible to place a marker and then indent everything from the current cursor to the marker.

Thus, enter ma where you want the top of your indented block, cursor down as far as you need and then type >'a (note that "a" can be substituted for any valid marker name). This is sometimes easier than 5>> or vjjj>.

<逆流佳人身旁 2024-07-14 12:15:53

所有命令的主人是

gg=G

这会缩进整个文件!

下面是一些用于在 Vim 或 gVim 中快速缩进的简单而优雅的命令。

缩进当前行

==

缩进当前行以下的所有行

=G

缩进当前行以下的 n

n==< /code>

例如,要在当前行下方缩进 4 行

4==

要缩进代码块,请转到大括号之一并使用命令

=%

这些是最简单但功能强大的多行缩进命令。

The master of all commands is

gg=G

This indents the entire file!

And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.

To indent the current line

==

To indent the all the lines below the current line

=G

To indent n lines below the current line

n==

For example, to indent 4 lines below the current line

4==

To indent a block of code, go to one of the braces and use command

=%

These are the simplest, yet powerful commands to indent multiple lines.

痴情 2024-07-14 12:15:53

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

vnoremap < <gv

vnoremap > >gv

它可以让您根据需要缩进您的选择多次。

When you select a block and use > to indent, it indents then goes back to normal mode. I have this in my .vimrc file:

vnoremap < <gv

vnoremap > >gv

It lets you indent your selection as many time as you want.

遗心遗梦遗幸福 2024-07-14 12:15:53

转到文本开头,

  • v 进入可视模式。
  • 使用向上/向下箭头突出显示文本。
  • = 缩进所有突出显示的行。

Go to the start of the text

  • press v for visual mode.
  • use up/down arrow to highlight text.
  • press = to indent all the lines you highlighted.
晨曦慕雪 2024-07-14 12:15:53

除了提供的解决方案之外,我还喜欢使用 >} 一次完成一段操作

As well as the offered solutions, I like to do things a paragraph at a time with >}

音盲 2024-07-14 12:15:53

假设您使用 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{move-around}3>” (将第 3 行缩进移动到
右)。

Suppose you use 2 spaces to indent your code. Type:

:set shiftwidth=2
  • Type v (to enter visual block editing mode)
  • Move the cursor with the arrow keys (or with h/j/k/l) to highlight the lines you want to indent or unindent.

Then:

  • Type > to indent once (2 spaces).
  • Type 2> to indent twice (4 spaces).
  • Type 3> to indent thrice (6 spaces).
  • ...
  • Type < to unindent once (2 spaces).
  • Type 2< to unindent twice (4 spaces).
  • Type 3< to unindent thrice (6 spaces).
  • ...

You get the idea.

(Empty lines will not get indented, which I think is kind of nice.)


I found the answer in the (g)vim documentation for indenting blocks:

:help visual-block
/indent

If you want to give a count to the command, do this just before typing
the operator character: "v{move-around}3>" (move lines 3 indents to
the right).

我做我的改变 2024-07-14 12:15:53

Vim UI 的美妙之处在于它的一致性。 编辑命令由命令和光标移动组成。
光标移动始终相同:

  • H 到屏幕顶部,L 到底部,M 到中间
  • n G 转到第 n 行,G 单独转到文件底部,gg 到顶部
  • n 移至下一个搜索匹配,N 到上一个
  • } 到段落末尾
  • % 到下一个匹配括号,括号或标记类型
  • enter 到下一行
  • 'x 标记 x,其中x 是一个字母或另一个'
  • 更多,包括用于单词的 wW,用于行提示的 $0 等,这不适用于此处,因为不是线运动。

因此,为了使用 vim,您必须学习移动光标并记住一系列命令,例如, > 缩进(以及 < 到“突出”)。

因此,要将行从光标位置缩进到屏幕顶部,您可以执行 >H>G< /kbd> 缩进到文件底部。

如果您不是输入 >H,而是输入 dH,那么您将删除相同的行块、 cH 用于替换它等。

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

的“i”中并输入 >% 将缩进到结束

标签,

这就是 Vim 的工作方式:只需记住光标移动和命令,以及如何混合它们。
所以我对这个问题的回答是“转到要缩进的行块的一端,然后键入 > 命令并移动到该块的另一端” if < em>indent 被解释为移动行,如果 indent 被解释为漂亮打印,则 =

The beauty of Vim's UI is that its consistency. Editing commands are made up of the command and a cursor move.
The cursor moves are always the same:

  • H to top of screen, L to bottom, M to middle
  • nG to go to line n, G alone to bottom of file, gg to top
  • n to move to next search match, N to previous
  • } to end of paragraph
  • % to next matching bracket, either of the parentheses or the tag kind
  • enter to the next line
  • 'x to mark x where x is a letter or another '.
  • many more, including w and W for word, $ or 0 to tips of the line, etc., that don't apply here because are not line movements.

So, in order to use vim you have to learn to move the cursor and remember a repertoire of commands like, for example, > to indent (and < to "outdent").

Thus, for indenting the lines from the cursor position to the top of the screen you do >H, >G to indent to the bottom of the file.

If, instead of typing >H, you type dH then you are deleting the same block of lines, cH for replacing it, etc.

Some cursor movements fit better with specific commands. In particular, the % command is handy to indent a whole HTML or XML block. If the file has syntax highlighted (:syn on) then setting the cursor in the text of a tag (like, in the "i" of <div> and entering >% will indent up to the closing </div> tag.

This is how Vim works: one has to remember only the cursor movements and the commands, and how to mix them.
So my answer to this question would be "go to one end of the block of lines you want to indent, and then type the > command and a movement to the other end of the block" if indent is interpreted as shifting the lines, = if indent is interpreted as in pretty-printing.

携余温的黄昏 2024-07-14 12:15:53

您可以使用 norm i 命令在行首插入给定文本。 要在第 2-10 行之前插入 10 个空格:

:2,10norm 10i 

请记住,命令末尾必须有一个空格字符 - 这将是我们想要插入的字符。 我们还可以用任何其他文本缩进一行,例如用五个下划线字符缩进文件中的每一行:

:%norm 5i_

或者更奇特的东西:

:%norm 2i[ ]

更实际的例子是用 # 字符注释 Bash/Python/etc 代码:

:1,20norm i#

重新缩进使用x而不是i。 例如,要删除每行的前 5 个字符:

:%norm 5x

You can use the norm i command to insert given text at the beginning of the line. To insert 10 spaces before lines 2-10:

:2,10norm 10i 

Remember that there has to be a space character at the end of the command - this will be the character we want to have inserted. We can also indent a line with any other text, for example to indent every line in a file with five underscore characters:

:%norm 5i_

Or something even more fancy:

:%norm 2i[ ]

More practical example is commenting Bash/Python/etc code with # character:

:1,20norm i#

To re-indent use x instead of i. For example, to remove first 5 characters from every line:

:%norm 5x
や莫失莫忘 2024-07-14 12:15:53
  1. 按“SHIFT + v”进入VISUAL LINE 模式。
  2. 使用光标键或“j”和“k”键选择要缩进的文本。
  3. 要向右缩进,请按“SHIFT + 点”(> 字符)。
    要向左缩进,请按“SHIFT + 逗号”(< 字符)。

来源: https://www .fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html

  1. Press "SHIFT + v" to enter VISUAL LINE mode.
  2. Select the text you wish to indent but using either the cursor keys or the "j" and "k" keys.
  3. To indent right press "SHIFT + dot" (> character).
    To indent left press "SHIFT + comma" (< character).

Source: https://www.fir3net.com/UNIX/General/how-do-i-tab-multiple-lines-within-vi.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文