
Coming Home to Vim - stevelosh
http://stevelosh.com/blog/2010/09/coming-home-to-vim/
======
merijnv
What annoys me about a lot of tech blog posts like this one is that they're
essentially voodoo. People like the author write instructions about what you
should do without even understanding the instructions themselves.

    
    
        "I’m not entirely sure what the filetype lines do. I’ve read that they’re necessary and so far I haven’t had problems."
    

His example .vimrc has filetype listed twice, which is redundant. This in
itself isn't a big deal, but the problem these lines illustrate is.

Sentences like these are the reason for billions of blog posts about setting
up an OpenSSL certificate authority all repeating the same example which the
documentation explicitly mentions is the wrong way of doing things.

In fact I find that these voodoo instructions often crowd out any actual
_proper_ tech guides in the search results, all for the sake of drawing
traffic to some blog...

Don't write tech guides if you can't be bothered to do the research and
explain to your readers why they should do what you do and _what_ exactly
they're doing!

~~~
andymorris
I'm not a vim guru by any stretch of the imagination, but I still don't take
what he says at face value. I don't trust ANYTHING I see on the internet just
because it's there!

Your criticism seems to be due to the belief that, in a world where the only
information on the internet could be treated as gospel, that would allow the
experts opinion to come through easily. Unfortunately, that sets up the
circumstance where the experts get granted much more exposure and trust than
they necessarily deserve, and it inevitably gets subverted and abused.

I personally prefer the current system - treat all information as suspect
until you can verify it. It leaves MUCH less room for exploitation than all of
our other "trust by default" mediums.

\-- Ayjay on Fedang

~~~
merijnv
Oh, I agree that not trusting information on the internet is the sensible
default. However, there are plenty of people who will be copying his examples
directly without any investigation.

Some will probably post the exact same stuff on their own blogs or link to
this post. Before you know it this example will drown out any serious search
results for "vim filetype", effectively stopping new people from learning how
to do things properly.

Now obviously this post is a bit hyperbole in this instance. But these things
do happen because people can't be arsed to treat the things they write/publish
with the care it deserves.

------
samdk
One of the things I love about Vim is that every time I read though a post
like this or look at someone's .vimrc I learn something new about what it can
do or get another insight about how its features can make my life easier.

My (semi-commented) .vimrc is on GitHub if anyone wants another example:
<http://github.com/samdk/vimconf/blob/master/dotvimrc>

I've also found StackOverflow's list of most-voted questions tagged _vim_ to
be a very useful source of little tidbits:
[http://stackoverflow.com/questions/tagged/vim?sort=votes&...](http://stackoverflow.com/questions/tagged/vim?sort=votes&pagesize=15)

And there've been several HN .vimrc posts that I also found very useful. This
is one of the best: <http://news.ycombinator.com/item?id=856051>

This is another excellently commented and extensive .vimrc that (I think) is
mentioned in one of the above links, but deserves its own specific mention
here: <http://www.vi-improved.org/vimrc.php>

And this is another I found completely by accident a while ago that includes
some excellent examples of vimscript macros: <http://fingelrest.silent-
blade.org/uploads/Main/vimrc.html>

~~~
stevelosh
Exactly.

Case in point: I just found out about ending the backupdir with two slashes to
include the full path in the filename by reading your .vimrc. Thanks!

~~~
stevelosh
Replying to myself because I can't seem to reply to sigzero:

Yeah, I definitely need to get around to reading :h filetype.

I mentioned Snipmate in the article, so I know about it :)

~~~
samdk
This is not vim-related, but the reason you couldn't reply is that HN doesn't
let you reply to a comment until after a certain amount of time has passed.
This amount of time depends on how deep the comment is in the tree. (I believe
the restriction is there to help prevent excessively deep comment trees that
are just people arguing back and forth.)

~~~
stevelosh
Cool, thanks!

It would be helpful if it mentioned that somewhere...

~~~
stevelosh
Addendum: ugh, it's not even in the damn FAQ.

~~~
paulbaumgart
It was announced here: <http://ycombinator.com/newsnews.html>

------
CGamesPlay
> _Some people use a mapping that turns search highlighting off and on, but I
> don’t like that solution. It means I have to remember to turn it back on
> later once I’ve probably already forgotten about the original search._

:nohlsearch removes highlighting, doesn't modify search history, and also
doesn't _disable_ highlighted search. It will still highlight matches for the
next search.

    
    
      > nnoremap <leader>S ?{<CR>jV/^\s*\}?$<CR>k:sort<CR>:let @/=''<CR> 
    

I'm curious why not just use `ViB:sort`?

> _When I press return, create a new line at the same indentation level as the
> current one. Don’t try to be clever and adjust the indent of new line in any
> fashion._
    
    
      set autoindent nosmartindent nocindent
    

Wrap it in a filetype line if you like.

~~~
stevelosh
> _I'm curious why not just use `ViB:sort`?_

Because we use LessCSS[1] which lets you nest selectors:

    
    
        body {
            color: #151515;
            background-color: white;
            
            a {
                color: red;
            }
        }
    

When I sort the "body" properties I don't want it to break up the stuff nested
under it.

I'll give the indent settings a try, thanks!

[1]: <http://lesscss.org/>

------
mhansen
For a free, cross-platform (and excellent) alternative to PeepOpen, try
Command-T

I used to use FuzzyFinder_TextMate, but that's stopped development, and the
creator recommends you use Command-T instead.

<https://wincent.com/products/command-t>

~~~
pyre
LustyExplorer is good. One feature that I really like is that I can start
exploring from the current directory or the directory of the current file.
This way I can keep vim's current directory at the root of my source tree, and
not have to deal with turning on 'acd' and all of the incompatibilities that
can arise.

~~~
cosmok
+1 for LustyExplorer. Use that every day at work and at home.

------
njharman
I dont see the appeal of nerdtree and the like. For me they are clumsy and too
much information.

I'm very adept at unix cmd line, find, ack, locate, CDPATH, cmd history, and
friends. I spend 10-20% of my editing time at the bash prompt. So much so i
aliased :e to vim

Having never got the "gui" file managemnet tools i cant say for certain but i
wonder if power and productivity gains one sees from truly learning vim can be
reproduced by truly learning cmd line.

~~~
lenni
Could you please share how exactly you do the "jump to file foo.bar"
functionality? That is the only part of vim I still find a bit laborious,
probably because I haven't found a good workflow for it.

~~~
jb55
:b has smart tab completion. You can type any part of the filename and you can
tab through the results. This is generally what I find myself doing when I
have a lot of buffers open. Otherwise I just use minibufexpl with buffer
next/prev keybinds.

~~~
lenni
But what about files that you haven't already opened?

~~~
xenomachina
:e also has tab completion.

I also use ctags. Usually I want to look things up by symbol name, not
filename, though it's trivially easy to make a script that generates a tags
file of just filenames too. (And :tag also supports tab completion.)

------
ptn
A little off-topic: I love how in the side panel of the page the sub-title
fades in and out as you scroll.

~~~
cracki
that's proportional to how much of the actual headline is still visible.
scroll slowly and you'll notice.

------
ollysb
Entirely agree on the aesthetics, one of the things holding me back from
switching is that I love vibrant ink in textmate. I just can't seem to find a
theme that is quite as bright (vividchalk was pretty close). Any suggestions?

~~~
davidmathers
Convert your textmate theme to vim: <http://coloration.sickill.net/>

~~~
ollysb
Thanks! macvim here I come :)

edit: Many of the most popular textmate schemes have already been converted
and can be found at <http://github.com/squil/vim_colors>

~~~
nagnatron
Some are even "improved". I love molokai
<http://winterdom.com/2008/08/molokaiforvim> .

~~~
calloc
The issue I had with Molokai (and it may be my screen) is that the comments
were barely readable. The colours were just too close to the background.

My favourite is earendel
(<http://www.vim.org/scripts/script.php?script_id=2188>)

------
irahul
Since the blog isn't taking comments, posting it here.

> doesn't work very well with is Python, because it preserves whitespace when
> it moves the code over the REPL.

If that's the only issue, it's easy to fix it. All you need to do is to
replace multiple new lines with single new line.

    
    
        let s:foo_text = substitute(a:text, '\n\s*\n\+', '\n', 'g') . "\n" 
    

Here is my slightly modified slime.vim: <http://gist.github.com/589934>

I have been using it on Ubuntu with vim and IPython and it seems to work fine.
When I started using slime.vim for Python, I ran into whitespace issue and
Jonathan(slime.vim developer) resolved it when I asked him on his blog.
[http://technotales.wordpress.com/2007/10/03/like-slime-
for-v...](http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/)

You can search for Rahul in the comments.

------
astrofinch
After a few months of vim use, I did part of the vim tutorial using both vim
and a click-and-type editor (gedit) and timed myself. Gedit was
_significantly_ faster. Can't say I understand you vim fanatics.

~~~
stevelosh
If you say _why_ Gedit was faster perhaps us vim fanatics can help you
understand us.

~~~
astrofinch
Let's say I need to change a word. With Gedit, I double-click the word and
retype it the right way. With Vim, I press j until I'm on the line where the
word is, then I press w until I'm at the beginning of the word, then I press
cw to change the word, retype it the right way, and press escape to go back to
command mode.

Triple-clicking in Gedit highlights a line. Triple-clicking and dragging
highlights a contiguous group of lines. In Vim, I'd need to move to the line I
want to begin my selection on ([number]G or jjj), then press V, then press jjj
until everything is selected. And I don't have to deal with the fact that the
default way of deleting things overwrites the default clipboard either.

I do use Vim when I need to process lines in a text document (with macros),
when I'm editing files on a server, or when I'm writing a git commit message.
I suppose I could use Vim in much the same way as Gedit through its mouse
capability, but I dislike the lack of interface between my system clipboard
and Vim's, and the fact that when I paste code in to a Vim document, the
indentation gets all screwed up.

~~~
stevelosh
"With Vim, I press j until I'm on the line where the word is, then I press w
until I'm at the beginning of the word, then I press cw to change the word,
retype it the right way, and press escape to go back to command mode."

OK, this is something Vim people need to work on. Using j/k to move more than
one or two lines is the wrong way to do things.

I know, I know, we extoll the virtues of hjkl constantly, but really they're
_barely_ more effective than the arrow keys.

There are better ways to move around in Vim, such as [f]orward, '[t]il and
searching. In your case (changing word XYZ to something else) I would search
for the word with /XY… and press return to get there, then edit as you
described.

"Triple-clicking in Gedit highlights a line. Triple-clicking and dragging
highlights a contiguous group of lines. In Vim, I'd need to move to the line I
want to begin my selection on ([number]G or jjj), then press V, then press jjj
until everything is selected."

See my previous comment about movement. You'd move to the line you want by
using a search (or '{', which moves to empty lines), start your selection with
V, and move to the end with another search (or some kind of movement command
that gets you where you want to go quickly).

We Vim users need to stop touting our Nethack-honed hjkl skills and start
talking about Vim's better movement commands.

"And I don't have to deal with the fact that the default way of deleting
things overwrites the default clipboard either."

Yeah, this is dumb. YankRing helps a lot by letting you 'Ctrl+P' to the right
text, but I really think Vim should have a "clipboard register" that only gets
overwritten when you mean it (and have other registers for the last yanked
elements).

"when I paste code in to a Vim document, the indentation gets all screwed up."

If you're using a graphical Vim like gvim or MacVim this simply shouldn't
happen. It's a bug and you could report it.

If you're using the command line Vim you might need to do a bit more work to
launch Vim and tell it to talk to X. It's a few extra minutes up front, but
such is the price of using a terminal-based editor in a graphical environment.

~~~
astrofinch
"There are better ways to move around in Vim, such as [f]orward, '[t]il and
searching. In your case (changing word XYZ to something else) I would search
for the word with /XY… and press return to get there, then edit as you
described."

That is both slower and has the disadvantage that any intermediary occurrences
of the word between my cursor and the one I want to edit will be accessed
first.

"See my previous comment about movement. You'd move to the line you want by
using a search (or '{', which moves to empty lines), start your selection with
V, and move to the end with another search (or some kind of movement command
that gets you where you want to go quickly)."

I invented the following task for myself: With the cursor on a blank line
above 4 lines of text followed by a blank line, copy the 4 lines and paste
them so that there is a buffer of 2 blank lines between the 4 lines of text
and their duplicates. I did the task 4 times in both gedit and vim, restarting
whenever I screwed up (which was quite often). Here are my results.

vim: 18.28s gedit: 14.45s

I discarded an earlier task that I think Vim might have been doing better on
because the task ended with the buffer back where it started, and I wasn't
sure I was able to count accurately while performing the task. However, I had
practiced with Vim on that task to the point where my movements were purely
mechanical, and I was typing characters nearly as fast as I'm typing these
words now.

It's probably a better idea to relax while doing these tasks instead of racing
through them, as I'm generally relaxed when I code, but I still don't see much
promise in Vim at this point.

As a side note, why is it that I'm the only one doing tests on myself? It's
almost as though you guys don't actually want to know which editor is faster.

~~~
Ivatar
I tried your test, and was slightly faster at doing it vim than gedit
(assuming my fingers were on the keyboard to begin with). How exactly did you
do it in vim? I used the following:

y4j5jp -- yank down to 4 lines, move down to the blank line, paste after it.

I appreciate that there's an awful lot to learn before you can edit quickly in
vim, and that the difference (if any) between that and gedit(or any other
editor) may not be worth it.

I prefer vim for reasons other than speed, though -- the composable commands
"feel" right to me, and the macro system makes short work of repetitive tasks.
I'm essentially making lots of small programs to write the larger program for
me.

~~~
astrofinch
I did V}y}p.

------
adorton
This is an excellent vim tutorial. Unlike others I've read, it makes me want
to be vim power user by clearly spelling out the benefits of various tricks
and commands.

------
njharman

      "Many people like to remove any extra whitespace from the
      " ends of lines. Here is one way to do it automatically
      " when saving file.
      fun! <SID>StripTrailingWhitespaces()
        let l = line(".")
        let c = col(".")
        %s/\s\+$//e
        call cursor(l, c)
      endfun
      autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
    
      " When editing a file, always jump to the last known cursor position.
      au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
    
      " This function determines, wether we are on the start of the line text (then tab indents)
      " or if we want to try autocompletion
      function InsertTabWrapper()
        let col = col('.') - 1
        if !col || getline('.')[col - 1] !~ '\k'
            return "\<tab>"
        else
            return "\<c-p>"
        endif
      endfunction
      " Remap the tab key to select action with InsertTabWrapper
      inoremap <tab> <c-r>=InsertTabWrapper()<cr>

------
julianz
Great read. Among other things it made me dig deep into the heart of my Vista
machine and find whatever font was making your site (and others) utterly
unreadable. The answer is shitty Palatino fonts installed by HP, presumably as
part of a printer driver.

So there you go, if Steve's site makes your eyes bleed then go and kill the HP
Palatino fonts on your machine.

~~~
stevelosh
Hmm, perhaps I should change the font stack.

Is your shitty HP font named "Palatino" or "Palatino Linotype"?

If it's "Palatino" I'll need to insert a Windows-only font at the front of the
list.

If it's "Palatino Linotype" I'll need to insert a Windows-only font as the
second element in the list.

~~~
julianz
Hi Steve - it's "Palatino". I also have "Palatino Linotype" and it looks fine
now that I've knocked off Palatino.

It's a problem I've come across previously on Windows, I think Sun's Java was
also installing some horrible version of Palatino a while back. Pity, as it's
a wonderful typeface and looks nice on your site.

------
swannodette
On a side note, the Gundo feature (<http://stevelosh.com/blog/2010/09/coming-
home-to-vim/#gundo>) is available for Emacs
(<http://briancarper.net/blog/568/emacs-undo-trees>).

------
SpookyET
I am quite proud of my vimrc. It took me a while to build it. By default, it's
just a text editor without settings required for development. Use dev to
enable devmode and nodev to go back to normal text editing mode.

<http://github.com/SpookyET/dotfiles/blob/master/vimrc>

------
sabalaba
If anybody is curious, or just wanted to steal his .vimrc (like myself), here
it is [I'm surprised he didn't provide it at the end of the article...]

<http://paste.pocoo.org/show/265338/>

~~~
stevelosh
Here's the BitBucket link, which will stay current as I push more updates in
the future: <http://bitbucket.org/sjl/dotfiles/src/tip/vim/.vimrc>

I did link it in the "Important .vimrc Lines" section, but perhaps I should
add it at the bottom as well.

------
sunny36
A litte off topic. Much that I like vim I haven't found any plugin than can
indent Javascript properly. To be honest whenever I write Javascript I just
open up emacs. Is there something close to js-mode in vim?

~~~
sharkbrainguy
(ITYM js2-mode) No.

I use this to indent my JS:
<http://www.vim.org/scripts/script.php?script_id=1936>

You could use JSLint plugin to get more of the features, but I've never gotten
it to feel like js2-mode.

------
barfoomoo
A bit off the topic but a couple of doubts regarding pathogen. I installed
pathogen and am a bit confused regarding the organisation of the plugins
within pathogen. Say I have plugin called foo.vim, inside the bundles folder
do I directly drop it or do I create a folder called foo and then drop foo.vim
within it? Also say a plugin has syntax files and an ftplugin folder then do I
just drop the plugin in pathogen and it is supposed to work? Is there any good
doc out there explaining how to configure with pathogen?

~~~
mattikus
"Say I have plugin called foo.vim, inside the bundles folder do I directly
drop it or do I create a folder called foo and then drop foo.vim within it?

You need to create the folder foo with a folder called plugin and then foo.vim
inside it -> foo/plugin/foo.vim

" Also say a plugin has syntax files and an ftplugin folder then do I just
drop the plugin in pathogen and it is supposed to work?

Yep. Normally vim plugins are shipped in zip files that contain the structure
and are meant to be installed on top of your .vim directory. All you do is
instead install them into .bundles/pluginName instead. Also, since a lot of
vim plugins are kept in source control anyways, a lot on github, you can
simply use git submodules to version your plugins as well making updating them
a cinch.

~~~
barfoomoo
Thank you.

------
BoppreH
I can't get snipMate to work. Downloaded a brand-new gVim (windows 64, btw)
dropped a pathogen.vim file straight from github into my vimfiles/autoload
folder and copied the snipMate's files to vimfiles/bundle/snipMate.vim

I'm using the article's _vimrc.

:scriptnames lists the "plugin/snipMate.vim" files from both the
"snipmate.vim/" and "snipmate.vim/after"

But when I type some snippet (like a "for" in a *.c file) and press tab, a
single space (not a tab) is inserted. Pressing tabs on non-snippets inserts a
tab like expected.

Am I missing something here?

------
wccrawford
I disagree with everything that he starts with 'I almost never do X so I...'

But other than that, I think this is a great list of addons and I already use
a few of them myself.

~~~
njharman
yeah i rely on replacing first pattern all the time!

------
tommynazareth
TextMate doesn't have split windows?!

~~~
compay
Nope. It's one of the biggest complaints about Textmate, and one of the things
that led me to (mostly) switch to vim.

A lot of people are pretty upset with the author because rather than choosing
to release an incremental update that adds smaller features like this, he
decided on a total rewrite and got bogged down with no release in sight.

That said, Textmate still feels remarkably current despite the fact that its
last major update was more than 4 years ago.

------
grayrest
Couple tricks:

    
    
        " this splits a netrw window in the directory of the current file, :Vexplore for vsplit.
        nnoremap - :Sexplore<CR>
    
        " I use surround.vim all the time. The default mapping for s is terrible.
        nmap s ysi
        nmap S ysa

------
picklestime
Interesting article. I found it a bit strange that he didn't link to or
reference this: <http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim>

which shares the exact same title.

~~~
stevelosh
Yeah, I didn't realize that post had the same title, even though I read it and
it's a fantastic post.

I'll add a link to it now.

------
dschobel
I have nothing constructive to add beyond-- thank you Steve, what a
fantastically informative post. It clearly took a lot of time and effort to
put together and it is very much appreciated.

------
boskone
Don't lose another four years before blogging about how you finally switched
to emacs from vim and bite the bullet now. Emacs is to vim as vim is to
textmate IF you are a hard core software developer. Vim is best for sysadmins,
simple, quick non-complex editing.

I know, I know... But when you were newly in "love" with textmate I'm sure if
I said you need to switch to vim you'd say no way. Skip vim and go right to
the best there is, Emacs.

~~~
silentbicycle
...and once your fingers are raw, bloody stumps from typing with three
modifier keys held down, then you can write about coming home to vim again!
Bonus!

Seriously, can we not start yet another Emacs vs. vi argument? Neither is
perfect, they both have ardent fans, pick either, get really proficient with
it, and don't look back.

(Emacs user who loves Emacs's extensibility and multi-buffer design but
prefers vi's modal keyboard ui. And yes, I know about viper, etc.)

~~~
oddthink
Have you noticed that all of the recent press seems to be for vim? It seems
like every month there's a new "I switched to vim!" post, but never a similar
emacs version, or even a mention of emacs in the switch article.

Note: I am not attempting to argue emacs vs. vim.

~~~
silentbicycle
If I'm not mistaken, they're usually switching to vim specifically _from
textmate_. The Mac + Ruby community seems to be disproportionately well-
represented in blogs, probably due to a high concentration of web developers.

Some high-profile Ruby people (e.g. Yehuda Katz) wrote about switching to vim
about two months ago ([http://yehudakatz.com/2010/07/29/everyone-who-tried-to-
convi...](http://yehudakatz.com/2010/07/29/everyone-who-tried-to-convince-me-
to-use-vim-was-wrong/)), and it probably set off a chain reaction.

------
nagnatron
I was getting a bit tired of seeing/reading a blog post by a random convert to
vim/emacs every week but this one is very good.

------
kaffiene
I feel the same way about Emacs. I use IDEs for specific development tasks (I
don't generally code Java in Emacs, for example, since Netbeans, Eclipse, Idea
are all much better in that context) but where content is all over the place
as it often is with net development, I use Emacs.

------
kevincolyar
If you're a vim user on a mac, try ViKing. It lets you use vi-like navigation
throughout OSX. It's still in beta and I would love some feedback.

<http://vikingapp.com> @vikingapp

~~~
steve19
The kevincolyar account does nothing but spam HN with links to his
application. I am sick of seeing him post spam each time a VIM article hits
the homepage.

~~~
stevelosh
I'm the author and submitter of the post and for what it's worth I don't mind
his comment at all.

It's relevant because OS X Vim users would probably like to use Vim movement
everywhere. I know I would.

It's certainly more relevant than the "use emacs lol" comments that inevitably
show up.

~~~
mikecaron
Yeah, the whole vim/emacs war is so 1995.

