How can I quickly indent multiple lines in the vi text editor? What are the most efficient methods for indenting blocks of code or text in vi, and are there any specific commands or visual mode techniques that make this process faster?
To quickly indent multiple lines in vi, you can use visual mode to select a block of text and then the > command to indent or < to unindent, or use normal mode commands with line numbers and the >> and << operators for efficient block indentation. The most efficient methods involve combining visual selection with indentation commands or using range-based operations in normal mode for precise control over large text blocks.
Contents
- Visual Mode Techniques
- Normal Mode Methods
- Advanced Indentation Strategies
- Customizing Indentation Behavior
- Tips for Efficient Workflow
Visual Mode Techniques
Visual mode in vi provides an intuitive way to select text blocks and apply indentation operations. The most common approach is to use the > command after selecting your text block.
Basic Visual Line Selection
To indent multiple lines using visual line mode:
- Press
V(capital V) to enter visual line mode - Use arrow keys or
j/kto select the desired lines - Press
>to indent one level or<to unindent - Repeat the command with
.for additional indentation
For example, if you want to indent lines 5-10:
V5G> # Select from current line to line 5, then indent
V10G> # Select from current line to line 10, then indent
Block Visual Mode
For column-based indentation:
- Press
Ctrl+Vto enter block visual mode - Select the columns you want to affect
- Press
>to indent the selected block
This is particularly useful for aligning code or creating consistent indentation in specific columns.
Normal Mode Methods
Normal mode offers powerful range-based commands for precise indentation control without visual selection.
Range-Based Indentation
The most efficient method for indenting specific line ranges:
5,10>> # Indent lines 5 through 10 one level
5,10<< # Unindent lines 5 through 10 one level
5,10>>> # Indent lines 5 through 10 two levels
Current Line Indentation
For quick single-line operations:
>> # Indent current line one level
<< # Unindent current line one level
5>> # Indent current line 5 levels
Multiple Line Operations
To indent from current line to end or beginning:
.,$>> # Indent from current line to end of file
1,.>> # Indent from beginning of file to current line
Advanced Indentation Strategies
Using Marks for Repeated Operations
Set marks for frequently indented sections:
ma # Set mark 'a' at current position
j10>> # Indent next 10 lines
'a # Jump back to mark 'a```
### Search-Based Indentation
Indent lines matching a pattern:
```vim
:g/^\s*if/>> # Indent all lines starting with "if"
:g/^\s*{/>> # Indent all lines starting with "{"
Macro-Based Indentation
Record and replay indentation sequences:
qq # Start recording macro in register q
V>> # Visual line mode and indent
q # Stop recording
@q # Replay the macro
@@ # Replay last macro again
Customizing Indentation Behavior
Setting Indentation Levels
Control tab width and indentation behavior:
:set shiftwidth=4 # Set indentation to 4 spaces
:set tabstop=8 # Set tab width to 8 spaces
:set expandtab # Convert tabs to spaces
:set autoindent # Enable auto-indentation
:set smartindent # Enable smart indentation
Language-Specific Settings
Different languages may require different indentation approaches:
:set cindent # C-style indentation
:set smartindent # Smart indentation for various languages
:set filetype=python # Enable Python-specific indentation
Indent File Types
Automatically apply indentation settings based on file type:
filetype indent on # Enable filetype-specific indentation
Tips for Efficient Workflow
Quick Indentation Shortcuts
==- Auto-indent current line based on contextgg=G- Indent entire file!}fmt- Format paragraph using external formatter=i{- Auto-indent inside current block
Repetition Commands
Use the . command to repeat the last indentation operation:
V>>. # Select and indent, then repeat on next lines
5>>. # Indent 5 lines, then repeat operation
Visual Mode Efficiency
Combine visual mode with movement commands for faster selection:
Vj>> # Select next line and indent
Vk<< # Select previous line and unindent
VG>> # Select to end of file and indent
Multiple Selection Techniques
For complex indentation patterns:
:'a,'b>> # Indent from mark 'a' to mark 'b'
:'<,'>> # Indent from visual selection start to end
These methods provide a comprehensive toolkit for efficient indentation in vi, allowing you to quickly format code and text blocks with minimal keystrokes. The choice of method depends on your specific use case, whether you’re working with small sections of code or large files requiring consistent formatting.