7 Habits of Being Effective

When I saw first 10 minutes of 7 Habits For Effective Text Editing 2.0, by Brian Moolenaar, I immediately realized it was the best way to spent 10 minutes in the last month. It struck me I was not aware of so basic features of vim! Sure I know a lot about it, but I didn’t know fundamental commands!

The rest of the talk was not as good so I recommend to see at least the beginning of it. Maybe you will like the rest as well. But if you don’t have time, here is what I found there:

Something we do the most often. This is why it is worth to get more than familiar on this topic. Good start is to read vim’s documentation on the subject: :help search-commands.

The basic search skill is to be aware of :set hlsearch and be able to use it with a star command *. The star and its derivatives (#,g*,…) allow to find the word you have near the cursor.

Code Completion

It works out of the box! Once you write a complex method name, vim is aware of it and next time vim may help you with autocompleting it. Just start typing a few letters of the workd you have written before, then press CTRL-N and vim does autocomplete - if you are lucky. If you are not - press the shortcut a couple of times until you hit the proper name.

Other hints

As many others when I am stuck I reach for stackoverflow to find answers. And I usually do. Below are my findings.

Stats

Q: I want to see numbers of characters I have highlighted. My VIM does not do it out of the box.

A: But people say it already has such the capability and it is as simple as highlighting a block with v and typing g CTRL-g. It works great.

Multiple lines

Vim by default searches for a text which spans multiple lines. \s matches a white characters (space, tab). Adding an underscore _\s expands the match to include newline. The underscore adds a newline to any character class.

I found interesting /^abc and /abc$ match only at the beginning and the end for a line correspondingly. But ^$ loose it special meaning anywhere else. In the pattern /abc^def$xwy they are just ordinary characters.

However following patterns keep their special meaning anywhere:

\_^ matches begin of the line (zero width)

\_$ matches end of the line (zero width)

Zero width is little tricky with the above. For example:

  • /abcd\_$efgh finds nothing because the search is looking for abcdefgh where abcd is also at the end of the line (cannot be).
  • A working example is /abcd\_$\_s*efgh which finds abcd at the end-of-line followed by any whitespace or newlines then efgh.
  • /abcd\_s*\_^efgh finds abcd followed by any whitespace or newlines then efgh which begins the line.

Complex Examples

  • /abc\n\*efgh

Replacing multiple lines by a few or a single one.

Read first an article on searching across multiple lines.

References

  1. help regexp
  2. help magic
  3. help /multi
  4. vim wikipedia
  5. vim stackexchange