24 KiB
Vim (Vi IMproved) is an enhanced version of the original Vi (Visual) editor. While Vim and Vi share many similarities, Vim offers a wide range of additional features and improvements that make it more powerful and user-friendly. Most of the basic concepts and commands that I mentioned earlier are applicable to both Vim and Vi. However, Vim extends and builds upon Vi's functionality.
Here are some key differences between Vim and Vi:
-
Extended Command Set: Vim introduces many new commands and features that are not available in Vi. These include visual selection, text objects, extended regular expressions, spell checking, folding, and more. While the basic commands remain the same, Vim provides a richer set of tools for text manipulation.
-
Customization and Configuration: Vim offers much more extensive customization options compared to Vi. With Vim, you can configure key mappings, define custom commands, set options, and use a vimrc file to personalize your editing environment. Vim's configuration language, Vimscript, is more powerful and expressive than Vi's limited options.
-
Multiple Levels of Undo and Redo: Vim provides an unlimited undo and redo tree, allowing you to go back and forth through your editing history. Vi, on the other hand, has a more limited undo/redo functionality.
-
Syntax Highlighting and Filetype Detection: Vim has built-in syntax highlighting support for a wide range of programming languages and file formats. It can automatically detect the filetype and apply appropriate syntax highlighting. Vi has limited or no syntax highlighting capabilities.
-
Plugin Ecosystem: Vim has a large and active community that develops plugins to extend its functionality. There are plugins available for almost any task, from code completion to version control integration. Vi has a more limited plugin ecosystem.
-
Graphical User Interface (GUI): Vim has a graphical user interface version called gVim (or MacVim on macOS), which provides additional features like toolbar, menu bar, and mouse support. Vi is primarily a command-line based editor.
-
Portability: Vim is highly portable and is available on a wide range of operating systems, including Unix, Linux, macOS, and Windows. Vi, being an older editor, may have more limited portability.
-
Documentation and Help System: Vim has an extensive built-in documentation and help system. You can access detailed information on commands, options, and features using the
:helpcommand. Vi's documentation is more limited in comparison.
Despite these differences, the core editing model and basic commands remain largely compatible between Vim and Vi. If you learn Vim, you can use most of that knowledge in Vi as well. However, Vim's additional features and improvements make it a more powerful and flexible editor.
It's worth noting that on many modern systems, the vi command is often symlinked to vim, so when you run vi, you're actually running Vim in compatible mode. This mode disables some of Vim's advanced features to maintain compatibility with Vi.
In summary, while Vi and Vim share a common foundation, Vim offers a significantly expanded feature set, better customization options, and a more extensive ecosystem. It builds upon Vi's core concepts and provides a more powerful and user-friendly editing experience.
Certainly! Let me provide you with a more comprehensive overview of Vim's structure and key concepts to help you understand and use the tool more effectively.
-
Modal Editing: Vim is a modal editor, which means it has different modes for different purposes: a. Normal Mode: Used for navigation and manipulation of text. This is the default mode. b. Insert Mode: Used for inserting and modifying text. c. Visual Mode: Used for selecting text and performing operations on the selection. d. Command-line Mode: Used for entering commands and searching.
-
Text Objects: Text objects are a way to define a range of text in Vim. They allow you to perform operations on specific parts of the text, such as words, sentences, paragraphs, or even custom-defined ranges. Text objects are used in combination with operators (like delete, change, or yank) to manipulate the text.
-
Operators: Operators are commands that perform actions on text, such as deleting, changing, or copying. They are often used in combination with motions or text objects to define the range of text to be operated on. Some common operators include
d(delete),c(change), andy(yank). -
Motions: Motions are commands that move the cursor around the text. They can be used to navigate through the document or to define a range of text when combined with an operator. Some examples of motions include
w(word),b(back),j(down),k(up), and$(end of line). -
Counts: Counts are numbers that can be prefixed to a command to repeat it multiple times. For example,
3ddwill delete three lines, and5wwill move the cursor forward five words. -
Registers: Registers are like named clipboards in Vim. They store text that has been yanked (copied) or deleted. You can access registers using the
"character followed by the register name. For example,"ayyyanks the current line into register "a", and"appastes the contents of register "a". -
Macros: Macros allow you to record a series of keystrokes and replay them later. This is useful for automating repetitive tasks. To start recording a macro, press
qfollowed by a register name. To stop recording, pressqagain. To execute the macro, press@followed by the register name. -
Configurations: Vim is highly configurable and can be customized to suit your needs. The configuration file is called
.vimrc(or_vimrcon Windows) and is located in your home directory. You can use this file to set options, define mappings, and customize Vim's behavior. -
Plugins: Vim has a rich plugin ecosystem that extends its functionality. Plugins can add new features, improve existing ones, or provide integration with other tools. Some popular plugin managers include Vundle, Pathogen, and Vim-Plug.
-
Vimscript: Vimscript is Vim's built-in scripting language. It allows you to write scripts to automate tasks, define custom commands, and create plugins. Vimscript has its own syntax and can be used to interact with Vim's internals.
Understanding these concepts will give you a solid foundation for using Vim effectively. Vim has a steep learning curve, but once you master its concepts and commands, you'll be able to edit text with great speed and efficiency.
Remember to practice regularly and refer to Vim's comprehensive documentation (:help) whenever you need more information on a specific topic. The Vim community is also a great resource, with many tutorials, cheat sheets, and forums available online.
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.