Update vim.md with various commands used often

This commit is contained in:
Donovan Glover 2017-12-14 23:54:04 -05:00
parent 7a28e9ce15
commit da3c9c5cdb
No known key found for this signature in database
GPG Key ID: 8FC5F7D90A5D8F4D

View File

@ -7,27 +7,174 @@ For some vim commands, I use a `<leader>` keybinding as an alternative since I f
## Starting vim
- Open a file at a particular line number: `vim +<number> <file>`
- Open multiple files in separate tabs: `vim -p <files>`
- Open all files that contain the function / variable / string `str` in the current directory: `vim -p "grep -l <str> [files]"`
- Note that `[files]` is optional, allowing you to only search for e.g. `*.js` files
- Also note that you should use "\`" instead of quotation marks
- Compare two files side by side for differences between the two: `vim -d <file1> <file2>`
## Movement Keys
### Inserting Things
- Go to the beginning of the file: `gg`
- Go to the end of the file: `G`
- Go to the beginning of the line: `0`
- Go to the first non-whitespace character: `^`
- Go to the end of the line: `$`
- Go to the next word: `w`
- Go to the next big word: `W`
- Go to the previous word: `b`
- Go to the previous big word: `B`
- Go to the next end of a word: `e`
- Go to the next end of a big word: `E`
- Go to the previous sentence: `(`
- Go to the next sentence: `)`
- Go to the previous paragraph: `{`
- Go to the next paragraph: `}`
- Insert before the cursor: `i`
- Insert at the beginning of the line: `I`
- Insert after the cursor: `a`
- Insert at the end of the line: `A`
Note that `0` is sometimes called the **hard beginning of line** and `^` is sometimes called the **soft beginning of line**.
Note that you can also go to a specific line number with `5gg` or `5G`, but you should really be using other keys like `/` instead.
- Go to the previous line: `k`
- Go to the next line: `j`
- Go to the previous letter: `h`
- Go to the next letter: `l`
Note that a letter in vim refers to the characters in the file.
These keys may also be used similar to movement keys, although you should really be using the other keys instead.
Note that you can chain numbers with the movement keys, such as `5j` to move down 5 lines, although you should really be using the other keys instead.
Note that you should avoid mapping `j` to `gj` (same for `k` to `gk`). It encourages bad practice and defeats the purpose of using vim.
## Chained Commands
- Copying things: `y + <MOTION>`
- Copy the current line: `yy`
- Copy the next 5 lines: `y5y`
- Copy the current word: `yw`
- Copy to the end of the line: `y$`
- Copy inside of the tag: `yit`
- Copy all of the tag: `yat`
- Deleting things: `d + <MOTION>`
- Delete the current line: `dd`
- Delete the current word: `dw`
- Delete the next 5 words: `d5w`
- Delete up to the period: `dt.`
- Delete up to and including the period: `df.`
- Delete inside of the square brackets: `di[`
- Delete all of the curly braces: `da{`
- Changing things: `c + <MOTION>`
- Change the current line: `cc`
- Change the current word: `cw`
- Change the next 5 words: `c5w`
- Change the next 5 letters: `c5l`
- Change inside the quotation marks: `ci"`
- Change all of the parentheses: `ca(`
- Change up until the question mark: `ct?`
- Change up to and including the question mark: `cf?`
`c` is functionally similar to `d`, but also leaves you in insert mode to change things.
Note that `ci<block>` and `ca<block>` work for pretty much anything, including `{`, `[`, and `t` (for `<tags>`).
## Basic Commands
- Undo the last action: `u`
- Redo the last action: `<C-r>`
- Paste below the current line: `p`
- Paste above the current line: `P`
- Delete the current character: `x`
- Backspace from the cursor position: `X`
Note: If you only copied part of a line, then `p` and `P` will paste after and before the cursor respectfully.
Note that if you want to paste from the system clipboard (i.e. not vim), then you must use `<C-S-v>` in insert mode.
## Inserting Things
- Insert before the cursor: `i`
- Insert at the beginning of the line: `I`
- Insert after the cursor: `a`
- Insert at the end of the line: `A`
- Insert below the current line: `o`
- Insert above the current line: `O`
- Replace the character under the cursor: `r`
- Enter replace mode, replacing the characters you type over: `R`
To help remembering `a`, just know that it *appends* things (after the cursor and at the end of the line)
## Working with Files
Note that there are a lot of buffer commands, however, I only document the ones I actively use here.
There are also a lot of window commands; however, I don't need to use most of them.
Also note that vim tabs is a thing, although you really shouldn't be using them.
- List all the buffers: `:ls`
- Go to the next buffer: `:bn`
- Go to the previous buffer: `:bp`
- Delete the current buffer: `:bd`
- Open a new buffer with file F: `:e <F>`
TODO: Replace these commands with the ones from your plugins
- Open a horizontal split: `:sp [F]`
- Open a vertical split: `:vs [F]`
- Open a new tab: `:tabnew [F]`
- Switch to the next tab: `gt`
- Switch to the previous tab: `gT`
- Close all windows except the current one: `:only`
## Visual Mode
Note that you will never need to use visual mode 99.9% of the time. Use the other keys instead.
- Enter visual mode: `v`
- Enter linewise visual mode: `V`
- Switch sides (in order to mark in the other direction): `o`
Visual mode can be combined with commands such as `y`, `>`, and `d` (non-exaustive).
## Other Stuff
Note that commands I do not find useful are not mentioned here. Consult the help files if you really want to know all the commands in vim.
- Move the current line to the middle of the screen: `zz`
- Move the current line to the top of the screen: `zt`
- Move the screen down half a page: `<C-d>`
- Move the screen up half a page: `<C-u>`
- Convert to lowercase: `gu + <MOTION>`
- Convert to uppercase: `gU + <MOTION>`
## Indenting Things
- Indent: `> + <MOTION>`
- Outdent: `< + <MOTION>`
- Indent the current line: `>>`
- Indent the next 10 lines: `10>>`
- Unindent the current line: `<<`
- Unindent the next 10 lines: `10<<`
- Outdent the current line: `<<`
- Outdent the next 10 lines: `10<<`
- Re-indent the entire file: `gg=G`
- `gg`: Go to the top of the file
- `=`: Start indenting
- `G`: Until the end of the file
Note that all other vim movements work while indenting things as well.
## Commands
### Manipulating Files
- Save the current buffer: `:w`
- Chain `q` to save the current buffer then quit it
- Chain `!` to force write the currenet buffer
- Quit the current buffer without trying to save: `:q`
- Chain `!` to force close the current buffer without saving it
### Syntax Highlighting
- Change the syntax highlighting of the current file: `:set syntax=<syntax>`
@ -35,8 +182,6 @@ Note that all other vim movements work while indenting things as well.
- Note that to get the value of any variable, just add `?` to it.
- It is possible to manually change the syntax of a file, although you shouldn't do this since plugins and other features you may have will not be loaded.
## Common operations
### Commenting lines
- Comment the current line:
@ -68,3 +213,19 @@ Note that all other vim movements work while indenting things as well.
- Open the result in a new horizontal split: `<C-x>`
- Open the result in a new vertical split: `<C-v>`
- Use a command in fullscreen: `:Command!`
### vimtex
- Change inside the command: `cic`
- Change inside the environment: `cie`
- Change inside the math block: `ci$`
- Change inside the section: `ciP`
- Go to the matching pair: `%`
- Go to the next section: `]]`
- Go to the previous section: `[[`
## Important Things to Note
- You should only be in insert mode when you are inserting text. If you are not inserting text, then you should not be in insert mode.
- To cancel most things and switch back to normal (keybindings) mode, simply press `esc`.
- Line numbers should NOT be important in your daily workflow and should be disabled most of the time. This lets you focus on what you need to look at: the program itself and nothing more.