
How to boost your Vim productivity - mirceasoaica
http://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
======
roel_v
Step 1 for those who _really_ want to boost productivity (but let's be honest,
most of us don't!): forget about all those plugins that are time sinks to
configure and integrate with each other, about obscure tweaks that take half a
day to get right, and about elaborate .vimrc's that take you so far away from
'standard' that you're crippled without them. Stick to a fairly basic standard
install and a .vimrc of 50 lines, tops.

I've been using vim for 15+ years now, since late 1990's. I use viemu in
Visual Studio and I've written code in 10+ languages in vim. I've written non-
trivial vimscripts and integration tools with other parts of my workflows over
the years. I love how comfortable it feels, and I feel crippled when I have to
use a 'normal' editor; like walking with a little pebble in your shoe - not a
big a deal enough to prevent you from doing anything you could otherwise, but
still damn uncomfortable.

With that said, I'm not convinced that I've actually _saved time_ with it. I
shudder if I think of the many man-days I spend 10 years ago on getting this
and that plugin working, tweaking settings and keybindings for things I use
twice a month tops and automating things that I could have done manually 10
times faster. It's not like regular editors are _that_ much slower to use. It
felt like throwing off a yoke when I dumped all those plugins so that I could
focus on writing software, instead of tweaking the editor so that I could
write programs with a few keystrokes.

So yeah, that was gramps' advice I guess...

~~~
samuell
I started feeling the same, about the configuration time sinks ... my solution
was to create ansible-configurable docker images with everything setup ... so
I can throw away and recreate the images once a month, when the plugins get
screwed up (which they really do, about once a month or so!):

\- Go version: [https://github.com/samuell/devbox-
golang](https://github.com/samuell/devbox-golang)

\- Python version: [https://github.com/samuell/devbox-
python](https://github.com/samuell/devbox-python)

I love being able to SSH in to a fresh docker (yes, I use fat SSH:able
docker), and enjoy full auto-completion for python and Go! :)

But yeah, except for the auto-completion and a few minor tweaks, there's not
too much extras there.

~~~
hobarrera
Why not handle the configurations with git? Handling dotfiles with git is
pretty common nowadays, and you can roll back/forward as you please.

Plus, the overhead is a lot less.

------
rav
In my 6 years of coding and writing LaTeX in Vim, I have found that the
greatest boost to my Vim productivity is learning all about the vi and ex
foundation of Vim. My Vim knowledge applies 100% to any default Vim
installation on a modern distro, and my vimrc contains mostly trivial tweaks.

This means using :normal and macros instead of :s for most of my
search/replace actions; using H, M, L, {, } to navigate quickly in the lines
visible on the screen, and using f/F/t/T/;/,/% to navigate quickly within a
line.

I would say that 98% of my autocomplete needs are fulfilled by token
completion (:help i_CTRL-N) and line completion (:help i_CTRL-X_CTRL-L).

I frequently use the command-line window (:help c_CTRL-F) and listing matching
names (:help c_CTRL-D).

I specifically don't have mappings that involve the leader key, and I don't
use the Ctrl-P plugin or a package manager or anything like that -- I honestly
don't think that mapping <Leader>w to :w<CR> will make me any more productive
in Vim.

~~~
krat0sprakhar
> This means using :normal and macros instead of :s for most of my
> search/replace actions

Interesting. Can you please elaborate more on this?

~~~
rav
Suppose a range of lines contains function calls in some programming language:

    
    
        i = foo(a, 42, 1)
        j = bar(b, c, 100, 2)  # this is a comment (well, duh)
        bazbar(etc, 3)
    

You want to edit the last parameter in each function call -- change it to
'hello'. I have two ways of approaching this: macros (interactive) or :norm
(less interactive).

1\. Put your cursor on the first byte in the first line, type qq to record a
macro of your change, q to finish. Select the rest of the lines in visual line
mode and type :'<,'>norm @q to run the macro on each of the remaining lines.

2\. Select the lines in visual line mode and type, for instance, :'<,'>norm
%F,ws'hello' This is the "non-interactive" version of the above since the
macro keystrokes are given directly to :normal. Pro: ability to undo
keystrokes unlike when recording a macro. Con: cannot actually see the effect
of the keystrokes (but with practice, this is not a problem).

(Note that colon in visual mode automatically inserts the :'<,'> so you only
have to type :norm ...)

I find the declarative nature of regexes/:s to be too restrictive -- I much
prefer the operational nature of macros/:normal, since that lets me make the
change I want directly, without me having to phrase the change as a regular
expression.

I could also use CTRL-A in normal mode to increment the last argument in each
line: :'<,'>norm %b^A (where ^A is the literal, typed CTRL-V CTRL-A)

I feel like a wizard whenever I use :normal in a non-trivial manner.

~~~
pyvpx
care to suggest any resources that helped you reach wizard-like levels of
usage? I very much wish to work towards that kind of ability in vi/vim.

~~~
rav
I am sorry, but I don't know any resources... I have collected a few things on
the following page: [http://users-cs.au.dk/rav/vim/](http://users-
cs.au.dk/rav/vim/)

Basically, I achieved my wizard-level C++, Vim and Git knowledge during my
three years as a part-time student programmer in the basic research center
MADALGO. I took the time to study the documentation (respectively the C++11
draft, :help and the git man pages) whenever I was curious. For Vim in
particular, I made sure to eliminate all repeated keysmashing in my daily
workflow, which "unfortunately" required me to learn about macro wizardry.

My only advice is to keep practicing, to keep trying to spot inefficiencies in
your own workflows, and to actively eliminate these inefficiencies by studying
the Vim help pages.

------
andrewstuart2
I'm all for bending your text editor to your will, but a few of these tips
gloss over the core of vim: the shortcut _language_ it offers and the
composability of its parts of speech [1].

For example, remapping the paste key to _paste_ \+ _move to end_ is cool, but
`ppppp` to paste 5 times circumvents vim's killer repetition. Want to paste
100 copies? (hopefully you do this rarely). Just type `100p` and you're done.
I won't bother typing out the obvious sequence of repeated `p` presses, but
you get the drift.

Also, remapping v to progressively select larger surrounding text objects will
keep you from learning that you can perform any action on the 3rd parent curly
braces by following the action with the text object `3a{` or `3a}`. Select it:
`v3a{`, delete it: `d3a{`, comment it out `gc3a{` ([2]), whatever. If there's
an action in vim you can perform it on a text object which can usually start
with some count.

I really do mean language, too. When I'm using vim, I feel a lot more like I'm
communicating with my computer than trying to figure out how to do what I want
to do.

The actions are there: delete, change, select. The direct objects are there:
braces, this line, line number N, end of this word, the next character C, this
paragraph. The prepositions are there: inside, outside, up to (inclusive,
exclusive).

Heck, the sensible defaults feel a whole lot like the "understood" words that
you can leave out of sentences. Instead of "navigate to line 30", you can just
use the shortcut for "line 30" (30G) and be done.

One of my favorite things about vim is the number of times I've literally been
_surprised_ that vim didn't read my mind and go where I was looking. Once
you're familiar with the different pieces, it's so good that you might expect
it to read your mind.

</almost-blog-post>

[1] [http://ferd.ca/vim-and-composability.html](http://ferd.ca/vim-and-
composability.html)

[2] [https://github.com/tpope/vim-commentary](https://github.com/tpope/vim-
commentary)

~~~
rav
> I really do mean language, too. When I'm using vim, I feel a lot more like
> I'm communicating with my computer than trying to figure out how to do what
> I want to do.

I tell people learning Vim or Emacs that they should treat it like learning a
new programming language -- a programming language for text manipulation. (In
particular, don't attempt a first project in C++ while writing your first
report in LaTeX, both in Vim for the first time -- it will not be a pleasant
experience!)

~~~
andrewstuart2
On the other hand, if you like crying yourself to sleep, then that's
definitely a good idea. :-D

------
dyates
This post has some valuable tips (I'm probably going to try out <Space> as a
leader), but one or two near the start rub me the wrong way in terms of how I
use vim and how it's my impression that it's intended to be used.

>It seems like vvv is slower than vp but in practice I don’t need to think
beforehand what to select, and what key combination to use.

The way I've always used vim and always thought it was intended to be used is
that you do think beforehand. You sit at your editor, think about what changes
you want to make, and then key in a set of precision commands in vim-editing-
language and it happens.

>This way v replaces viw, vaw, vi", va", vi(, va(, vi[, va[, vi{, va{, vip,
vap, vit, vat, ... you get the idea.

I kind of like the precision of having all of those different things, and of
course the option of using them for more than just visual select but also
change, delete, and so on. Although I suppose this doesn't remove any of those
keymappings, I must protest remapping Ctrl+v: I can't even use an editor
without block select.

I imagine there's a plugin (or even builtin feature) that at least generalises
"s, (s , [s, tags and things of that sort though.

>Stop that stupid window from popping up: >map q: :q

I know it's a weird and irritating thing to have that window pop up when you
meant to quit, but it's actually a very neat interface: a whole vim buffer for
recomposing commands and your command history for later execution (almost
acme-like). Give the poor guy a chance.

As a counterpoint to what I've pointed out above, I'd like to recommend Drew
Neil's [ _Practical Vim_ ]([http://www.amazon.com/Practical-Vim-Thought-
Pragmatic-Progra...](http://www.amazon.com/Practical-Vim-Thought-Pragmatic-
Programmers/dp/1934356980)) to anyone who hasn't read it already. It's got a
lot of great content, and really goes a long way to explain vim's quirks and
methods of doing things.

One of the useful tips I learnt from that was the ex command "normal", which
allows you to execute a string of normal mode commands over a range of lines.
So, for example, you can append a semicolon to each line in a visual selection
by entering

    
    
        :'<,'>%normal A;
    

A small thing, but one that I've used a lot since learning about it.

~~~
kweinber
+100 for Practical Vim... Hands down the best vim guide. After 10 years of
kinda-knowing-vim, that book changed how I use vim entirely.

------
ortuna
You may also like spacemacs. They've got a pretty nice <SPACE> \+ a lot of
keys binding system.
[https://github.com/syl20bnr/spacemacs](https://github.com/syl20bnr/spacemacs)

------
orthecreedence
This was great even just for the

    
    
        <Leader>p "+p
    

mapping. I do this all the time ("+p). It's weird because I tend to go through
waves of realizing "Oh! I can just remap that!" I feel like I have fairly good
vim skills but somehow always forget I can keep making it _more_ efficient
than it already is.

Thanks for the reminder =].

~~~
ThrustVectoring
I've got vim set up to use the system clipboard automatically. Want to visit a
URL in a readme file? yiW, tab over to chrome (with vimium), hit P to open it
in a new tab. Want to add a URL to documentation? yy it from chrome, tab to
terminal, p it in.

------
erikb
It is a long long time ago that someone taught me something really new about
vim and used plugins that are not just fancy (looking at most status line
plugins or nerdtree) but actually seem to improve productivity in a vim-ish
sense. Kudos!

~~~
pyre
python-mode or jshint2.vim? I don't think that they are "just fancy. Bringing
linting into the editing window is more helpful (to me personally at least)
than having a separate process running with it.

(Note: I turn rope off in python-mode because it's horribly inefficient, and
sometimes causes Vim to delay reacting to user input)

------
INTPnerd
This is one of the best "how to boost you Vim productivity" articles I have
ever seen. It really gets to the heart of the types of customizations that a
lot of people don't consider that make a massive difference.

------
grondilu
With bash, if 'fg' is too long to type you can just type '%'.

~~~
arc_of_descent
I use 'fg' a lot. Thanks for the '%' tip. Now to practice and grow some memory
muscles!

------
joelthelion
Why don't people seem to use the F keys? I have mappings for very frequent
functions on F1-F5, and it really helps:

    
    
        map  <F1> <Esc>:w<CR>|      "Fast save
        imap <F1> <Esc>:w<CR>|      "Fast save
        map  <F2> :make<Up><CR>|    "Fast compile
        map  <F3> :bn<CR>
        map  <S-F3> :bp<CR>
        " Same mapping for gnome-terminal ( see     http://stackoverflow.com/q/12813126/164171 )
        map ^[O1;2R :bp<CR>
    
        map  <F4> :bd<CR>|          "Close buffer
    
        map  <F5> :cnext<cr>
        map  <S-F5> :cprev<cr>
        imap  <F5> <Esc>:cnext<cr>
        imap  <S-F5> <Esc>:cprev<cr>

~~~
andrewstuart2
One of my favorite bits for saving my own butt: commit more.

    
    
        "Function to commit.
        function! GitCommit ()
          execute 'write'
          let message = input('Enter commit message: ')
          execute '!git add ' . bufname('%') . ' && git commit -m ''' . message . ''';'
        endfunction
        
        imap <F7> <Esc>:call GitCommit()<CR>
        map <F7> <Esc>:call GitCommit()<CR>

~~~
leephillips
Have you tried fugitive (a vim wrapper around git)?

------
blueblob
A few of these are good suggestions but I can't live without C-v, whether I am
already in visual mode or not. Block selection may not be used that frequently
but when it is, it's a lifesaver.

~~~
yramagicman
I use block selection so often that I've switched the mappings in my vimrc. I
have v mapped to visual-block selection and ctrl-v mapped to visual selection.
It's very rare that I miss normal visual mode as I use block and line
selection more than anything.

------
yramagicman
I have hacked my tabline to be a list of my open buffers. It still functions
as a tabline, for those of you who use tabs, but it's kinda nice to see all my
open buffers in a nice list across the top of my screen.

[https://github.com/yramagicman/dotfiles/blob/master/.vim/aft...](https://github.com/yramagicman/dotfiles/blob/master/.vim/after/plugin/tabline.vim)

~~~
graywh
See also: bufexplorer, MiniBufExplorer, buftabs

------
clinta
I disliked the part about setting up Vim on servers. Do you really want to be
adding all the dependencies for your editor on every server? I'm an advocate
of using the netrw plugin to edit over ssh directly and avoiding logging into
servers when not necessary.

vim scp://host//etc/whatever

~~~
sheerun
Plugins aren't installed until you call :PlugInstall

You can also use .nvimrc instead .vimrc, so only nvim instance on server gets
plugins.

------
Veritaas
The most productive mapping ever: imap jj <ESC>.

No more having to reach for the escape key.

~~~
positr0n
Even better:

    
    
        imap jk <ESC>
        imap kj <ESC>
    

I just hit both and j at the same time with my pointer and index fingers so
it's essentially one keystroke.

~~~
thomasahle
How does this work? What if you are in insert mode and actually want to type j
or k?

~~~
leephillips
It's the combination jk (or kj) that is set up to switch from insert to normal
mode. The idea is that this combination rarely occurs in English text. The
drawbacks are a slight delay in cursor movement when typing j or k as vim
waits to see what comes next, and momentary confusion when you inevitably need
to actually insert these sequences.

------
gfodor
This is great. One thing the author should do is remap enter to something
better than "jump to end of file." I remapped mine to ":w<CR>" to save the
current file, and it's stuck.

------
mastensg
Here is a way to do the Ctrl-Z trick in Bash:

[https://gist.github.com/mastensg/6d9030e25699c2eed5c4](https://gist.github.com/mastensg/6d9030e25699c2eed5c4)

~~~
kokonotu
Thanks! I am looking for this

------
yramagicman
Does anyone have any advice on bindings to change split size? Ctrl-w + and
Ctrl-w - just don't work well, and my current configuration shadows Ctrl-a,
which I really would like to avoid.

~~~
rav
I rarely resize windows, but when I do, I set mouse=a and use the mouse to
drag the window borders around. There's also CTRL-W _ (underscore) to maximize
the current window and CTRL-W = to make window sizes equal.

~~~
yramagicman
Also, did you know that Ctrl-w | maximizes the a window horizontally? So if
you have splits open above and beside you ctlr-w _ ctrl-w | will maximize your
current split.

 _immediately maps this to ctrl-w m_

~~~
ta0967
yes i did, having read the manual. :) there's also `:on[ly]` (i have `set
hidden` in my .vimrc)

------
floatboth
The zsh Ctrl+Z binding is brilliant!

The Space and Ctrl+Space thing is interesting, but not interesting enough for
me to switch (from , and Ctrl+q – with Ctrl being on Caps Lock).

------
critium
Learning that makeprg can be anything (even a script) made a huge difference
to me. That and errfmt means I dont need to leave vim to compile or lint

------
bliti
Very useful, thank you. I'm spending more and more time using Vim through ssh
and trying to re-learn as much as I can.

------
shmerl
Lately I use neovim, because it allows using 24 bit color themes in the
terminal.

~~~
INTPnerd
So neovim is usable now? Do you run into many problems with it?

~~~
EduardoBautista
I have been using it for months without any issues. It is actually a bit
faster than regular vim.

