# Vim - Cheatsheet

# Vim Cheatsheet

# Useful Keyboard Shortcuts

## Select the beginning of a function

Shift-V -

## Get version of vim

```
:version
```

<div class="code-toolbar" id="bkmrk-copy"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Get to the end of a function

```
$%
```

<div class="code-toolbar" id="bkmrk-copy-1"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Autoindent the current file

```
gg=G
```

<div class="code-toolbar" id="bkmrk-copy-2"><div class="toolbar"><div class="toolbar-item"></div></div></div>Breaking it down:

**gg** to get to the start of the file, **=** to indent and **G** to get to the end of the file.

## Use JQ to make JSON human readable

```
:%!jq '.'
```

<div class="code-toolbar" id="bkmrk-copy-3"><div class="toolbar"><div class="toolbar-item"></div></div></div>Alternatively, you can also use python:

```
:%!python -m json.tool
```

<div class="code-toolbar" id="bkmrk-copy-4"><div class="toolbar"><div class="toolbar-item"></div></div></div>Resources:  
[JQ approach](https://til.hashrocket.com/posts/ha0ci0pvkj-format-json-in-vim-with-jq)  
[Python approach](https://blog.realnitro.be/2010/12/20/format-json-in-vim-using-pythons-jsontool-module/)

## Paste over line

```
Vp
```

<div class="code-toolbar" id="bkmrk-copy-5"><div class="toolbar"><div class="toolbar-item"></div></div></div>Resource: [https://stackoverflow.com/questions/4533530/vim-replacing-a-line-with-another-one-yanked-before](https://stackoverflow.com/questions/4533530/vim-replacing-a-line-with-another-one-yanked-before)

## Delete all text in a file

If you want to delete all of the text in file you’re in, use the following keyboard shortcut:

```
1G
dG
```

<div class="code-toolbar" id="bkmrk-copy-6"><div class="toolbar"><div class="toolbar-item"></div></div></div>Resources:  
[http://stackoverflow.com/questions/506075/how-do-i-fix-the-indentation-of-an-entire-file-in-vi](http://stackoverflow.com/questions/506075/how-do-i-fix-the-indentation-of-an-entire-file-in-vi)  
[http://stackoverflow.com/questions/8124315/how-i-can-delete-in-vim-all-text-from-current-line-to-end-of-file](http://stackoverflow.com/questions/8124315/how-i-can-delete-in-vim-all-text-from-current-line-to-end-of-file)

## Alternative to esc

```
ctrl-c
```

<div class="code-toolbar" id="bkmrk-copy-7"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Alternative to wq

```
ZZ
```

<div class="code-toolbar" id="bkmrk-copy-8"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Alternative to q

```
ZQ
```

<div class="code-toolbar" id="bkmrk-copy-9"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Indenting

Indent once in normal mode: `>>` Repeat the indent: `.` Indent five times: `5>>` Unindent: `<<` `Ctrl-T` to indent in insert mode `Ctrl-D` to unindent in insert mode  
**Resource**: [http://vim.wikia.com/wiki/Shifting\_blocks\_visually](http://vim.wikia.com/wiki/Shifting_blocks_visually)

## Encrypt file

```
:setlocal cm=blowfish2
```

<div class="code-toolbar" id="bkmrk-copy-10"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**: [https://www.tecmint.com/password-protect-vim-file-in-linux/](https://www.tecmint.com/password-protect-vim-file-in-linux/)

## Decrypt file

Open the file and do the following:

1. `:X`
2. Press the enter key twice

## Find all lines containing pattern and delete them

```
:g/pattern/d
```

<div class="code-toolbar" id="bkmrk-copy-11"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**: [https://vim.fandom.com/wiki/Delete\_all\_lines\_containing\_a\_pattern](https://vim.fandom.com/wiki/Delete_all_lines_containing_a_pattern)

## Go to end of line

```
$
```

<div class="code-toolbar" id="bkmrk-copy-12"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**: [https://stackoverflow.com/questions/105721/how-do-i-move-to-end-of-line-in-vim](https://stackoverflow.com/questions/105721/how-do-i-move-to-end-of-line-in-vim)

## Remove blank lines

```
:g/^$/d
```

<div class="code-toolbar" id="bkmrk-copy-13"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**: [https://stackoverflow.com/questions/706076/vim-delete-blank-lines](https://stackoverflow.com/questions/706076/vim-delete-blank-lines)

## Open file

```
:n <file>
```

<div class="code-toolbar" id="bkmrk-copy-14"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**: [https://stackoverflow.com/questions/23680778/how-do-you-open-a-file-from-within-vim](https://stackoverflow.com/questions/23680778/how-do-you-open-a-file-from-within-vim)

## List files in a directory

```
:e <directory>
```

<div class="code-toolbar" id="bkmrk-copy-15"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Change directory

```
:cd <directory>
```

<div class="code-toolbar" id="bkmrk-copy-16"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**:  
[https://stackoverflow.com/questions/2288756/how-to-set-working-current-directory-in-vim](https://stackoverflow.com/questions/2288756/how-to-set-working-current-directory-in-vim)

## Page down

ctrl-f

## Down half a page

ctrl-d

## Page up

ctrl-b

## Up half a page

ctrl-u

**Resource**:  
[https://superuser.com/questions/335944/vim-in-osx-how-to-do-page-up-page-down-go-to-eol-through-a-vim-file-opened-in-t#:~:text=Page up%2Fdown %3A Using the,to start%2Fend of line](https://superuser.com/questions/335944/vim-in-osx-how-to-do-page-up-page-down-go-to-eol-through-a-vim-file-opened-in-t#:~:text=Page%20up%2Fdown%20%3A%20Using%20the,to%20start%2Fend%20of%20line)

## Programmatically add line to end of file

```
vim -b -c "Go" -c "s/.*/line of stuff to introduce/" -c "wq!" file/to/change
```

<div class="code-toolbar" id="bkmrk-copy-17"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resources**:  
[https://vi.stackexchange.com/questions/5630/how-to-quickly-add-content-in-a-new-line-at-end-of-file](https://vi.stackexchange.com/questions/5630/how-to-quickly-add-content-in-a-new-line-at-end-of-file)  
[https://unix.stackexchange.com/questions/322663/how-to-write-bash-script-to-open-vi-and-edit-document](https://unix.stackexchange.com/questions/322663/how-to-write-bash-script-to-open-vi-and-edit-document)

## Comment out a block of lines

This will comment out lines 66 through 70:

```
:66,70s/^/#
```

<div class="code-toolbar" id="bkmrk-copy-18"><div class="toolbar"><div class="toolbar-item"></div></div></div>Uncomment those lines:

```
:66,70s/^#/
```

<div class="code-toolbar" id="bkmrk-copy-19"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**:  
[https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once](https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once)

## Find and replace from current line to send of file

```
:.,$s/foo/bar/g
```

<div class="code-toolbar" id="bkmrk-copy-20"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**:  
[https://stackoverflow.com/questions/18020381/vim-search-and-replace-using-current-line-as-reference-point](https://stackoverflow.com/questions/18020381/vim-search-and-replace-using-current-line-as-reference-point)

# Password protect a file

```
vim -x file.txt
```

<div class="code-toolbar" id="bkmrk-copy-21"><div class="toolbar"><div class="toolbar-item"></div></div></div># Install vim with brew

```
brew install --with-override-system-vi vim
```

<div class="code-toolbar" id="bkmrk-copy-22"><div class="toolbar"><div class="toolbar-item"></div></div></div># Auto set paste

Add this to the end of `~./vimrc` (`~/.SpaceVim/vimrc` if you use SpaceVim):

```
function! WrapForTmux(s)
  if !exists('$TMUX')
    return a:s
  endif

  let tmux_start = "\<Esc>Ptmux;"
  let tmux_end = "\<Esc>\\"

  return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
endfunction

let &t_SI .= WrapForTmux("\<Esc>[?2004h")
let &t_EI .= WrapForTmux("\<Esc>[?2004l")

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
```

<div class="code-toolbar" id="bkmrk-copy-23"><div class="toolbar"><div class="toolbar-item"></div></div></div>This should work for tmux as well.

**Resource:**  
[https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode](https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode)

# Replace line in file

1. Highlight the line you want to copy with SHIFT v and the arrow keys
2. To copy what you’ve highlighted, input Y
3. Highlight the line you want to replace with SHIFT v
4. Hit the p key to replace it

**Resource**:  
[https://stackoverflow.com/questions/6140719/vim-how-to-replace-a-line-with-contents-of-yank-register](https://stackoverflow.com/questions/6140719/vim-how-to-replace-a-line-with-contents-of-yank-register)

# Copy code into system clipboard

1. Highlight what you want to copy with SHIFT v and the arrow keys
2. To copy what you’ve highlighted, hit “\*yy

**Resource**:  
[https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim](https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim)

# SpaceVim

## Install a custom plugin

```
vi ~/.SpaceVim.d/init.toml
```

<div class="code-toolbar" id="bkmrk-copy-24"><div class="toolbar"><div class="toolbar-item"></div></div></div>2. Add the following format to the bottom of the file for each plugin:

```
[[custom_plugins]]
name = 'nameof/plugin'
merged = 0
```

<div class="code-toolbar" id="bkmrk-copy-25"><div class="toolbar"><div class="toolbar-item"></div></div></div>3. Continue to add new plugins as needed. For example:

```
[[custom_plugins]]
name = 'hashivim/vim-terraform'
merged = 0

[[custom_plugins]]
name = 'vim-syntastic/syntastic'
merged = 0

[[custom_plugins]]
name = 'juliosueiras/vim-terraform-completion'
merged = 0
```

<div class="code-toolbar" id="bkmrk-copy-26"><div class="toolbar"><div class="toolbar-item"></div></div></div>4. To install them, simply open `vi`

**Resource**:  
[https://github.com/SpaceVim/SpaceVim/issues/73](https://github.com/SpaceVim/SpaceVim/issues/73)

## Show debug info

```
:SPDebugInfo
```

<div class="code-toolbar" id="bkmrk-copy-27"><div class="toolbar"><div class="toolbar-item"></div></div></div>## Uninstall SpaceVim

```
curl -sLf https://spacevim.org/install.sh | bash -s -- --uninstall
rm -rf ~/.SpaceVim*
```

<div class="code-toolbar" id="bkmrk-copy-28"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resource**:  
[https://github.com/SpaceVim/SpaceVim/issues/1845](https://github.com/SpaceVim/SpaceVim/issues/1845)

# Setup Vim on Ubuntu for python dev

I recommend following the guide found in resources below to get started. What I find useful for development may not be what you find useful.

# YouCompleteMe dependencies

Needed for `YouCompleteMe`:

```
sudo apt-get install -y build-essential cmake vim-nox python3-dev ycmd
cd ~/.vim/plugged/YouCompleteMe/
python3 install.py
```

<div class="code-toolbar" id="bkmrk-copy-29"><div class="toolbar"><div class="toolbar-item"></div></div></div>**Resources**:  
[Guide that I followed](https://realpython.com/vim-and-python-a-match-made-in-heaven/)  
[Plugin manager I enjoy](https://github.com/junegunn/vim-plug)

# Indent block n spaces

1. Use `Ctrl-V` to select the blocks you want to indent
2. Press `I` for insert mode and input the number of spaces you want to indent the block with your `space bar`

**Resource**:  
[https://stackoverflow.com/questions/9287813/vim-indent-with-one-space-not-shiftwidth-spaces](https://stackoverflow.com/questions/9287813/vim-indent-with-one-space-not-shiftwidth-spaces)  
[Original Article](https://techvomit.net/vim-cheatsheet/)