Do you want to process words or edit text?
I use vim for more than 12 years, most of the time as my primary editor for text and code. This post is a summary of a short master class for Donetsk Coffee’n’code meeting in December 2013. More experienced vim users should probably go directly to the end to check collection of links.
Power of Modes
Quite often I find that it is much more efficient to focus on one task than to multitask and achieve nothing.
For example, here is one singletasking lifehack I use: In a given time, I typically participate in several projects. Last year, I found that it was very effective to create different accounts in my computer OS for such different roles. It did help to make processes much more efficient.
One of the most important concepts for beginners starting to use vim is that working with it we are also switching modes. In a given moment, we are writing, moving arround, searching and so on. When you embrace this separation into modes it does make text editing more effective.
When we just open vim we are in normal (moving) mode that allows us to move around efficiently. Learn the basic for moving by typing
vimtutor from command line.
Here are some keys that I currently use for navigation in addition to standard ones quite often:
G to go to the end of file,
gg to go to the beginning of file,
zz for centering.
Also remember to navigate by words (
b) or other text concepts and with search of the current word (
#) and search in general (
/), which can be considered as a separate mode.
Tags are a standard feature of vim. Typically I create Makefile command for each project I work on that creates tags with
ctags -R .. Navigation between tags:
:ts. Here and in many other cases
:help tags is your friend.
The easiests way to go into insert (text entering) mode is with
i key, one can go back with
Esc, but it is very naive to use just that option. One can start editing from the beginning of line (
I), from the end (
A), create an empty line after and before current line and start editing (
o), these options are very useful in many cases.
Do not try to imediately memorize as many keys as possible. Start with very minimum. After some time you will get familiar with more. Try to observe ineficiencies in your use and search for solutions.
Many vim keys are mnemonic, and after some time they will “make sense”. For example, similar to
A that appends text at the end of the line, there is
a that appends after the cursor.
Normal mode is not only for navigation but is also good for editing with concepts (words, lines, text objects, paragraphs, marks, motions etc.) And the way one communicates this editing commands is very similar to a language. Vim grammar has a fixed word order:
<command count><verb><motion count><modifier><object>
Counts and modifiers are not always relevant. So for the beginning you might want to keep a simplier picture in mind:
The major verbs are
c (for yank, delete, and change).
Some motions objects are
$ for beginning and end of line;
t<character> for find the character and till the character. Normal search with
/ operator also can be used.
Text objects can be words (
w), brackets (
(), tags (
t) etc. Modifiers are
a to indicated if we take inner part of the object or the whole object.
ct)- change till the closing parenthesis
ci]- change inside square brackets
da]- delete square brackets
"+yit- copy what’s inside a tag to a
+buffer that corresponds to system clipboard
dG- delete till the end of file
If you install surround plugin:
ys3w'- surround 3 words with single parenthesis
And here example in visual (yet another mode) is appropriate:
<Ctrl-V><select text with motion commands>S" - surround it with double parenthesis.
The Wisdom of Crowds
Speaking of plugins.
Here are some plugins that I would like to mention (in the order of use frequency):
- CtrlP - fuzzy file finder
- Fugitive - git wrapper
- NERDTree - file system tree explorer
- Tagbar - displays tags in a side window for easy navigation
- Emmet - improves HTML workflow
As with keys, do not try to install many plugins at once and start using all of them. Go incrementally.
The post above is a summary of a short talk about vim. If you’d like to go deeper, check out links below. They are a compilation of vim-related links from my Wiki, Twitter, Delicious. They are in no particular order. More important in my opinion are on top and highlighted in bold.
- VimGolf - real Vim ninjas count every keystroke! - not quite a tutorial but a lot can be learned by doing the challenges
- Vim Text Objects: The Definitive Guide
- The grammar of Vim
- A vim Tutorial and Primer - a deeper dive
In general I would not recommend to use cheetsheets for learning vim, this one though is well organized.
- Secrets of the Vim Ninja - systematization of keywords
- Best of VIM Tips, gVIM’s Key Features zzapper
- Vim anti-patterns - Arabesque
- YBlog - Learn Vim Progressively
- Dr. Bunsen / Writing in Vim - advices for using vim as word processor
- Vim After 11 Years - Ian Langworth’s Things of Variable Interest
- An Incremental Approach to Vim - Experiment and Routine
- How I boosted my Vim » nvie.com
- Luciano Fiandesio - VIM configuration for happy Java coding
- List of vim plugins I use - with mini tutorials · mirnazim.org
- Grokking the Zen of the Vim Wu-Wei • Jeet Sukumaran
- 7 Habits For Effective Text Editing 2.0 - YouTube
- Bare Bones Navigation, by Kris Jenkins on Vimeo
- OSCON 2013: "More Instantly Better Vim" - Damian Conway - YouTube
- Efficient Text Editing in Vim – Alan Christopher Thomas – Show-Me Tech #1 - YouTube
- Using Vim as Python IDE - PDF slides of talk by Kostyantyn Leschenko during last OdessaPy, good not only for Python developers
- Turning Vim into a modern Python IDE
- Vim+Python. Для начинающих / Хабрахабр