-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
title: Missing Semester Lecture 3 - Editors(Vim) | ||
date: 2024-08-28 00:02:27 | ||
category: Missing Semester | ||
tags: | ||
- Missing Semester | ||
- Bash | ||
- Linux | ||
--- | ||
|
||
MIT The Missing semester Lecture of Your CS Education Lecture 3 - Editors(Vim) | ||
|
||
<!-- more --> | ||
|
||
## Philosophy of Vim | ||
|
||
When programming, you spend most of your time reading/editing, not writing. For this reason, Vim is a _modal_ editor: it has different modes for inserting text vs manipulating text. Vim is programmable (with Vimscript and also other languages like Python), and Vim’s interface itself is a programming language: keystrokes (with mnemonic names) are commands, and these commands are composable. Vim avoids the use of the mouse, because it’s too slow; Vim even avoids using the arrow keys because it requires too much movement. | ||
|
||
The end result is an editor that can match the speed at which you think. | ||
|
||
## Basics | ||
|
||
### Command-line | ||
|
||
Command mode can be entered by typing `:` in Normal mode. Your cursor will jump to the command line at the bottom of the screen upon pressing `:`. This mode has many functionalities, including opening, saving, and closing files, and quitting Vim. | ||
|
||
- `:q` quit (close window) | ||
- `:w` save (“write”) | ||
- `:wq` save and quit | ||
- `:e {name of file}` open file for editing | ||
- `:ls` show open buffers | ||
- `:help {topic}` open help | ||
- `:help :w` opens help for the `:w` command | ||
- `:help w` opens help for the `w` movement | ||
|
||
## Vim’s interface is a programming language | ||
|
||
The most important idea in Vim is that Vim’s interface itself is a programming language. Keystrokes (with mnemonic names) are commands, and these commands _compose_. This enables efficient movement and edits, especially once the commands become muscle memory. | ||
|
||
### Movement | ||
|
||
You should spend most of your time in Normal mode, using movement commands to navigate the buffer. Movements in Vim are also called “nouns”, because they refer to chunks of text. | ||
|
||
- Basic movement: `hjkl` (left, down, up, right) | ||
- Words: `w` (next word), `b` (beginning of word), `e` (end of word) | ||
- Lines: `0` (beginning of line), `^` (first non-blank character), `$` (end of line) | ||
- Screen: `H` (top of screen), `M` (middle of screen), `L` (bottom of screen) | ||
- Scroll: `Ctrl-u` (up), `Ctrl-d` (down) | ||
- File: `gg` (beginning of file), `G` (end of file) | ||
- Line numbers: `:{number}<CR>` or `{number}G` (line {number}) | ||
- Misc: `%` (corresponding item) | ||
- Find: `f{character}`, `t{character}`, `F{character}`, `T{character}` | ||
- find/to forward/backward {character} on the current line | ||
- `,` / `;` for navigating matches | ||
- Search: `/{regex}`, `n` / `N` for navigating matches | ||
|
||
### Selection | ||
|
||
Visual modes: | ||
|
||
- Visual: `v` | ||
- Visual Line: `V` | ||
- Visual Block: `Ctrl-v` | ||
|
||
Can use movement keys to make selection. | ||
|
||
### Edits | ||
|
||
Everything that you used to do with the mouse, you now do with the keyboard using editing commands that compose with movement commands. Here’s where Vim’s interface starts to look like a programming language. Vim’s editing commands are also called “verbs”, because verbs act on nouns. | ||
|
||
- `i` enter Insert mode | ||
- but for manipulating/deleting text, want to use something more than backspace | ||
- `o` / `O` insert line below / above | ||
- `d{motion}` delete {motion} | ||
- e.g. `dw` is delete word, `d$` is delete to end of line, `d0` is delete to beginning of line | ||
- `c{motion}` change {motion} | ||
- e.g. `cw` is change word | ||
- like `d{motion}` followed by `i` | ||
- `x` delete character (equal do `dl`) | ||
- `s` substitute character (equal to `cl`) | ||
- Visual mode + manipulation | ||
- select text, `d` to delete it or `c` to change it | ||
- `u` to undo, `<C-r>` to redo | ||
- `y` to copy / “yank” (some other commands like `d` also copy) | ||
- `p` to paste | ||
- Lots more to learn: e.g. `~` flips the case of a character | ||
|
||
### Counts | ||
|
||
You can combine nouns and verbs with a count, which will perform a given action a number of times. | ||
|
||
- `3w` move 3 words forward | ||
- `5j` move 5 lines down | ||
- `7dw` delete 7 words | ||
|
||
### Modifiers | ||
|
||
You can use modifiers to change the meaning of a noun. Some modifiers are `i`, which means “inner” or “inside”, and `a`, which means “around”. | ||
|
||
- `ci(` change the contents inside the current pair of parentheses | ||
- `ci[` change the contents inside the current pair of square brackets | ||
- `da'` delete a single-quoted string, including the surrounding single quotes |