1 of 24

Vim Workshop

The 80/20 you need to know about Vim

2 of 24

Setup

brew install vim

You probably want Python support

Vim’s config file: ~/.vimrc

And for plugins and history (see later)

3 of 24

Fast file navigation is a must!

4 of 24

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

5 of 24

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>

6 of 24

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.

  • 5x - delete 5 characters (and stay in normal mode)
  • 4s - delete 4 characters (and go into insert mode)
  • 3dw - delete 3 words
  • 10~ swap case 10 characters

7 of 24

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 ^^^

8 of 24

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

9 of 24

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)

10 of 24

Built-in autocomplete

Ctrl + n

E.g. typing au in my .vimrc, then ctrl + n it shows:

11 of 24

More autocompleting and type hints

12 of 24

Leader key

13 of 24

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>

14 of 24

Syntax highlighting and spaces (Python!)

15 of 24

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

16 of 24

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)

17 of 24

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

18 of 24

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>

19 of 24

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

20 of 24

Plugins / Vundle

Demo: let’s install indentpython.vim

21 of 24

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>

22 of 24

Miscellaneous

  • Undo and redo: u / crtl+r
  • Where am I in the file? ctrl+g
  • Check spelling :set spell
  • Show line numbers :set nu
  • Indent / dedent: > / <
  • To enter visual mode (line wise): V (shift v)
  • Split window
    • Inside vim -> :vsp file
    • Command line / open files in split window: vi -O script.py test_script.py

23 of 24

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

24 of 24

Resources