ViEmu
ViEmu

ViEmu Blog

A blog about all things ViEmu
Emulating
the one true editor
since 2005!

A Vim and ViEmu mapping you *really* can’t miss – never type :noh again!

Today, I noticed a mapping Tomas Lundell had posted on the ViEmu forums two months ago. Here it is in all its glory:

:nnoremap <esc> :noh<return><esc>

It’s the kind of mapping that, when you read and understand, it makes you think: “how can I not have thought of this before?”, and even, “how come this is not all over the net?”. I have added to both my _vimrc and my _viemurc, and I’m sure it will be with me for many years to come. The kind of mapping I’ll never forget. And this is from someone who <em>doesn’t</em> like to customize his editor(s) too much – my _viemurc is only5 lines, and my _vimrc is a meager 24.

What does it do? I will explain it in detail for those of you who don’t have all the background. If you don’t know what it does, then this post contains more than one valuable lesson for you.

Its function is related to vim’s “hlsearch” feature. When you search for a string in vim, either with / or ? (forward or backwards), or even with :g or :s, if hlsearch is set (“:set hlsearch”), vim will highlight all instances of the string. If the pattern was a regex, all sequences that match will be highlighted. This, as you can imagine, is very useful. It’s actually the “inspiration” (ahem) for one of the features of Codekana, which adds this functionality to Visual Studio’s native search. Bringing vim features to poor souls who depend on regular editors is a surefire way to improve their editing experience… But now on to the mapping.

Once you’ve seen all matches of the string, and you have done what you wanted to do, those highlights become an annoyance. And this is where the problem comes — the vim-standard way of removing the highlights is typing the “:nohlsearch” ex command. You can abbreviate it to “:noh”, but that’s about it. There is an alternate way: just search for a string that doesn’t appear – say, “/asdfgh”, which will remove the highlights. But this is ugly.

So the hlsearch feature is great, but it becomes an annoyance after using, and the annoyance is cumbersome to get rid of. Ouch.

Different people solve this in different ways. You can assign a keybinding to the :noh command. I tried to map it to “\”, which is unused by vi/vim’s keymappings, but couldn’t train myself to use it. Someone suggested “<alt-n>”, which is kind of nice because ‘n’ and ‘N’ are the vi motions for “find next” and “find previous”, but I just forgot to use it. I have always just kept using :noh every time. Until today.

The new mapping does magic. It maps the <Esc> key in normal mode to remove the highlighting (:noh<return>), and also to keep doing the typical function of <Esc> (by default, in normal mode, it will just beep). This is amazing. It’s reminding me of what Python programmers say about things being “pythonic” or not. This mapping is totally “vimmic” — it fits with the rest of vi/vim editing, it doesn’t remove any other functionality of vi/vim, it doesn’t even gobble up other keys which could be used as map-leaders (this is another advanced topic), and it is memorable and discoverable (I won’t forget it, because I will be reminded every time I pressEsc).

I tweeted about it (I’m very active inTwitter lately, and liking it a lot — I’m at http://twitter.com/jonbho). Several people are already retweeting it, even if I didn’t add any explanation (I will now tweet a link to this). I hope this will become popular and reach every vim user, because it’s really good and useful.

It’s shameful that I didn’t really pay the original post the proper attention then, until another person commented on it yesterday. My apologies, Tomas! And one thousand thanks!

Although this forum thread hasn’t been very active, just this has made it worthwhile that I added the sticky topic for tricks at the forum:

http://www.viemu.com/forums/viewtopic.php?pid=2456#p2456

And I hope this forum thread and the creativity of Vim and ViEmu users will generate more gold like this!

[[Update from Tomas Lundell: he wrote in with this comment:

Thanks for the kudos. However, much like everything else in the world, I didn’t come up with it. Any glory should go to Shai of Colemak (www.colemak.com), whose Vim mode for Colemak includes that mapping.

/ Tom]]

Leave a Reply


Highlights

ViEmu: vi/vim emulation for Visual Studio, Word, Outlook and SQL Server:
ViEmu
See where ViEmu customers are around the world:
Map of ViEmu customers around the world
Hear what others are saying about ViEmu:
ViEmu testimonials
Learn vi/vim easily with this cheat-sheet and tutorial:
Vi/vim Cheat Sheet and Tutorial
Read why vi/vim editing is the killerest:
Why vi/vim editing?
Discover ViEmu's sister product, Codekana:
Codekana outliner and syntax highlighter