Vim Workshop
The 80/20 you need to know about Vim
Setup
brew install vim
You probably want Python support
�
Vim’s config file: ~/.vimrc
And for plugins and history (see later)
Fast file navigation is a must!
Moving around quickly
gg - go to start of the file
G - go to end of the file
10G - to go to line 10 (you can open a file at line 11 with: vim file.py +11)
10j - to go 10 lines down (added this to .vimrc: nnoremap <C-e> 10<C-e>)
ctrl b / f - to scroll entire screen (or d = half screen)
ctrl i / o - to jump through last visited places
b / w - to jump over words
Stop using arrow keys
h j k l (move or motion keys) are closer to the other keys you’ll use with Vim so I recommend using them over arrow keys.
To disable the arrow keys entirely in Vim add this to your .vimrc:
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
Repeating
Q: Why can Vim make you so fast?�A: It has repetition (scripting) built in!
The dot (.) is your best friend here: you can chunk your edits (operations) and repeat them typing . (demo …)
Another form of repeating is to prepend a move command with a number, e.g.
For more complex sequences you can use a macro
q<buffer>, say ‘q’ -> start the recorder
Do a bunch of edits (takes a bit to get used to thinking this out)
@q -> replay the recorded macro
N@@ -> replay it another N times (again note the built in repetition)
Let’s do this quickly ^^^
Search
/<regex>
When on a string press g* to make that the active search string
Highlight search - set this in your .vimrc: set hls is
And to deactivate the current highlighting by pressing enter ->�nnoremap <silent> <CR> :nohl<CR><C-l>
? - to search in the other direction
Search and replace
:%s/search-term/replacement/
= current line.
Adding a g at the end it replaces all occurrences in the file:
:%s/search-term/replacement/g
(% is the current file)
Built-in autocomplete
Ctrl + n
E.g. typing au in my .vimrc, then ctrl + n it shows:
More autocompleting and type hints
Leader key
Mapleader shortcuts I use
nmap <leader>t :FZF<CR>�nmap <leader>c :%s///gn<CR> " shows number of search hits�nmap <leader>f :w<CR>:!flake8 %<CR>�nmap <leader>bl :w<CR>:!black %<CR>�nmap <leader>i :w<CR>:!isort %<CR>�nmap <leader>p :w<CR>:!python3 %<CR>�nmap <leader>y :w<CR>:!pytest<CR>�nmap <leader>v :vsplit<CR>�nnoremap <leader>n :NERDTreeToggle<cr>�map <buffer> <leader>ifm :normal iif __name__ == "__main__":<ESC>
Syntax highlighting and spaces (Python!)
Save more history
.vimrc
set undodir=~/.vim/undodir�set undofile
https://vi.stackexchange.com/a/53
I also have this set:
" tell vim to remember certain things when we exit
set viminfo='10,\"100,:20,%,n~/.viminfo
Buffers
“<char> to delete / copy (“yank”) text into a named buffer
Example of filling and retrieving multiple buffers using cntr+r�(it’s like having a clipboard on steroids)
Sequences I use a lot
SUPPORT_EMAIL = 'support@pybit.es'
Let’s replace support by info
fs to go to the first lowercase s
ct@ to delete all till @ and go into insert mode
type “info”
ESC
Commands I use a lot
d i (a) w (delete a word)
c i (a) w (change inside word)
f / F to go to the next occurrence on line of a character
ci’ (or “ or any char) - delete text inside single or double quotes and be put into insert mode
ct<char> - delete (and go into insert mode) till <char>
cf<char> - delete (and go into insert mode) till and including <char>
Check Python syntax before saving
In my .vimrc I have this function:
RaiseExceptionForUnresolvedErrors > runs pyflakes and upon fail prevent saving the file (catch errors asap)
From Kevin Y > https://gist.github.com/kyokley/0d7bb03eede831bea3fa
This solves me a lot of time (and problems down the road)
Override save = :noa w
Plugins / Vundle
Demo: let’s install indentpython.vim
Make a carbon image from selected code (bonus)
" https://github.com/kristijanhusak/vim-carbon-now-sh�let g:carbon_now_sh_options =�\ { 'lang': 'Python',� \ 't': 'seti',� \ 'bg': '#ABB8C3' }�vnoremap <leader>b :CarbonNowSh<CR>
Miscellaneous
Some other useful .vimrc settings
" remove trailing whitespace upon save
" https://vim.fandom.com/wiki/Remove_unwanted_spaces
autocmd BufWritePre * %s/\s\+$//e
" no .swp file errors please - https://stackoverflow.com/a/1588848
set shortmess+=A��“ copy / paste to / from clipboard�vmap <C-x> :!pbcopy<CR>�vmap <C-c> :w !pbcopy<CR><CR>��:nmap <c-s> :w<CR>�map <C-q> :q<CR>�(you can also use ZZ to save and exit)
�
Here is a copy of my .vimrc file: https://gist.github.com/bbelderbos/0a13568df64a92e3567ef7079e7f9a88
Resources
$ vimtutor
:help …
Vim docs: http://vimdoc.sourceforge.net
Practical Vim book: https://pragprog.com/titles/dnvim2/practical-vim-second-edition/
Vim Casts: http://vimcasts.org