[Vim is a language](https://github.com/mhinz/vim-galore). Use it often enough and you'll be [fluent](https://medium.com/@mkozlows/why-atom-cant-replace-vim-433852f4b4d1) in no time.
For some vim commands, I use a `<leader>` keybinding as an alternative since I find it faster. Some of those commands may not be mentioned here. Please see `.vimrc` for more details.
Note that the commands below assume that you're writing them in a `.vimrc`. If you want to add new mappings directly in vim, remember to execute the commands in command mode with `:`.
- Map in normal, visual, and operator-pending mode: `map`
- Map in normal mode only: `nmap`
- Map in visual mode only: `xmap`
- Map in command mode only: `cmap`
- Map in operator-pending mode only: `omap`
- Map in insert mode only: `imap`
Note that you should be using `noremap` instead of `map` most (if not all) of the time. This prevents mappings from using mappings made by other commands.
Note that text deleted is also copied into vim's hidden buffer. There is no need for a "cut" command since copying things is handled automatically by vim.
Repeat the last operation with `.`. This is really powerful when you want to perform the same action multiple times.
If you want to repeat the last operation on multiple lines, simply press `<C-v>` to enter visual block mode, select the lines you want to operate on, then execute `.` on them.
An example of combining the insert keys with movement keys is `ea`. This combination of keys allows you to append text to the end of a word, since `e` goes to the end of the word and `a` enters insert mode after the cursor.
Note that you can use `<C-o>` at any time while in insert mode to execute a single normal mode command. This is useful if, for example, you want to navigate to the end of the line after making changes to another part of it.
- Close all tabs except the current one: `:tabonly`
## Search and Replace
- Replace foo with bar everywhere: `:%s/foo/bar/g`
- Replace foo with bar on the current line only: `:s/foo/bar/g`
- Replace foo with bar everywhere, but ask for confirmation on each change: `:%s/foo/bar/gc`
- Change the current item and move on to the next one: `y`
- Do not change the current item and move on to the next one: `n`
- Change all the results: `a`
- Quit making changes: `q`
By default searches are assumed to be case insensitive if the entire string is lowercase. Note that if case matters and the entire search string is lowercase, you should chain `I` to the command to make it case sensitive.
- Jump between the opening and closing parentheses, brackets, curly braces, and tags: `%`
- Delete the character under the cursor and enter insert mode: `s`
- Note that this is an alias of `xi`
- Clear the current line and enter insert mode: `S`
- Note that this is an alias of `cc`
- Join the current line with the next one: `J`
## Macros
Macros are used for complex commands that can't be repeated with `.`. They help reduce repetition and repeat exactly what you did when you create them.
- Start recording a macro: `q + <SYMBOL>`
- To end the macro, press `q` again.
- If you want to end the macro in insert mode, press `<C-o>` then `q`.
- Play back a previously recorded macro: `@ + <SYMBOL>`
- To repeat the last macro played, simply use `@@`.
- Change the syntax highlighting of the current file: `:set syntax=<syntax>`
- Get the syntax of the current file: `:set syntax?`
- 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.
- Simply press `I` to enter insert mode at the beginning of the line. From there you can do whatever you want, including inserting a comment.
- If you want to repeat the process for multiple lines, you can go to the line you want to change and press `.` to execute the last set of key combinations.
- Comment multiple lines at the same time:
- Use `<C-v>` to enter visual block mode
- Select the lines you want to comment out with `j/k`
- Press `I` to go to the beginning of the line
- Type whatever you want to put at the beginning of all the lines here, including comments
- When you're done, exit the mode as usual
### Uncommenting lines
- Uncomment the current line:
- Simply press `0` to go to the beginning of the line (if needed)
- Then, press `x` to delete the character under the cursor
- Uncomment multiple lines at the same time:
- Use `<C-v>` to enter visual block mode
- Select the lines you want to comment out with `j/k`
- Press `x` to delete the first character, repeatedly if necessary
These commands may get used from time to time, but aren't used enough to warrant keybindings.
- Switch between soft and hard tabs: `:set expandtab!`
- Note that you should never have to use this since the repository should have an `.editorconfig`
- Toggle wrap: `:set wrap!`
- Most of the time you don't want to hide text in a text editor, especially vim
- Resize windows placed on top of each other (as opposed to side by side): `:<N>winc <+/-/=>`
-`N` is the number of units to increment / decrement the window size by.
- Do not use `N` when using `=`.
- Most of the time you shouldn't have to deal with these kinds of window sizes. Instead, prefer vertical splits and `<C-w> + >` / `<C-w> + <` / `<C-w> + =`
- 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.
- Registers keep track of all the things that vim has copied, although I haven't found myself using it often enough to document it here.
- The leader key (space) is used for commands not specific to any file type.
- The local leader (backslash) is used for commands that only affect a certain file type.
- Note that vim has folding capabilities as well although I personally haven't made much use of them.
## Miscellaneous
- If for some reason you need to edit files that have line feeds in them, you can use `:%s/^M/\r/g` to remove the line feeds. At this point, you should easily be able to tell what this command does and reproduce it if necessary.