Vim motions continued
This video, “Lesser Known Vim Tricks That Will Boost Your Workflow,” provides a comprehensive guide to various Vim motions and tricks. Here’s a summary of the mentioned tricks and tips:
Exiting Vim Quickly
Selecting and Changing Content Inside Delimiters
vi(: Selects content inside parentheses [01:09].vi{: Selects content inside curly braces [01:15].vib: Selects content inside parentheses (more accessible on some keyboards) [01:28].viB: Selects content inside curly braces [01:37].cib: Changes (deletes and enters insert mode) content inside parentheses [01:49].
Making Changes to Multiple Lines (Visual Block Mode)
Ctrl + v: Enters visual block mode [02:22].I(capital I): Enters insert mode at the beginning of the selected block to add text [02:30].GV: Re-selects the last highlighted text [02:53].$: Moves the cursor to the end of the line [02:59].A(capital A): Appends text to the end of the selected lines [03:00].
Toggling Case
g~: Toggles the case of the character under the cursor [03:24].g~w: Toggles the case of the entire word [03:40].g~it: Toggles the case of content inside an HTML tag [03:46].
Re-indenting and Jumping Between Matching Pairs
gg=G: Re-indents the entire file [04:07].%: Jumps between matching pairs of parentheses, brackets, or curly braces [04:18].
Managing Vim Sessions
Ctrl + z: Suspends Vim and puts it in the background [04:51].fg: Brings Vim back to the foreground [05:01].:mk session <filename.vim>: Creates a Vim session file [05:12].:source <filename.vim>: Loads a previously saved Vim session [05:34].
Opening URLs and Files
gx: Opens the URL under the cursor in a browser [06:21].gf: Opens the file path under the cursor [06:42].
Vim Marks for Navigation
m<lowercase_letter>: Sets a mark within the current file (e.g.,ma) [07:13].`<lowercase_letter>: Jumps to a mark within the current file (e.g.,`a) [07:27].m<uppercase_letter>: Sets a mark that can be jumped to from different files (e.g.,mA) [07:37].`<uppercase_letter>: Jumps to a mark in a different file (e.g.,`A) [07:49].
Jumping to Specific Lines
:<line_number>: Jumps to a specific line number from command mode (e.g.,:40) [08:01].<line_number>G: Jumps to a specific line number (e.g.,12G) [08:11].
Joining Lines
J: Joins the current line with the line below it, inserting a space [08:56].gJ: Joins the current line with the line below it without inserting a space [09:08].
You can watch the video at https://www.youtube.com/watch?v=RdyfT2dbt78
This video, “Vim Motions & Operators Tutorial,” provides a detailed tutorial on various Vim motions and operators. Here’s a breakdown of the key concepts covered:
The video starts by introducing Vim motions as a way to speed up coding, applicable in various modern code editors like PyCharm and VS Code, not just Vim [00:29]. It also mentions that tools like Vime allow browsing the web with these same keys [00:36].
Core Vim Concepts:
-
Modes: Vim operates in different modes. The default is Normal Mode, where you can navigate text but not change it [01:07].
-
Navigation (Normal Mode):
- Instead of arrow keys, Vim uses H, J, K, L for left, down, up, and right respectively [01:32].
- W moves forward word by word [04:51].
- B moves backward word by word [04:57].
- E moves to the end of a word [05:04].
- $ moves to the end of a line [05:28].
- 0 moves to the very beginning of a line (column zero) [05:35].
- ^ moves to the first non-blank character of a line [05:47].
- gg goes to the top of the document [09:40].
- Shift + G goes to the very last line of the document [09:46].
- Shift + M moves to the middle of the visible screen [09:53].
- ZZ scrolls the document to center the current line [10:11].
- f followed by a character finds the first occurrence of that character on the line [07:16].
- ; repeats the last find command forward [07:37].
- , repeats the last find command backward [07:52].
- Shift + F searches backward from the cursor [07:59].
- Numbers can be combined with motions (e.g.,
4ftto find the fourth ’t’,5Wto jump five words) [08:06]. - % jumps to the matching counterpart of a bracket, brace, or other pair [14:22].
- / starts a search forward for a word or pattern [18:18].
- n scrolls to the next search result [18:38].
- Shift + N scrolls to the previous search result [18:38].
- Shift + / (question mark) searches backward [18:50].
-
Editing Modes:
- I enters Insert Mode before the current character [02:25].
- A enters Insert Mode after the current character [03:39].
- Shift + A starts writing after the line (appends to the end of the line) [04:04].
- Shift + I starts writing before the current line [04:32].
- Escape (or mapped keys like
JJorJK) exits Insert Mode and returns to Normal Mode [02:37]. - Shift + R enters Replace Mode, allowing you to type over existing text [13:42].
- o is a quick operator to start a new line in insert mode [17:32].
-
Editing Actions (Normal Mode):
- D is the deletion operator [11:04].
- dw deletes a word [11:04].
- dd deletes an entire line [12:14].
- u is for undo [11:12].
- Ctrl + R is for redo [14:01].
- P pastes after the cursor (or after the current line if a line was deleted/yanked) [11:24].
- Shift + P pastes before the cursor (or before the current line) [12:48].
- Y is the yank (copy) operator [11:55].
- yw yanks a word [12:02].
- yy yanks an entire line [12:21].
- = = fixes indentation for the current line [08:55].
- . (dot operator) repeats the last action [17:08].
-
Visual Mode:
-
Text Objects:
- These allow operations on specific text structures.
- diw (delete in word) deletes the entire word regardless of cursor position [15:07].
- daw (delete around word) deletes the word and surrounding space [15:56].
- This concept extends to other text objects like paragraphs (
ip,ap), functions, and content within quotes or brackets (e.g.,ci(to change inside parentheses,di"to delete inside quotes) [15:14].
-
Command Mode:
- : (colon) enters Command Mode [19:17].
- : followed by a command (e.g.,
qto quit,wto save,wqto save and quit) [21:32]. - : followed by
q!forces quitting without saving [21:51]. - Shift + ZZ is a shortcut to save and quit [21:58].
- Search and replace can be done using regular expressions in command mode (e.g.,
:%s/target/replace/gfor global replacement across the document) [19:41].
The video also briefly mentions helpful tools like “Vim Adventures” for practicing navigation [02:14] and “Witch Key” for NeoVim users to suggest next keys in motions [10:32].
You can watch the video at https://www.youtube.com/watch?v=z4eA2eC28qg