My Favorite Vim Tips

Vim IconHere are some of my favorite tips for working in Vim. I use MacVim but everything below should apply to any version of Vim you are using. Vim is such an amazingly powerful editor you can use it for years and still learn new stuff all the time. This is a small collection of features I use most often and find most helpful. Hopefully they will be helpful to you as well.

Fold any block of code

I am assuming that the foldmethod is set to manual in your .vimrc file

set foldmethod=manual

Navigate your cursor somewhere inside of the code block you want to fold, make sure you are in command/normal mode (press escape if you need to) then type zfa}

Searching Through Projects With vimgrep

Search through all the PHP files in a project.

  • Make sure you are at the root of your project in Vim (type :pwd to see) if you are in the wrong dir then type :cd /path/to/project
  • :vimgrep pattern **/*.php

Examples

Find the pattern “findMe” but do not jump to the first match.
Using the j flag prevents jumping to the first match.

:vimgrep /findMe/j **/*.php

Find the pattern “findMe” but match on case as well (case-sensitive)

:vimgrep findMeC **/*.php

Find the pattern “findMe” without regard to case (case-insensitive)

:vimgrep findMec **/*.php

To open your search results in their own buffer type :copen

Uppercase & Lowercase

Convert a visual selection to all uppercase letters.

gU

Convert to lowercase letters

gu

If you do not have a visual selection you can use movement commands to change case.

Uppercase until the end of a word

gUw

Uppercase the next 10 characters

gU1ol

Quickly expand NERDTree Column

If your file name is too long or you just need to see your NERDTree window a bit wider, put your cursor in the NERDTree window and type Shift+A

Writing Stuff That Is Not Code

I compose my blog posts in Vim and here are the settings I like to use when I am composing stuff that is not code. The following settings make text wrap nicely as you work.

set wrap
set linebreak
set spell

set wrap: Wrap the text at the edge of the window without actually introducing a linebreak

set linebreak: When a line wraps, make sure it wraps at a reasonable location rather than smack in the middle of a word.

set spell: Turn on spell checking. I do not use this all the time because I often write about code which triggers far too many spelling warnings.

You can turn all this stuff back off with the following commands:

set nowrap
set nolinebreak
set nospell

Here are the commands you need to know for moving around and doing stuff in the spell checker:

  • ]s –> move to the next mispelled word
  • [s –> move to the previous mispelled word
  • zg –> add a word to the dictionary
  • zug –> undo the addition of a word to the dictionary
  • z= –> view spelling suggestions for a mispelled word

Get Word Count

In command mode, press g ctrl-g.
This will give you the following information about the current file:

  • the current column
  • the line number
  • the word count
  • the byte count

Leave a Reply

Your email address will not be published. Required fields are marked *