Vim Cheatsheet

Make json human readable

:%!python -m json.tool

Resource: https://blog.realnitro.be/2010/12/20/format-json-in-vim-using-pythons-jsontool-module/

Auto Indent Entire File

If you want to autoindent a file you're on, use the following keyboard shortcut:

gg=G

So basically you use gg to get to the start of the file, = to indent and G to get to the end of the file.

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

Resources:
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

Alternative to esc

ctrl-c

Alternative to wq!

ZZ

Alternative to q!

ZQ

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

Password protect a file

vim -x file.txt

To set a strong encryption method, run this while the file is open:

:setlocal cm=blowfish2

Resource: https://www.tecmint.com/password-protect-vim-file-in-linux/

Decrypt it

Open the file and do the following:

  1. :X
  2. Press the enter key twice

SpaceVim

Install a custom plugin

  1. vi ~/.SpaceVim.d/init.toml
  2. Add the following format to the bottom of the file for each plugin:
[[custom_plugins]]
name = 'nameof/plugin'
merged = 0
  1. 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
  1. To install them, simply open vi

Resource: https://github.com/SpaceVim/SpaceVim/issues/73

Show debug info

:SPDebugInfo

Install vim with brew

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

Get version of vim

:version

Find all lines with a pattern and delete

Find all lines containing pattern and delete them:

:g/pattern/d

Resource: https://vim.fandom.com/wiki/Delete_all_lines_containing_a_pattern

Go to end of line

$

Resource: https://stackoverflow.com/questions/105721/how-do-i-move-to-end-of-line-in-vim

Remove blank lines

:g/^$/d

Resource: https://stackoverflow.com/questions/706076/vim-delete-blank-lines

Auto set paste

Add this to ~./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()

This should work for tmux as well.

Resource: https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode