Why vim?
- To my knowledge, it is the fastest way for a human to process text on a
computer
- Can anyone think of another?
- It’s highly customizable and programmable
- It’s highly extensible with plugins
- It’s a power tool, which after a lot of practice and experience with it allows
you to:
- write almost as fast as you think
- correct, revise and reorganize your text extremely fast
- vim was created mostly for coders, but is also an excellent platform for writers. If you are coder and a writer, vim is pure joy to work with.
vim documentation
:h registers
Basic tutorial
- Vim registers: The basics and beyond
- Intro
- bunch of spaces in memory
- these spaces are the registers
- vim uses them to store text
- each space has an identifier
- text in the spaces can be accessed later
- similar to storing text in computer clipboard
- but vim has many places to store the text
- bunch of spaces in memory
- The basic usage
- accessing a register
- use a double quote before its name
- Example, to access text in register ‘r’:
"r
- copying selected text to register:
"ry
- pasting text from register:
"rp
- registers can be accessed from insert mode:
<c-r>
- Example
<c-r> r
:reg
command shows all registers and the content- to view only certain registers, add a space separated list after the
command:
:reg a b c
- to view only certain registers, add a space separated list after the
command:
- accessing a register
- the unnamed register
- accessed with
""
- holds any text deleted or yanked
- by default,
p
key pastes from unnamed register- same as doing `"”p
- Never lose a yanked text again
- Numbered registers
- numbered 0 through 9 (“0 to “9)
- “0 has the latest yanked text
- “1 through “9 has the latest deleted text (with 9 being the oldest)
- you can always paste yanked text with
"0p
- Numbered registers
- accessed with
- The read only registers
- There are 4 read only registers: “., “%, “: and “#
".
contains the last inserted text"%
contains the current file path- copy current path to file to clipboard
:let @+=@%
- copy current path to file to clipboard
":
- most recently executed command
- can be used to rerun last command:
@:
"#
name of the alternate file- can be thought of as the last edited file
- see
:h alternate-file
- vim uses it to switch between files when <c-^> is used
- can also do
:e <c-r> #
- can also do
- There are 4 read only registers: “., “%, “: and “#
- The expression and the search registers
"=
- known as the expression register
- used to deal with the result of expressions
- in insert mode, type
<c-r> =
- type in the expression and result will be printed
- Examples:
<c-r>=2+2
will output4
to the buffer<c-r>=system('ls')
will output the list of all files to the buffer
"/
- Search register
- where the last search text goes
- changed when a search is performed with
/
,?
,*
or#
- changed when a search is performed with
- Macros
- recording are stored in named registers
- you don’t have to re record a macro if you do it wrong
- you can just edit the register it is in, instead
- uppercase register names are how we append contents to a register:
:let @W='i;'
- to edit a macro in the middle, do:
:let @w='<c-r> <REGISTER>
- Intro
Maps
Viewing existing maps
:nmap
for normal mode mappings:vmap
for visual mode mappings:imap
for insert mode mappings:verbose map
Here’s a random collection of vim maps I use:
Plugins
Some useful plugins I use (not a complete list):
Plugins I’m currently exploring
Jump lists
Command Line History
- :help history
- type in first few letters, hit up arrow
- finds previous commands that start with those letters
- :his:
- lists all previous commands
- More info: Using command-line history | Vim Tips Wiki | Fandom
Completion
echo complete_info()
to get current completion info<c-e>
to exit completion mode<c-y>
to choose currently highlighted item in menu- good to add to vimrc:
set completeopt+=longest
- allows you to whittle down options in the popup menu by typing more letters
Official documentation
From vim command line, run:
- :h ins-completion
- :h omnifunc
- :h compl-omni
- :h completeopt
Navigating buffers
- From Buffers Working with Vim - mkaz.blog
- What are buffers?
- text files residing “in-memory”
- vim window is a viewport on the buffer
- switching
- you can switch between buffers
- viewport can show a different buffer
- you can switch between buffers
- author posits that buffers are better than tabs
- Opening Multiple Files
vim file1 file2 file3
- open new file in an additional buffer
:e file
- Main Buffer Commands
:buffers
or:ls
- list buffers
:b {bufname}
use buffer name, supports completion- good for switching between buffers
:bd
- close current buffer
:bn
- next buffer
:bp
- previous buffer
:b#
- last buffer
:b1
- buffer 1
:bm
- move not next modified buffer
- buffer 1
- Recommended shortcuts
nnoremap <leader>3 :b#<cr>
- to bounce back to previous buffer
nnoremap <leader>n :bn<cr>
- go to next buffer
nnoremap Q :bd!<cr>
- close buffer
nnoremap <Leader>, :Buffers<CR> "
- browse buffers
- comes from the fzf.vim plugin
- allows for a more forgiving fuzzy match of buffer names
- author has set up fzf and ripgrep for advanced searching
- What are buffers?
spell check feature
- turn on with
set spell
- highlight red with:
hi SpellBad guifg=red
- useful mappings
- zg -> add word under cursor to dictionary permanently
- zG -> add word under cursor to dictionary temporarily
- more mappings: http://vimdoc.sourceforge.net
z=
:- have vim suggest alternatives
]s
:- move to next misspelled word
- [s:
- move to back to previous misspelled word
Useful resources and good reads
CTAGS
Vim scripting
Searching
- lvimgrep
:lv[imgrep][!] /{pattern}/[g][j] {file} ...
- vimgrep
:vim[grep][!] /{pattern}/[g][j] {file} ...
Other notes linking here:
View the edit history