# Vim
## Resources
### [vimcolorschemes.com](https://vimcolorschemes.com/)
### [Fix Keyboard Input on Terminals - Please](http://www.leonerd.org.uk/hacks/fixterms/)
### [Learn Vimscript the Hard Way](https://learnvimscriptthehardway.stevelosh.com/)
- Actually really well done and not hard at all, despite the title
## Usage Tips
### [Write file and delete buffer in one command](https://superuser.com/a/542085)
```vim
:w|bd
```
### Vim sessions
Save current tabs, buffers, splits as session. If `[name]` is not supplied, defaults to `Session.vim` in the current directory (usually good enough)
```vim
:mks[ession] [name]
```
Overwrite session
```vim
:mks[ession]! [name]
```
Restore session when opening vim
```bash
vim -S <path_to_session>
```
Restore session from within vim
```vim
:source <path_to_session>
```
Good, basic intro to vim sessions: [Link](https://bocoup.com/blog/sessions-the-vim-feature-you-probably-arent-using)
### [Interactively delete the current file](https://unix.stackexchange.com/a/495104/488417)
```vim
:E
```
- Delete with `Shift-D`
- Confirm with `y` then `return`
### Working with `()`, `{}`, `[]`, `''`, `""` delimiters
- Stack Overflow: https://stackoverflow.com/a/1062001/2513061
- Text object selection in the manual (good reading): http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select
- Or type `:help text-objects` within Vim
### `%` to jump to other bracket / parentheses
- Doesn't work with quotes
### `<C-^>` to go back to previous file
e.g. after pressing `<C-]>` to see a definition
### Open current file in new tab: `:tabe %`
### [Use Neovim as `$MANPAGER`](https://stackoverflow.com/a/16741577)
```bash
export MANPAGER='nvim -c "%! col -b" -c "set ft=man nomod | let &titlestring=$MAN_PN"'
```
Full details in blog post: [Vim as $MANPAGER](https://muru.dev/2015/08/28/vim-for-man.html)
### Open `man` page in [[Vim]]:
1. Open Vim
2. `:Man <page>`
### Open `--help` in Vim
```bash
rg --help | vim
```
### Close the current tab page
`:tabc` (`:tabclose`)
https://stackoverflow.com/questions/19066965/how-do-i-close-all-windows-only-in-the-current-tab-of-vim
### Run vim command from shell without opening vim
e.g. for vim command `:PlugInstall`:
```
vim +'PlugInstall --sync' +qall
```
- `+PlugInstall` runs `:PlugInstall` inside vim
- `--sync` is [required](https://github.com/junegunn/vim-plug/issues/675#issuecomment-328157169) for [[vim-plug]] specifically because the installer on Vim 8 is asynchronous
- `+qall` quits vim and is equivalent to `:qa`
### [Find and replace](https://linuxize.com/post/vim-find-replace/)
Find and replace all occurrences of `foo` in current file with `bar`
```vim
:%s/foo/bar/g
```
Instead of the slash character `/`, you can use any other non-alphanumeric single-byte character except as a delimiter. This option is useful when you have the `/` character in the search pattern or the replacement string.
```vim
:%s|foo|bar|g
```
To confirm each substitution, use the `c` flag:
```vim
:%s/foo/bar/gc
```
You can also use [regular expressions](https://linuxize.com/post/regular-expressions-in-grep/) as a search pattern. The command bellow replaces all lines starting with `foo` with `beta cuck`:
```vim
:%s/^foo.*/beta cuck/gc
```
## Config Tips
### How to map Alt key
https://vi.stackexchange.com/questions/2350/how-to-map-alt-key
Basically: just use the symbol e.g. åß∂ƒ©˙∆˚¬…:
### How to map to nothing
https://vi.stackexchange.com/questions/30844/map-to-a-key-to-do-nothing
`inoremap <c-w> <nop>`
### Suppress output of `autocmd`
- This prevents the "press enter to continue" screen
- https://stackoverflow.com/questions/11073714/suppress-output-from-a-vim-autocmd
### List of unused keys
https://vim.fandom.com/wiki/Unused_keys
## Debugging Tips
### [Show all warnings, errors, and informational messages that appeared during startup](https://stackoverflow.com/a/17959126)
e.g. If you get an error like this that you can't read:
![[Screen Shot 2022-06-27 at 1.31.40 PM.png]]
Run this in the vim prompt
```vim
:messages
```
### Interpreting error messages
e.g.
```
Error detected while processing function 297[30]..<SNR>38_callback:
line 23:
Vim(tabnext):E788: Not allowed to edit another buffer now
Press ENTER or type command to continue
```
- `<SNR>38` denotes that the error occurred in the 38th script in `:scriptnames`
- `E788` is the error code, not a line number or position
- `23` could mean the `23rd` line of the function, not the whole file
### Identifying a function
e.g.
```
Error detected while processing function 285[30]..<SNR>18_callback[25]..function 285[30]..<SNR>18_callback:
line 23:
Vim(let):E684: list index out of range: 1
```
In this case we want to know what function `285` is. Run the following in the console:
```vim
function g:285
```
Should print the function body:
```vim
function 285(id, code, ...) dict
1 if s:getpos() == self.ppos " {'window': 'enew'}
...
35 endif
endfunction
```
For more info see `:help numbered-function`
### (Guide) [Debugging Vim by Example](https://codeinthehole.com/tips/debugging-vim-by-example/)
- Print statements
- Debug mode
- Add breakpoints
- Verbose mode