Vim Notes
There are several modes in VIM. NORMAL
, INSERT
, VISUAL
are most common ones.
The Most Used Default Key Bindings
:q
: quit VIM.:wq
: save the current buffer and quit.:q!
discard unsaved changes and quith
,j
,k
,l
: Movement. Example:8k
- move 8 lines up:99
: jump to the line 99w
: move one word forwardb
: move one word backward$
: move to the end of the current line0
: move to the beginning of the current lineo
: insert a new line below and go insert modeO
: insert a new line above and go insert modeA
: go insert mode at the end of the current liney
: yank (copy) selected textp
: paste the yanked textd
: delete the selected textdd
: delete the current linex
: delete the cursor characterv
: select the cursor characterV
: select the current line:sp
: split the screen horizontally:vs
: split the screen vertically/john
: search for “john” in the currentfilebuffern
: jump to the next search resultN
: jump to the previous search resultgg
: jump to the top of the current fileG
: jump to the bottom of the current file%
: jump to the closing/opening pair bracket:%s/abc/xyz/g
: replace every “abc” with “xyz” in the current file:vimgrep
: search current directory, see the help for more info:help
: open VIM help
Vim “leader” Key
<leader>
key is a vim tool that can be used to create personalized shortcuts.
Default <leader>
key is backslash \
. To change the leader key to comma ,
add let mapleader = ","
to your vimrc. You can use any other key as <leader>
key, even space or function keys. But noting that you only have 1 second to press the actual shortcut key after the leader key is stroke(by default of course, you can configure it after all), it’s kinda shitty to use backslash or function key as leader, isn’t it?
Mapping a command with <leader>
key is pretty easy.
|
|
Above mapping means if you press <leader>
and h
key in sequence, it will run :noh
(remove highlighting for in-buffer search).
Mapping custom functions? No problem!
Above function toggles both relative and normal line numbers but not relative when using versions before vim 7.4.
And here’s the mapping for it.
|
|
Vim Buffer
Buffer is the coolest concept I’ve learned while I use Vim to be honest.
In Vim, a buffer is nothing more than text that you are editing. For example, when you open a file, the content of the file is loaded into a buffer. So when you issue this command:
|
|
You are actually launching Vim with a single buffer that is filled with the contents of the .vimrc file. Easy peasy!
Now let’s look at what happens when you try to edit multiple files. Let’s issue this command:
|
|
Now that you’ve opened two buffers, you can hop from one to another, it’s very very similar to tabs in other applications like VS Code or Chrome.
For example, :bnext
will lead you to the next buffer, and it cycles through the list of all open buffers.
So adding below two lines to your .vimrc
file will give you a superpower to use buffer smartly and visually (I assume you’re using airline plugin. But there must be similar ways if you are not using airline).
You can view all the buffer commands by :h :buffer
, but personally, I found only using :bnext
and :bd
was cool enough for my needs.
There are other related concepts in Vim, like window
and tab
. A window in Vim is just a way to view a buffer. Whenever you create a new vertical or horizontal split, that is a window. And Tabs? A tab is just a collection of windows according to the docs. However, correct use of buffers may bring a lot of fun on its own.
Now that you’ve got a correct understanding of buffers in Vim, you can supercharge your daily coding with Vim! :D
Search and Replace Across Multiple Files
Vim has a native feature called “quickfix”, which facilitates finding a list of positions in files. You can check it out by typing :help quickfix
.
You can use Vim’s native command to add findings to the quickfix list, or 3rd party extensions to achieve the same.
Using “vimgrep” Command
Let’s say you want to find all occurrences of windows
in the current working directory.
:vimgrep /windows/gj **/*.*
g
is a flag instructing “Add all matches in a line to the quickfix list.”j
is a flag instructing “Do not jump the cursor to the location of first pattern match.”**
is the pattern for all subdirectories*.*
is the wildcard file name with wildcard extensions
If you want to search in a specific subdirectory:
:vimgrep /windows/gj contents/**/*.*
contents
is the subdirectory name
As the result of vimgrep
command, the quickfix list will be filled with all findings.
Using Telescope with RipGrep
Very simple, you only need to press <Ctrl + q> after you got findings. It will add all findings to the quickfix list.
Nativating the Quickfix List
Here are commands that you can manage the quickfix list.
- :copen - Open the quickfix list window.
- :ccl or :cclose - Close the quickfix list window.
- :cnext or :cn - Go to the next item on the list.
- :cprev or :cp - Go to the previous item on the list.
- :cfirst - Go to the first item on the list.
- :clast - Go to the last item on the list.
- :cc
- Go to the nth item.
Replace Across Files
Vim has cfdo
and cdo
commands that allows you to run commands for each files in the quickfix list.
:cfdo %s/windows/window/g | update
Above command will replace all occurrences of windows
with window
in the quickfix list and write the buffers.
Code Folding
zf
- Create a fold with the selected linesza
- Toggle the current foldzo
- Open the current foldzc
- Close the current fold
There’re a lot more options you can try. Read the docs for more. :help fold
Common Issues
Pasting from the system clipboard generates extra empty lines and indents.