---
# System prepended metadata

title: Vim Cheatsheet

---

# Vim Cheatsheet

First of all, there are tons of resources on the internet for teaching you how to use vim. Should it be necessary to have tutorials on how to use a text editor? That's a question for another day. This document is *not* a definitive tutorial. You should search elsewhere for that. It's just a description of my personal setup.

I've found using neovim to be slightly better than a vim as a few things seem to just work a bit nicer. You need to do `module load neovim` on the scc to get it, and then `nvim filename` rather than `vim filename`. However, they are pretty similar so it doesn't really matter too much.
## vimrc
* There are a ton of plugins and customization you can do. I didn't want to think too much about this, so I just used https://github.com/amix/vimrc. I installed the "awesome" version:
```
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh
```
* I added a few minor customizations. The contents of `~/.vim_runtime/my_configs.vim` is:
```
" I don't actually understand how to write vim configs,
" so all of these commands are basically pulled verbatim from the internet.
" I'll list the url above the commands.

" Turn on mouse support in all modes.
" I know vim is supposed to let you not use a mouse, but
" I still want to use a mouse sometimes...
" https://vimtricks.com/p/using-a-mouse-in-vim/
set mouse=a

" In vim you can indent with > and < (similar to ctrl-] in vscode).
" First, you must select the lines in visual mode and then indent.
" By default this throws you out of visual mode, which I find annoying.
" The following keeps you in visual mode.
" https://superuser.com/questions/310417/how-to-keep-in-visual-mode-after-identing-by-shift-in-vim
vnoremap < <gv
vnoremap > >gv

" display line numbers on the left hand side of the screen
set number
```

## Common tasks

### vimtutor
* `vimtutor` (from the shell, not inside `vim`) will open a program to teach you a bit of how to use `vim`.

### indent lines
From visual mode, do `>` or `<`.

### comment lines
from visual mode, do `gc`. `gc` again to uncomment.

### Undo
* undo everything in last insert mode operation: `u` (from normal mode)
* undo only changes on current line: `ctrl-u` (from *insert* mode). Also sometimes `U` (capital U) from normal mode will do this, but I find `ctrl-u` is more likely to do what I want.

### Start/end of file
* `gg` for start of file, `G` for end of file.

### Redo
* `ctrl-r` (from normal mode)

### Find
* Hit `/<search test>` and then `<Enter>`. Now, typing `n` or `N` will search forward and backward in the document.

### Return to previous position
* `ctrl-o` will jump the cursor back to previous positions.

### Mark/recall cursor position
* Press `m<Char>` where `<Char>` can be any character (although upper and lower case behave [slightly differently](https://vim.fandom.com/wiki/Using_marks)). This will "save" the current cursor position. Then `'<Char>` to return to that saved position.

### Find and replace
* This surprisingly hard to find: most people seem to suggest some regex monstrosity, which seems really unwieldly in comparison to the standard find-replace flow in GUI editors. Instead, as described [here](https://www.baeldung.com/linux/vim-search-replace), there is a reasonably straightforward way:
1. Search for your text with `/<search text>` and then `<Enter>` from normal mode.
2. Keep pressing `n` until you find something you want to replace.
3. Type `cgn`. This will delete the search string that occurse following your cursor and drop you into insert mode.
4. Type your replacement text and hit `<ESC>` to leave insert mode.
5. Now press `n` or `N` to search forward and backwards until you find another string you want to replace.
6. Hit `.` to replace with your replacement text.
7. Repeat until you've replaced everyting you want.

### copy to clipboard
When you do a "yank" in vim, it only yanks internally, you can't paste the text anywhere else. This is annoying, but it can be fixed (at least for neovim - I don't actually know why this doesn't work in vim for me). To install:
1. first install this [plugin manager](https://github.com/junegunn/vim-plug):
```
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
```
Then, modify `~/.vim_runtime/my_config.vim` by adding the following to the end of the file (which installs [this plugin](https://github.com/ojroques/vim-oscyank)):
```

call plug#begin(has('nvim') ? stdpath('data') . '/plugged' : '~/.vim/plugged')

Plug 'ojroques/vim-oscyank', {'branch': 'main', 'tag': 'v1.0.0'}

call plug#end()

autocmd TextYankPost *
    \ if v:event.operator is 'y' |
    \ execute 'OSCYankReg +' |
    \ endif
```
Then exit and restart `nvim` and run `:PlugInstall`. You may then need to exit and restart `nvim` again.

Now, yanks will yank to the system clipboard to allow pasting externall.

#### Notes on clipboard yank
Supposedly you could yank to the system clipboard to allow global pasting with either `"*y` or `"+y` depending on the system , but this requires vim to have been originally compiled with a particular option, which was not done on the scc.  However, the helix editor can do this out-of-the-box, and looking at the source code here it seems to be using some weird trick: https://github.com/helix-editor/helix/blob/299bcce481abe7e13f2023ec6e82c59b43f12f47/helix-view/src/clipboard.rs#L198 . Googling the relevant term "OSC 52", we find the vim equivalent: https://jdhao.github.io/2021/01/05/nvim_copy_from_remote_via_osc52/. I was able to install it, but only for neovim (even though I used the vim install instructions... ) Unfortunately, I don't know and don't really want to know too much about writing vim plugins, so I don't know how to debug this :)

Note that you could replace the autocmd above with
```
autocmd TextYankPost *
    \ if v:event.operator is 'y' && v:event.regname is '+' |
    \ execute 'OSCYankReg +' |
    \ endif
```
to make it only copy to clipboard with `"+y`, but I suspect I always want to copy to clipboard.