Useful Keyboard Shortcuts
Select the beginning of a function
Shift-V -
Get version of vim
:version
Get to the end of a function
$%
Autoindent the current file
gg=G
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 '.'
Alternatively, you can also use python:
:%!python -m json.tool
Resources:
Paste over line
Vp
Resource: 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
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
Encrypt file
:setlocal cm=blowfish2
Resource: https://www.tecmint.com/password-protect-vim-file-in-linux/
Decrypt file
Open the file and do the following:
:X
- Press the enter key twice
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
Open file
:n <file>
Resource: https://stackoverflow.com/questions/23680778/how-do-you-open-a-file-from-within-vim
List files in a directory
:e <directory>
Change directory
:cd <directory>
Resource:
Page down
ctrl-f
Down half a page
ctrl-d
Page up
ctrl-b
Up half a page
ctrl-u
Resource:
Programmatically add line to end of file
vim -b -c "Go" -c "s/.*/line of stuff to introduce/" -c "wq!" file/to/change
Resources:
- 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
Comment out a block of lines
This will comment out lines 66 through 70:
:66,70s/^/#
Uncomment those lines:
:66,70s/^#/
Resource:
Find and replace from current line to send of file
:.,$s/foo/bar/g
Resource:
Paste from system clipboard
"*p
Resource:
Password protect a file
vim -x file.txt
Install vim with brew
brew install --with-override-system-vi vim
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()
This should work for tmux as well.
Resource:
Replace line in file
- Highlight the line you want to copy with SHIFT v and the arrow keys
- To copy what you’ve highlighted, input Y
- Highlight the line you want to replace with SHIFT v
- Hit the p key to replace it
Resource:
- https://stackoverflow.com/questions/6140719/vim-how-to-replace-a-line-with-contents-of-yank-register
Copy code into system clipboard
- Highlight what you want to copy with SHIFT v and the arrow keys
- To copy what you’ve highlighted, hit “*yy
Resource:
SpaceVim
Install a custom plugin
vi ~/.SpaceVim.d/init.toml
Add the following format to the bottom of the file for each plugin:
[[custom_plugins]] name = 'nameof/plugin' merged = 0
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
To install them, simply open
vi
Resource:
Show debug info
:SPDebugInfo
Uninstall SpaceVim
curl -sLf https://spacevim.org/install.sh | bash -s -- --uninstall
rm -rf ~/.SpaceVim*
Resource:
Vim Plugins
Setup Vim on Ubuntu for python dev
I recommend following this guide 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
Resources:
Indent block n spaces
- Use
Ctrl-V
to select the blocks you want to indent - Press
I
for insert mode and input the number of spaces you want to indent the block with yourspace bar
Resource: