Vim is a free and open source terminal code editor from the old days.
-
I was born in a graphical user interface age and Vim is a new way of thinking for me.
-
The interaction with Vim is really unique and it feels like talking. I have never thought of the concept of Normal Mode and Insert Mode. This is what makes Vim so different and attractive. You navigate codes with sequential key strokes, not the shortcut that you have to press several keys simultaneously (chord). It feels more natural.
-
Using Vim is a progress of learning a language. You learn some verbs and some nouns, then you can combine yourself. There's no perfect Vim, every one builds their own Vim through time.
-
The configuration (settings) of my Vim is very simple. There are no fancy status bars and split window plugins around. I like Vim for its modest visual appearance.
-
I mainly use Vim for C++, Python and Processing most of the time.
-
Put folder
.vim
and file.vimrc
under home directory~
on Linux. Note that they should be dotfiles (hidden files), so you need to rename the file and directory with a dot.
in front of it. -
It is better to build your own Vim one line by one line from scratch, rather than copy others' configuration. This way you learn how Vim really works.
-
Read my vimrc with comments in the file to get what you need. It is important that don't put any lines in your vimrc that you don't understand.
-
To keep important keys close to reach (the home row), I map
Caps Lock
toCtrl
in my operating system and mapTab
toEsc
in Vim. (This is the original keyboard layout back in time when Vi is designed) -
Read Avoid the escape key for more solutions and trade-offs.
-
Map
;
to:
to trigger Command Mode in a single key stroke on modern keyboards. -
Map
Space
toLeader
to trigger customized shortcut more conveniently. Commonly used commands are set with a Leader shortcut, likeLeader-w
for:w
to save the file. -
Map
\
to change to next buffer:bn
to quickly change between buffers.
The folder contains several subfolders:
- colors: Vim color themes (interface and syntax highlighting).
- Monokai: Monokai theme with transparent background. This is default theme in Sublime Text.
- doc: documents for plugins.
- indent: language indent rules.
- css.vim: Better indentation for CSS.
- plugin: plugins.
- NERD_commenter.vim: Easy commenting by
<Leader>c<Space>
. I map it toCtrl-/
in.vimrc
. - fugitive.vim: Git integration, like show current branch.
:Gstatus
to show git status. - CtrlP.vim: Fuzzy file finder to replace
:e
. I map toLeader-f
. - surround.vim: Edit parentheses, brackets, quotes more easily.
ds[
to remove[
.ysiw[
to surround with[
. - matchit.vim: For HTML, match tags with
%
. - closetag.vim: For HTML, close tags with
Ctrl-_
.
- NERD_commenter.vim: Easy commenting by
- syntax: language syntax highlighting.
- c.vim/cpp.vim: function highlighting for C/C++.
- ghmarkdown.vim: GitHub-flavored Markdown highlighting. (needs to turn on by default in
.vimrc
) - gnuplot.vim: syntax highlighting for gnuplot files
-
First time users: run
vimtutor
command to learn by doing. -
A must read on motivation: Why, oh WHY, do those #?@! nutheads use vi?.
-
A structured overview for setup and usage: vim-galore.
-
More: search with keyword
vim
and you can find all kinds of answers.
Some tips I find most useful:
Don't move in Insert Mode and always tap Esc
to go Normal Mode right away after any insertion for Normal Mode is where the magic happens.
There aren't only h
, j
, k
, l
to move around to replace arrow keys. You have a lot more ways to move! If you are holding down a key to reach some place, there may exist some better way with less keystrokes.
w
(word), b
(backward) to move forward and backward by small word. W
, B
by larger word segmented by whitespaces. e
, E
(end) move at the last letter of the word, which is good for a
(append) to insert letters after the word.
{
and }
to move by paragraphs (segmented by blank lines) is a quick way to scroll.
I
to insert at the beginning of the line, and A
(append) to insert at the end of the line.
Jumping back and forth from last inserted place by g;
and g,
is really fast.
Read Vim anti-patterns for more.
Delete d
, yank (copy) y
and change (replace) c
are the main power of Vim's editing. Using Vim is like talking and you make up your own sentence.
cw
to change a word.
c$
to change the rest of the line.
c%
to change until the closing brace like ), ], }.
ciw
(change in word) to replace the word.
ci"
(change in " ") to replace the string inside " ".
You can replace the c
in above examples with d
or y
for more amazing command.
Remember you can use .
to repeat any of these command. The magic scales up! These only happen in Normal Mode, so get out of Insert Mode as soon as possible.
Record a series of commands and store to a to z and play it anywhere. For example:
qd
start recording to register d.- ... your complex series of commands.
q
stop recording.@d
execute your macro.@@
execute your macro again.
Ctrl-v
to select the lines in Visual Mode and then I
to insert.
This might be useful but maybe the .
thing is more powerful or use macros.
==
to align indentation the current line.
>aB
to indent a block by {}.
G=gg
to re-indent the whole file to the current indentation settings. Nice to clean up a messy file once and for all.
I like buffers more than tabs for buffers don't take up screen space, leaving the screen uncluttered and less distraction.
:e FILENAME
to open a new buffer. Use :CtrlP
plugin to use fuzzy finder.
:ls
to list current buffers.
:bn
to switch to the next buffer. (I map \
to this enabling super fast switching)
Read Vim Tab Madness. Buffers vs Tabs for more.
Vim has its clipboard history to store all your copy called registers. And you can save your own registers by the name of a to z.
:regs
to list all registers.
"ay
to yank the selection to register a. (I map '
to "
for quick simple tap)
"ap
to put (paste) register a.
"0p
to put (paste) the last yank.
I find this really useful to put debugging code snippets around like std::cout
in C++.
Read Advanced Vim registers for more.
You can set marks (bookmarks) at any place by the name of a to z like registers.
:marks
to show all marks.
ma
to mark current cursor position as mark a.
`a
to jump to mark a.
Ctrl-o
to jump back. (I lose Ctrl-i
to jump forward because I map Tab
to Esc
. For Vim Ctrl-i
equals to Tab
)
:set list
to show all invisible characters.
:set list!
to hide them.
Press Ctrl-n
(next) and Ctrl-p
(previous) to autocomplete words. Though I usually type the whole thing by myself.
This is the environment I work in with Vim if you would like to know:
- Operating System: Arch Linux
- Desktop Environment: GNOME
- Terminal Emulator: GNOME Terminal
- GTK+ Theme: Arc Theme
- Terminal font: Source Code Pro
I also had vim-gnome
installed for better GNOME integration (system copy-paste and terminal window title). For more information on Vim packages on Arch Linux, see Arch Linux Wiki.
Ranger is a nice console file manager with Vim-like key binding. You would feel like home with everything Vim-like. Though Vim has its file browser, Ranger is better for leisure file browsing.
For a Vim-like web browsing experience, Vimium for Chrome/Chromium and VimFx for Firefox provide customizable key bindings. You can access shortcuts of specific website by hitting i
.