17 KiB
Certainly! Here's a comprehensive guide to using Vim effectively, focusing on the concept of text objects and other essential features. This guide assumes basic familiarity with Vim's modes and navigation.
-
Text Objects: a. Word:
aw: a word (includes surrounding whitespace)iw: inner word (excludes surrounding whitespace)- Example:
dawdeletes a word and its surrounding whitespace b. Sentence: as: a sentenceis: inner sentence- Example:
cischanges the current sentence c. Paragraph: ap: a paragraphip: inner paragraph- Example:
yapyanks (copies) the current paragraph d. Tag (HTML/XML): at: a tag (includes the tag itself)it: inner tag (excludes the tag)- Example:
datdeletes the entire tag and its contents e. Quotes: a",a',a`: a quoted string (includes the quotes)i",i',i`: inner quoted string (excludes the quotes)- Example:
ci"changes the contents inside double quotes f. Brackets: a),a],a}: a bracketed block (includes the brackets)i),i],i}: inner bracketed block (excludes the brackets)- Example:
yi(yanks the contents inside parentheses g. Line: al: a line (includes indentation)il: inner line (excludes indentation)- Example:
daldeletes the current line and its indentation
-
Operators: a.
d: delete b.c: change (delete and enter insert mode) c.y: yank (copy) d.p: put (paste) e.=: format (indent) f.>: indent g.<: de-indent h. Example:d2awdeletes the next two words -
Motions: a.
w: move to the start of the next word b.e: move to the end of the current word c.b: move to the start of the previous word d.f/F: find the next/previous occurrence of a character e.t/T: find the character before the next/previous occurrence f.$: move to the end of the line g.0: move to the start of the line h.^: move to the first non-blank character of the line i.%: move to the matching bracket -
Visual Mode: a.
v: character-wise visual mode b.V: line-wise visual mode c.Ctrl-v: block-wise visual mode d. Example:vipvisually selects the current paragraph -
Search and Replace: a.
/pattern: search forward for the pattern b.?pattern: search backward for the pattern c.n/N: find the next/previous occurrence of the search pattern d.:%s/old/new/g: replace all occurrences of "old" with "new" in the entire file e.:.,$s/old/new/g: replace all occurrences of "old" with "new" from the current line to the end of the file -
Macros: a.
qa: start recording a macro named "a" b.q: stop recording the macro c.@a: execute the macro named "a" d.@@: execute the last executed macro -
Undo and Redo: a.
u: undo the last change b.Ctrl-r: redo the last undone change -
Splitting Windows: a.
:split: split the current window horizontally b.:vsplit: split the current window vertically c.Ctrl-w h/j/k/l: move to the window on the left/down/up/right -
Tabs: a.
:tabnew: open a new tab b.:tabn: move to the next tab c.:tabp: move to the previous tab d.gt: move to the next tab e.gT: move to the previous tab -
Other Essential Commands: a.
:w: save the current file b.:q: quit Vim c.:wqor:x: save and quit d.:q!: quit without saving changes e.:help: open Vim's help documentation
By mastering these concepts and commands, you'll be able to navigate, manipulate, and edit text efficiently in Vim. Remember that practice is key to becoming proficient with Vim's powerful features. Don't hesitate to explore Vim's extensive documentation (:help) to learn even more advanced techniques and customization options.
Vim Guide
1. Introduction to Vim's Philosophy
Vim, renowned for its modal editing system, allows users to switch between various modes designed for specific tasks such as inserting text, navigating within files, or executing commands. This modal approach greatly enhances editing efficiency by reducing the number of keystrokes required for tasks and enabling a more intuitive control over text manipulation.
2. Quick Reference Guide
This section provides a quick lookup for the most frequently used Vim commands, categorized for easy reference:
- Navigating
h, j, k, l: Move left, down, up, right (the arrow keys of Vim).gg, G: Jump to the top or bottom of the document.
- Editing
i, a, o: Enter insert mode at cursor, after cursor, or on a new line.d, c, y: Delete, change, or yank (copy) text.
- Searching
/pattern, ?pattern: Search forward or backward for a pattern.n, N: Repeat search in the same or opposite direction.
3. Core Guides with Integrated Efficiency Tips
Basic Editing and File Management
Opening, Saving, and Exiting Files
- Commands:
vim filename,:w,:wq,:q!- Description: Open files with
vim filename. Save with:w, exit with:q!, or combine both with:wq.
- Description: Open files with
- Tip: Use
ZZas a quicker alternative to:wqwhen you need to save and exit efficiently.
Navigating Within Files
- Commands:
h, j, k, l- Description: Precisely navigate within lines or across the text.
- Tip: Pair
h, j, k, lwithCtrl(e.g.,Ctrl-fandCtrl-b) for faster document scrolling.
Efficient Navigation
Word and Line Movements
- Commands:
w, b, e, 0, ^, $- Description: Navigate efficiently by words or to specific positions within a line.
- Tip:
5wcan save significant time by jumping five words forward, reducing repeated keystrokes.
Document Navigation
- Commands:
gg, G- Description: Quickly move to the beginning or end of a document, or directly to a specific line with
50G.
- Description: Quickly move to the beginning or end of a document, or directly to a specific line with
- Tip: Use percentage jumps like
50%to reach the midpoint of a document quickly.
Advanced Text Manipulation
Editing Commands
- Commands:
dw, ciw, d$- Description: Efficient commands for deleting a word, changing inside a word, or deleting to the end of a line.
- Tip:
ci(changes content inside parentheses, a common task in programming.
Using Visual Mode for Complex Selections
- Commands:
v, V, Ctrl-v- Description: Select text for manipulation in various modes: character, line, or block.
- Tip: Use
>or<to indent selected blocks quickly, crucial in code formatting.
Search and Replace
Finding and Replacing Text
- Commands:
:s/old/new/g,/%s/old/new/g- Description: Replace 'old' text with 'new' globally or within the current line.
- Tip: Use
*to search for the word under the cursor quickly.
Navigational Searches
- Commands:
f<char>, F<char>, t<char>, T<char>- Description: Jump to the next or previous occurrence of a character on the line.
- Tip: Use
;and,to repeat the lastfortsearch efficiently.
Enhanced Motion for Programming and Structured Data
Paragraph and Section Motions
- Commands:
{,},[[,]]- Description: Navigate quickly between paragraphs or code blocks. Ideal for structured data and code structures.
Screen Motions
- Commands:
H,M,L,Ctrl-d,Ctrl-u- Description: Manage on-screen text positioning and rapid scrolling effectively.
Document Motions
- Commands:
gg,G- Description: Essential for moving quickly through large files or to specific lines, critical in debugging and reviewing.
Tips for Using Motions in Structured Data and Programming
- Combine motions with searches for efficient navigation within XML, JSON
, or YAML, especially useful in large data files or complex codebases.
- Record macros for repetitive edits across similar structures, optimizing time and ensuring consistency.
- Utilize
Ctrl-vfor block visual mode, particularly effective for column-based editing or configuration adjustments.
This expanded guide not only provides a comprehensive look at using Vim effectively but also integrates practical tips directly applicable to programming and structured data editing, ensuring users can leverage Vim's power to its fullest.
Here's a streamlined outline of the VIM guides, focusing on the most relevant and commonly used topics:
I. Copy, Paste, and Find/Replace Operations
A. Copy and Paste Lines
1. Copy a Single Line
2. Paste the Copied Line
B. Copy and Paste Words
1. Copy a Word
2. Paste the Copied Word
C. Find and Replace
1. Find and Replace in the Entire Document
2. Find and Replace in the Current Line
3. Find and Replace with Confirmation
II. Useful Tasks and How to Accomplish Them
A. Searching and Navigating
B. Indenting and Formatting
C. Folding Code
D. Working with Registers
E. Switching Between Files
III. Visual Mode and Text Selection
A. Understanding Visual Mode
B. Entering and Exiting Visual Mode
C. Selecting Text in Visual Mode
D. Operating on Selected Text
E. Visual Mode Tips and Tricks
IV. Text Objects and Motions
A. Understanding Text Objects
B. Word and WORD Text Objects
C. Sentence and Paragraph Text Objects
D. Code Block Text Objects
E. Combining Text Objects with Motions
F. Using Text Objects in Visual Mode
G. Indentation Text Objects
H. Line and Column Text Objects
I. Repeating Actions with the Dot Operator
V. Undo and Redo
A. Understanding Undo and Redo
B. Basic Undo and Redo Commands
C. Undo and Redo Tips and Tricks
VI. Buffer Management and Navigation
A. Understanding Buffers
B. Listing and Switching Buffers
C. Opening and Closing Buffers
D. Saving and Discarding Changes
E. Buffer Navigation
VII. Marks and Jumps
A. Understanding Marks
B. Setting and Jumping to Marks
C. Special Marks
D. Jumps and the Jumplist
VIII. Mastering Registers
A. Understanding Registers
B. Unnamed Register
C. Named Registers
D. System Clipboard Register
E. Read-Only Registers
IX. Customizing VIM with .vimrc
A. Understanding .vimrc
B. Basic .vimrc Settings
C. Mapping Keys in .vimrc
D. Customizing VIM Plugins in .vimrc
This streamlined outline focuses on the most essential and commonly used features of VIM that are relevant to a majority of users. It covers copy/paste operations, useful tasks, visual mode, text objects and motions, undo/redo, buffer management, marks and jumps, registers, and basic customization with .vimrc.
Less commonly used topics like persistent undo, advanced buffer management, complex text object manipulations, Vim scripting, and more specialized topics have been removed to keep the guide concise and focused on the most practical features for everyday VIM usage.
Sure! Here's a concise reference guide for the Copy, Paste, and Find/Replace Operations, as well as the Useful Tasks in VIM:
VIM Reference Guide
I. Copy, Paste, and Find/Replace Operations
Copy and Paste Lines
- Copy a line:
yy - Paste after current line:
p - Paste before current line:
P
Copy and Paste Words
- Copy a word:
yiw - Paste after cursor:
p - Paste before cursor:
P
Find and Replace
- Find and replace in the entire document:
:%s/old/new/g - Find and replace in the current line:
:s/old/new/g - Find and replace with confirmation:
:%s/old/new/gc
II. Useful Tasks
Searching and Navigating
- Search for a pattern:
/pattern - Jump to the next occurrence:
n - Jump to the previous occurrence:
N - Jump to a specific line number:
:line_number
Indenting and Formatting
- Indent a block of code:
Vto select lines, then>to indent or<to unindent - Autoindent the entire file:
gg=G - Format a paragraph:
gq}
Folding Code
- Create a fold:
zffollowed by a motion command (e.g.,zf5jto fold the next 5 lines) - Open a fold:
zo - Close a fold:
zc - Open all folds:
zR - Close all folds:
zM
Working with Registers
- Yank text into a named register:
"register_nameyfollowed by a motion command - Paste from a named register:
"register_namep - View the contents of all registers:
:reg
Switching Between Files
- Open a new file in a split window:
- Horizontal split:
:sp filename - Vertical split:
:vs filename
- Horizontal split:
- Switch between open files:
Ctrl-wfollowed byh,j,k, orl - Close the current file:
:q
This reference guide provides a quick overview of the essential copy, paste, find/replace operations, and useful tasks in VIM. Keep this guide handy for quick access to the most commonly used commands and techniques.
Sure! Here's a concise reference guide for Visual Mode and Text Selection, Text Objects and Motions, and Undo and Redo in VIM:
VIM Reference Guide
III. Visual Mode and Text Selection
Entering and Exiting Visual Mode
- Enter character-wise visual mode:
v - Enter line-wise visual mode:
V - Enter block-wise visual mode:
Ctrl-v - Exit visual mode:
EscorCtrl-c
Selecting Text
- Character-wise selection: Use motion commands (
h,j,k,l,w,b) - Line-wise selection: Use
jorkto select entire lines - Block-wise selection: Use motion commands to select a rectangular block
- Select all text:
ggVG
Operating on Selected Text
- Copy (yank) selected text:
y - Cut (delete) selected text:
d - Change selected text:
c - Indent selected text:
>to indent,<to unindent - Convert selected text to uppercase:
U - Convert selected text to lowercase:
u
Visual Mode Tips
- Switch between selection ends:
o - Reselect last visual selection:
gv
IV. Text Objects and Motions
Word and WORD Text Objects
w: word (alphanumeric characters and underscores)W: WORD (non-blank characters separated by whitespace)- Examples:
daw: delete a word (including trailing whitespace)ciW: change inner WORD (excluding surrounding whitespace)yiw: yank inner word (excluding trailing whitespace)
Sentence and Paragraph Text Objects
s: sentence (characters ending with.,!, or?, followed by whitespace or end of line)p: paragraph (block of text separated by blank lines)- Examples:
das: delete a sentence (including whitespace after the sentence)cip: change inner paragraph (excluding surrounding blank lines)yap: yank a paragraph (including surrounding blank lines)
Code Block Text Objects
(,),{,},[,],<,>: code blocks delimited by matching parentheses, braces, brackets, or angle brackets- Examples:
di(: delete inside parenthesesca{: change around curly braces (including the braces)yi[: yank inside square brackets
Combining Text Objects with Motions
- Examples:
d2aw: delete two wordsc3s: change three sentencesy4p: yank four paragraphs
V. Undo and Redo
Basic Undo and Redo Commands
- Undo last change:
u - Redo last undone change:
Ctrl-r - Undo multiple changes: Press
umultiple times - Redo multiple changes: Press
Ctrl-rmultiple times
Undo and Redo Tips
- Undo all changes to a line since entering insert mode:
U - Repeat last change:
.(dot)
This reference guide provides a quick overview of the essential commands and techniques for visual mode and text selection, text objects and motions, and undo and redo in VIM. Keep this guide handy for quick access to these powerful editing features.
Certainly! Ex commands in Vim are incredibly useful for a range of text editing tasks. Here are some basic Ex commands that you should start with:
1. Opening and Saving Files
- Open a file:
:e filename– Opensfilenamein the current buffer. - Write (save) the current file:
:w– Saves the changes you've made. - Save and quit:
:wqor:x– Saves changes and closes the editor. - Quit without saving:
:q!– Quits and discards any changes.
2. Line Manipulation
- Go to a specific line:
:#– Where#is the line number you want to go to. For example,:25takes you to line 25. - Move lines:
:#m#– Moves a line to a new position. For example,:2m5moves line 2 below line 5. - Copy lines:
:#t#– Copies a line to a new position. For example,:3t5copies line 3 below line 5.
3. Searching and Replacing
- Search for a text:
:/pattern– Finds the next occurrence ofpattern. - Global search and replace:
:%s/old/new/g– Replaces all occurrences ofoldwithnewthroughout the document. Remove thegif you only want to replace the first occurrence in each line.
4. Filtering and Executing External Commands
- Filter lines through an external command:
:#,#!command– For example,:1,10!sortsorts lines 1 to 10. - Read the output of an external command into the buffer:
:r !command– For example,:r !dateinserts the current date into the document.
5. Managing Multiple Files
- Next file:
:n– Moves to the next file in the list of files opened with Vim. - Previous file:
:prevor:p– Moves to the previous file.
6. Setting Options
- Set options:
:set option– Sets different configurations. For example,:set numberturns on line numbering,:set nonumberturns it off.
7. Undo and Redo
- Undo the last change:
:undoor justuin normal mode. - Redo the last undone change:
:redoorCtrl-rin normal mode.