
A Good Vimrc - sanjeetsuhag
http://dougblack.io/words/a-good-vimrc.html
======
Nyubis
Rebinding jk to escape because escape is too far away seemed like a fairly
ugly hack to me, especially because you can't type it anymore without waiting.

Why not simply rebind caps lock to escape? Caps lock is used awfully little,
considering how prominent its position on the keyboard is. On Linux, you can
rebind it by adding

    
    
      setxkbmap -option caps:escape
    

to your .profile. If instead of an additional escape you prefer it to be
swapped around, use

    
    
      setxkbmap -option caps:swapescape
    

This is perhaps the most impactful change on my vim workflow.

~~~
dougblack
Hi! Author here.

Fair points. I'm gonna start out on the defensive: I didn't post the article,
which kinda stinks because if I knew it was going to get such attention I
would have updated it to my current setup. Anyways as the 1.5 year old article
states:

    
    
      This article will almost certainly fall out of date with my vimrc in the very near future
    

I don't do jk escape anymore. I have since abandoned custom escapes because I
work often enough on remote servers with vanilla vimrcs that don't have custom
escape bindings set that the penalty I pay having to stop and consider which
environment I'm in before every escape wasn't worth the small quality of life
enhancement from having it set locally.

Good tip on caps lock, though I currently have it set to control which I find
is a much better use of the key. Especially once you know about ctrl-o.

~~~
Dobbs
ctrl+] will act as escape in any vim out there. At least that I've
encountered.

Having caps as ctrl makes it really easy to hit.

~~~
geofft
Ctrl-C seems to work in any vim (though, I think, not actual vi), and at some
point my muscle memory rewired itself to that. It's usually directly under my
left hand, and it's a keyboard shortcut I use elsewhere, like in the shell.

------
hoorayimhelping
I don't use vim as my go to editor so I may be asking a silly question; does
it just use idiotic defaults or something? I've only met a couple 'vim users'
who could just hop onto someone else's vim setup and be productive, and I've
never met a single vim user who could use someone else's setup without making
comments about everything being different.

I guess the question is do people modify vim cause they can or cause they have
to?

~~~
kosma
Vim's default are indeed dumb, but what you described is not a vim-specific
problem. Walk into any room full of coders and try to use their computers;
you'll discover tons of weirdness that can't be explained by anything except
by a desire to be treated like a princess.* Things like rebinding basic
shortcuts, reversing the scroll direction (Mac users will get this), swapping
Cmd/Ctrl, using some obscure shell or even infecting other people's machines
with some haphazardly scp'd dotfiles.

This kind of overcustomization is fine if you live in a basement and work on a
project alone. As soon as you become a part of a team, please stop. You're
making cooperation unnecessarily hard and spending company time on an
optimization that isn't.

*I sometimes joke that coders who refuse to conform to the industry standards should wear a tiara as an indication they are, indeed, unique snowflakes free from the usual social expectations. It seems to work.

~~~
scrollaway
> that can't be explained by anything except by a desire to be treated like a
> princess

Do you mean a desire to be more efficient?

Am I some kind of princess because I decided to use a different keyboard
layout than the default? That I decided to learn a different language than the
one I was told to learn in school? That I wanted to install a different
operating system than the one that came with my computer?

Your comment is not just hostile, it is unbelievably pretentious. You quite
clearly don't understand why people customize things. If you want to live in
your undecorated, perfectly square single-room house you're welcome to do so
without telling others they're not "part of the team" for having different
preferences than you do.

Damn...

~~~
Niksko
Realistically, what justification is there any more for working on somebody
else's machine, being force to use their config? Ok, if your machine explodes
and you need their's for a bit fine. But in the long run, the amount of times
where you need to actually edit some code by typing on another person's
machine should be vanishingly small.

I'm all for standards when it comes to collaborating on code. But when it
comes to text editors, vim configs, even using emacs (get out your crosses,
holy water and sliver bullets), it shouldn't really matter from my
perspective.

~~~
hibbelig
How do people who like to customize their environment do pair programming? I
belong to that group, and I like the idea of pair programming, but I've never
had the chance to try it in practice...

~~~
scrollaway
tmux/screen sharing work just fine.

------
exDM69
For those just getting started or using vim only occasionally, Tim Pope's vim-
sensible is a good, conservative starting point. It just sets some reasonable
base configs instead of Vim's crazy default settings. No custom keybindings or
fancy plugins included.

Vim is by default intended to be compatible with some historical
implementation of vi. In my opinion, it doesn't make any sense any more, but I
guess there is some reasoning behind the decision.

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

~~~
Svenstaro
neovim has some saner defaults, by the way.

~~~
fmoralesc
Mostly based on vim-sensible's (with some input from the community):
[https://github.com/neovim/neovim/issues/2676](https://github.com/neovim/neovim/issues/2676)

------
tinco
This is a very nice write up, but the tabs/spaces part of it makes no sense at
all.

"I like it, since it means my source code looks the same on every machine."

That reason just makes no sense at all.

There's only one reason for configuring tabs to be anything but tabs, and
that's because someone else set the standard. So if you're editing python or
bash, and you don't feel like being a rebel with a cause, you set expand tab.

But obviously you shouldn't be doing that manually every time you open a file,
so you should configure Vim to detect what's going on.

You can do that by adding the `ciaranm/detectindent` plugin and then setting
the following line:

    
    
        autocmd BufReadPost * :DetectIndent
    

To detect the indentation any time you open a file. That at least for me makes
the headache go away most of the time.

~~~
zo7
Why would either tabs or spaces be better than the other? Isn't that one of
those pedantic style debates that often come down to a matter of personal
preference? I'd get rather annoyed if someone on my team was constantly
changing the indentation from tabs to spaces or vice versa every time they
opened a file.

To elaborate on the author's reasoning for using spaces, people sometimes mix
spaces with tabs in the indentation unknowingly. If you're not using the
author's tab size on your machine, the code doesn't format well at all and if
you want to convert to spaces you'd have to manually edit a lot of things.
There's also the issue with aligning things as well, which spaces are more
consistent for as well. At least with spaces you can see the author's original
intention and then reformat it as you wish.

~~~
tinco
You're mixing up arguments. No one on your team should be changing
indentations. You should decide as a team what your settings are going to be,
that's why I suggest using DetectIndent to make sure your settings match
whatever the project guidelines are.

It's not a matter of personal preference though. Tabs are simply superior for
encoding indentation. If you use tabs for indentation you can configure their
width in your editor to match your personal preference, spaces doesn't allow
for that. That's the only significant difference, and the only good reason for
using tabs instead of spaces.

None of the arguments you made about spaces make sense. What's the fact that
sometimes people mix up tabs and spaces got to do with anything? The whole
idea of using tabs is that the code _does_ format well regardless of what tab-
width setting you have on your machine. Converting tabs to spaces _does not_
require manual intervention _at all_. Alignment should be done with spaces, no
one is argueing to use tabs for alignment. You can't use tabs for alignment
because their width is undefined! The whole idea of using tabs is semantically
encoding the authors original intention to allow you to reformat as you wish!
At this point.. are you just trolling me?

~~~
zo7
Oh sorry! I completely misunderstood what DetectIndent was doing, that's a
good tip.

I did want to point out that there's some sense to forcing the code to look
the same on every machine though. Of course converting tabs/spaces is
automatic, but my point was that the mix of the two requires either manual
editing or conforming to the width of whoever made the error. It does happen,
and I've been on at least one project with a professor who would occasionally
mix things up while in the throes of editing (and they used a tab width of 8
which made it especially irritating for others!). With spaces you at least
force whitespace to mean only one thing and if you absolutely can't stand
someone's indentation width you can still change it like you can with tabs.

This really is personal preference though. Spaces explicitly represent the
author's original intention including their indentation width, while tabs make
it more convenient to others while conflating whitespace to mean two different
things. Both are entirely reasonable though.

------
charlieegan3
Disabling the search highlight seems like a rather lowly use for leader+space.
Great guide though, didn't know about quite a few options in here, wildmenu is
great.

I switched to vim from subl about a month ago - it took a bit of getting used
to but I'm loving it now. I still use subl for some multi cursor wizardry but
that's less and less common now.

I found the disabling the arrow keys in normal mode was the point of no
return!

~~~
eru
> I found the disabling the arrow keys in normal mode was the point of no
> return!

Is there any reason to do this when you have a keyboard that has easily
reachable arrow keys? (Kinesis Advantage, in my case.)

~~~
code_sterling
Yea, it forces you to use the vim bindings. This is advantageous, since your
hands don't leave the home row. It seems minor, but when you actually "get"
vim, you'll actually see speed improvements. It sounds silly, the half second
saved here and there, but once you've done it long enough, you get into a flow
that you just can't get moving your hands to the arrows or mouse.

~~~
charlieegan3
^ this. I could likely reuse them for something now but when your muscle
memory has you going there all the time at the start it's better if they just
do nothing. I think it is at least.

------
Svenstaro
Great writeup and thank you making it clear that you want people to understand
what's happening. I love the clean style of your site.

Let me also recommend some of my favorites:

    
    
      set matchpairs+=<:>     " match < and > as well
      set ignorecase          " case-insensitive search
      set smartcase           " upper-case sensitive search
    

If you already use ctrlp, you might also cpsm [0], a better matcher for it and
ctrlp-funky [1] which allows you to go to methods and other symbols in a ctrlp
style.

    
    
      [0] https://github.com/nixprime/cpsm
      [1] https://github.com/tacahiroy/ctrlp-funky

------
dmytrish
The `ToggleNumber()` function may be shortened to

    
    
        :set invnumber
    

There two kind of prefixes for `:set` variables: `no-` and `inv-`, the first
one turns the variable off or empties it, the second just toggles between
states.

~~~
edanm
Cool trick.

FYI, you can do the same with :set number!.

Adding ! at the end of a variable will invert it. Adding ? at the end will
echo its value.

------
black_knight
Nice tutorial. While I understand the pleasure of configuring stuff — being a
Gentoo-lover I'm throwing stones in a glass house here — I always stuck to a
very simple Vimrc file. I find it better to learn how to use the standard
settings than forever me tinkering on my own. One could always wish the
standard settings were more sane, though…

------
sriram_sun
Came here to say "Don't put anything in your vimrc that you don't understand".
Pleasantly surprised when I clicked the link!

------
MichaelGG
I, too, wondered about $ and ^ - they are rather inconvenient to type. But
instead of B and E, why not map <Space> as leader, then have <Leader>b and
<Leader>e?

Space seems like the perfect leader key. Mapping , means you lose prev-finding
for some searches (f).

~~~
woogley
I actually map H to ^ and L to $. In my head it's like "h is left, H is far
left"

------
sac2171
I've played around with vimscript and my .vimrc many many times, and always
found it very unwieldy. I've started using a new and very different approach,
which involves using the amoffat/snake library to write my new .vimrc
functions in python. This is definitely not for everyone, but I've been able
to do some pretty cool stuff with it, like creating a function for writing
trace like debug statements, or sending the currently selected vim lines to a
shell via the window manager/os level, rather than a terminal multiplexer like
screen. If anyone else is interested, an example can be found at
gist.github.com/sac2171/ce7b086421e8387193fd

------
pan69

        syntax enable           " enable syntax processing
    
        The comment should be enough to describe this one.
    

Sorry, no it's not. The syntax processing of what? The syntax of the current
file loaded? Some sort of commands?

~~~
rane
A good example of a completely useless comment.

------
sandGorgon
I just realized something - my vimrc is 12 years old (carried on from my
college). It has all my quirks and shortcuts that doesnt make sense to anyone
but me. Every now and then, I pick up sone cool stuff from other vimrc and it
becomes part of my own.

What I just realized is that my Dockerfile is becoming something like that.
Everything from the way the file system is laid out to the supervisord config
is something that has become familiar. I do my development in it and its
already a year old. All my various production servers use the same base
Dockerfile (it includes Ruby, Python, Nginx, Openssl, Golang and Java!).

------
nightcracker
I don't understand why you'd want to map leader to anything but space.

~~~
weaksauce
I fully agree. I think it seems like this key that people think is special
like caps lock. The symmetry afforded by the space key and every other key on
the keyboard is phenomenal.

------
iKlsR
The first line is what I wrote about on my blog
[http://ricardolovelace.com/blog/2015/01/11/vim-is-not-a-
blac...](http://ricardolovelace.com/blog/2015/01/11/vim-is-not-a-black-box/).
Oddly enough there aren't more articles like these, just repos with a couple
hundred line long blobs with every plugin, "hack" and keybinding known to man.

------
dllthomas
One thing I've added fairly recently is:

    
    
        try
                source ./.vimlocal
        catch /^Vim\%((\a\+)\)\=:E484/
        endtry
    

which lets me tweak my settings (project specific key bindings, &c) by
dropping a .vimlocal file in the directory I'm editing from. The try-catch
ignores the error raised when the file is absent.

~~~
uniqueusername1
Why not check for file existance, instead of try-catch:

    
    
      if filereadable(expand("~/.vimlocal"))
          source ~/.vimlocal
      endif

~~~
dllthomas
I've no strong objection to that - it's racy but not in any way that probably
matters. Otherwise, it's mostly just a question of forgiveness vs
permission...

------
Niksko
Good tutorial. Looking forward to combing over my vimrc tomorrow when I have
some time. It's similarly copied from somebody else's many moons ago, and
there are things that it does that I don't really like but haven't been
bothered to change (eg. sometimes I want 2 space indents instead of 4).

------
chappi42
Nice writing. One point/one question:

. section titles can be finished with {{{1 and then no closing brackets are
necessary. It will just wrap to the next {{{1 occurence.

. regarding nvim: how can I find out what the defaults are there? Probably I
have quite a lot of definitions which are no longer needed...

~~~
devpipes
Neovim Defaults

From the current docs:

[0] [https://neovim.io/doc/user/vim_diff.html#nvim-option-
default...](https://neovim.io/doc/user/vim_diff.html#nvim-option-defaults)

From the GitHub issues page:

[1]
[https://github.com/neovim/neovim/issues/2676](https://github.com/neovim/neovim/issues/2676)

------
JD557
I've been testing your recommendation of using ag in ctrlP (let
g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'), but the performance seems
to be pretty much the same. Do you notice that much of a difference?

Disclaimer: I'm using a SSD.

~~~
octref
I'm using command-t and it works fine for me even in large code base.

AFAIK, ag shines for searching code in large code base, if you are just doing
matching with a list of file paths, ag is an overkill.

EDIT: And if you set `wildignore` correctly, you wouldn't have a lot of files.
Getting node_modules/ out of my Command-T result made it much faster. I
actually wrote a plugin[0] to extract patterns from `.gitignore` and apply to
`wildignore`, so that Command-T and CtrlP could have a shorter list to search
for.

[0]:
[https://github.com/octref/RootIgnore](https://github.com/octref/RootIgnore)

------
fantastick
Nice writeup, give Sift a chance - [https://sift-
tool.org/info.html](https://sift-tool.org/info.html) \- a nice replacement for
the silver searcher

~~~
ende42
Offtopic. But still: Thanks. Nice.

------
DiabloD3
Heh, I store my entire config in a Github repo:
[https://github.com/Diablo-D3/config](https://github.com/Diablo-D3/config)

------
welder
A must have plugin for moving to the middle of a word:

[https://github.com/bkad/CamelCaseMotion](https://github.com/bkad/CamelCaseMotion)

------
halayli
nothing special about this vimrc.

This is what I have:

[https://gist.github.com/halayli/ef3dd268986914ed77d5](https://gist.github.com/halayli/ef3dd268986914ed77d5)

At the very least, you need vim-session if you work on multiple projects (or
branches)

vim-airline is a nice to have

Ag is a must have. it allows you to search files and preview the results
directly.

I've also written a small function SwitchHeaderSource to switch between .c/cpp
files and their headers. ,s is the shortcut.

------
pyvpx
can anyone suggest similarly helpful resources for those of us stodgily using
nvi?

~~~
a_bonobo
This one's a bit older but I based most of my .vimrc on it:
[http://unlogic.co.uk/2013/02/08/vim-as-a-python-
ide/](http://unlogic.co.uk/2013/02/08/vim-as-a-python-ide/)

and

[http://www.sontek.net/blog/2011/05/07/turning_vim_into_a_mod...](http://www.sontek.net/blog/2011/05/07/turning_vim_into_a_modern_python_ide.html)

------
emanuelev
Great guide, thanks for sharing. Although that B and E remapping.. man, it
hurts!

------
deltaprotocol
I'm seriously interested in such an article for Emacs. Anyone?

~~~
curiousdude99
Check out spacemacs

~~~
deltaprotocol
I'm a relatively long time Spacemacs user, and just recently dropped it
exactly because it abstracts too much, and I wanted Emacs keybindings. I've
failed to comprehend how it answers my question. Thank you anyway.

~~~
gnuvince
A couple times a year, I'll scour a few .emacs.d repos on github and see if I
can find something interesting in there.

------
curiousdude99
My advise is to migrate to spacemacs

------
james2vegas
the best vimrc is the one that doesn't exist, so that vim runs in compat mode

------
platz
Badwolf is great

------
chanchan1987
goods

------
callesgg
what is the benefit of representing tabs by 4 spaces?

~~~
allannienhuis
one argument for spaces > tabs is that with tabs you cannot see if you have
(mistakenly) added spaces to indent a particular line (without :set list which
can be annoying to some). If someone else opens the file and their tabstop
setting was set to a different number than you, they'll see different
indentation on those lines. If everyone uses spaces, everyone will always see
the same thing.

I think tabs settings as something that should be agreed on by a team or
project lead - too easy to screw things up. Same with line ending settings in
version control if you're working with a mixed OS team.

------
ilaksh
Did he mention pathogen, vundle, youcompleteme, nerdtree, and powerline?

