Advertisement
General

Top 10 Pitfalls When Switching to Vim

by

Admit it: every time you see a person using Vim, you consider the possibility that they know something you don't. Why else would they be using an editor that, in your mind, is dated, open-source junk? Think what you wish, but there's a reason why top developers swear by Vim.

Republished Tutorial

Every few weeks, we revisit some of our reader's favorite posts from throughout the history of the site. This tutorial was first published in February 2011.

Until you've spent at least a month working every day with the editor, you'll undoubtedly hate it! This is specifically why the majority of newcomers will play around with Vim for a day, become disgusted, and never touch it again. What's unfortunate is that, if these developers could get beyond the pitfalls, they'd be introduced to incredible speed and flexibility.


1. There's Too Many Modes

It's true: Vim is not your standard code editor (it's better). Transitioning from, say, TextMate to Vim is going to be an entirely different process than switching from TextMate to Espresso. Always keep that in mind when you find yourself tearing our your hair because Vim seemingly refuses to enter text when you type.

Though there are multiple modes in Vim, we'll focus on the three most important. But before we continue, note that each key serves a different function, dependent upon which mode you're currently in. Even more confusing -- at first -- a capital letter triggers a different action than a lowercase.

In command mode, pressing the lowercase "i" will trigger "Insert Mode." However, an uppercase "I" will move the cursor to the beginning of the line. This might sound confusing, but it provides you will incredible power!

  • Command: By default, Vim should rest in command mode. Think of this mode as the "getting around" mode. While, in a traditional editor, you might be accustomed to using the mouse or the arrow keys to traverse your document, Vim's command mode makes the process quicker and "mouse-less."
  • Insert: Pressing the lowercase "i" in command mode will switch you into "Insert Mode." Newcomers to Vim will find this mode to be familiar. As a result, though, they often remain in this mode far longer than they should (I know I did). As a rule of thumb, insert mode should purely be used for the sole purpose of inserting text. When finished, immediately return to command mode.
  • Visual: Think of visual mode as "selection" mode. Need to select the next five lines and delete them? With a normal code editor, you could use the mouse to visually select five lines, and then press the backspace key. With Vim, on the other hand, you'd press capital V to switch to Visual mode, and then type 5j to select five lines down. Finally, you'd press d, for delete.

I know this sounds incredibly confusing at first. You might think to yourself, "All that work just to delete five lines?" The truth is, though, this method is significantly faster.


2. Ancient Editor

Why would you turn your nose up at over three decades of development?

You might hear your friends say, "Isn't Vim an ancient code editor?" Well, you could say that; it's been around for over thirty years. Though honestly, why would you turn your nose up at over three decades of development? That's longer than I've been alive! Better yet, even to this day, Vim is under active development. The most recent release, 7.3, was made available in August, 2010.

Secondly, it's important to keep in mind that Vim is not Vi. If your only experience is with the latter, take some time to download the latest release and toy around with the new features. You'll be pleasantly surprised!


3. I Love TextMate Snippets

If Vim can't natively do it, you can bet that there's a plugin available somewhere!

You'll quickly find that, if Vim can't perform a particular task, then it's likely that a plugin has developed to provide that functionality. For instance, consider TextMate's excellent snippets feature. While Vim doesn't support this natively, you can download the snipMate plugin, which should make the experience virtually identical to what you're used to.

Learn More About Switching to Vim From TextMate


4. I Can't Use the Arrow Keys

The less movement, the better.

Firstly, this isn't true. It might have been the case with Vi, but you're free to use how Vim in the way that feels most comfortable to you. So use the arrow keys to your heart's content -- though keep in mind that there's a reason why most Vim users don't.

The h,j,k,l keys mapping to left, down, up, and right, respectively, serve two purposes:

  • No Choice: Back in the day, machines didn't have those helpful arrow keys. As such, they had little choice but to choose the best alternative.
  • Less Movement: If your hands generally rest along the second row of the keyboard, it makes little sense to repeatedly move your hand to the lower-right portion of the keyboard every time you want to move the cursor. The less movement, the better. With this arrangement, you can traverse your documents without moving an inch.

When all is said and done, you are the person using the editor. If, at first, you feel more comfortable using the arrow keys, then by all means do!


5. I'm a Designer, Dude

That's okay! Vim is not for everybody. As a person who at least attempts to do design work from time to time, I can fully attest that Vim may not be the best fit for designers.

Developers are not designers; it's only natural that this fact will be reflected in their choice of editors.

If you find that the bulk of your days are spent working with HTML and CSS, then maybe Vim is not for you. Now, that's not to say that you shouldn't give it a shot; but it's certainly understandable, should you decide to stick with a more designer-friendly editor, such as Coda.


6. Vim Offers Nothing My Current Editor Doesn't Already Do

Plain and simple, that's rubbish. Certainly, every editor does have its strong points, but you'll find that Vim is incredibly powerful, and, more importantly, flexible. There are hundreds upon hundreds of plugins available (for free) that will provide support for virtually any kind of functionality that you require.

Many newcomers often cite the built-in "Change Inner" command as a huge selling point. It certainly was for me! Let's say that you have the following piece of code:

var joe = 'plumber';

Assuming that the cursor is at the beginning of that line, and you wish to change the value "plumber" to "black," traditionally, you might use the arrow keys or the mouse to select and change the text. With Vim, the process is hugely simplified. Simply type: ci'. This stands for "Change Inner Quotes," or, find the next set of single quotes, and change the value inside.

Kick-Ass Plugins

  • snipMate: Allows you to, for instance, type
    + tab, and have a full div element expanded. It’s tremendously helpful.
  • Surround: Wrap portions of text with parens, tags, braces, etc.
  • NerdTree: Explore your filesystem and to open files and directories. It presents the filesystem to you in the form of a tree which you manipulate with the keyboard and/or mouse. It also allows you to perform simple filesystem operations.
  • TComment: Easily and quickly comment certain lines of your code.
  • Sparkup: Similar to ZenCoding, but provide more support for applying values to elements as well, such as: ul > li { My list item text. }.

7. My Vimrc File is Blank

This was an initial gripe that I had with Vim, as well. When first launching, say, MacVim, you're thrown into the wolf-pack! No code highlighting, no formatting, no smart indenting... no nothing! Particularly if you're using a custom Vim editor, there should at least be a base vimrc file to get you started. It can be an intimidating experience trying to figure out how to apply your custom preferences.

For those unfamiliar with a vimrc file, it's essentially a file that allows you to specify your editor preferences.

Use this as a starter (click the Expand button below):

" .vimrc File
" Maintained by: Jeffrey Way
" jeffrey@jeffrey-way.com
" http://net.tutsplus.com
"

"Forget compatibility with Vi. Who cares.
set nocompatible

"Enable filetypes
filetype on
filetype plugin on
filetype indent on
syntax on

"Write the old file out when switching between files.
set autowrite

"Display current cursor position in lower right corner.
set ruler

"Want a different map leader than \
"set mapleader = ",";

"Ever notice a slight lag after typing the leader key + command? This lowers
"the timeout.
set timeoutlen=500

"Switch between buffers without saving
set hidden

"Set the color scheme. Change this to your preference. 
"Here's 100 to choose from: http://www.vim.org/scripts/script.php?script_id=625
colorscheme twilight

"Set font type and size. Depends on the resolution. Larger screens, prefer h20
set guifont=Menlo:h14

"Tab stuff
set tabstop=3
set shiftwidth=3
set softtabstop=3
set expandtab

"Show command in bottom right portion of the screen
set showcmd

"Show lines numbers
set number

"Prefer relative line numbering?
"set relativenumber"

"Indent stuff
set smartindent
set autoindent

"Always show the status line
set laststatus=2

"Prefer a slightly higher line height
set linespace=3

"Better line wrapping 
set wrap
set textwidth=79
set formatoptions=qrn1

"Set incremental searching"
set incsearch

"Highlight searching
set hlsearch

" case insensitive search
set ignorecase
set smartcase

"Hide MacVim toolbar by default
set go-=T

"Hard-wrap paragraphs of text
nnoremap <leader>q gqip

"Enable code folding
set foldenable

"Hide mouse when typing
set mousehide

"Shortcut to fold tags with leader (usually \) + ft
nnoremap <leader>ft Vatzf

" Create dictionary for custom expansions
set dictionary+=/Users/jeff_way/.vim/dict.txt

"Opens a vertical split and switches over (\v)
nnoremap <leader>v <C-w>v<C-w>l

"Split windows below the current window.
set splitbelow              

" session settings
set sessionoptions=resize,winpos,winsize,buffers,tabpages,folds,curdir,help

"Set up an HTML5 template for all new .html files
"autocmd BufNewFile * silent! 0r $VIMHOME/templates/%:e.tpl

"Load the current buffer in Firefox - Mac specific.
abbrev ff :! open -a firefox.app %:p<cr>

"Map a change directory to the desktop - Mac specific
nmap <leader>d :cd ~/Desktop<cr>:e.<cr>

"Shortcut for editing  vimrc file in a new tab
nmap <leader>ev :tabedit $MYVIMRC<cr>

"Change zen coding plugin expansion key to shift + e
let g:user_zen_expandabbr_key = '<C-e>'

"Faster shortcut for commenting. Requires T-Comment plugin
map <leader>c <c-_><c-_>

"Saves time; maps the spacebar to colon
nmap <space> :

"Automatically change current directory to that of the file in the buffer
autocmd BufEnter * cd %:p:h

"Map code completion to , + tab
imap <leader><tab> <C-x><C-o>

" More useful command-line completion
set wildmenu

"Auto-completion menu
set wildmode=list:longest

"http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
set completeopt=longest,menuone
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
  \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
  \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

"Map escape key to jj -- much faster
imap jj <esc>

"Delete all buffers (via Derek Wyatt)
nmap <silent> ,da :exec "1," . bufnr('$') . "bd"<cr>

"Bubble single lines (kicks butt)
"http://vimcasts.org/episodes/bubbling-text/
nmap <C-Up> ddkP
nmap <C-Down> ddp

"Bubble multiple lines
vmap <C-Up> xkP`[V`]
vmap <C-Down> xp`[V`]

" Source the vimrc file after saving it. This way, you don't have to reload Vim to see the changes.
if has("autocmd")
 augroup myvimrchooks
  au!
  autocmd bufwritepost .vimrc source ~/.vimrc
 augroup END
endif

" easier window navigation
nmap <C-h> <C-w>h
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-l> <C-w>l

"------------------------"
"NERDTREE PLUGIN SETTINGS
"------------------------"
"Shortcut for NERDTreeToggle
nmap <leader>nt :NERDTreeToggle <CR>

"Show hidden files in NerdTree
let NERDTreeShowHidden=1

"autopen NERDTree and focus cursor in new document
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p

"Helpeful abbreviations
iab lorem Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
iab llorem Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

"Spelling corrects. Just for example. Add yours below.
iab teh the
iab Teh The

" Get to home dir easier
" <leader>hm is easier to type than :cd ~
nmap <leader>hm :cd ~/ <CR>

" Alphabetically sort CSS properties in file with :SortCSS
:command! SortCSS :g#\({\n\)\@<=#.,/}/sort

" Shortcut to opening a virtual split to right of current pane
" Makes more sense than opening to the left
nmap <leader>bv :bel vsp

" Saves file when Vim window loses focus
au FocusLost * :wa

" Backups
set backupdir=~/.vim/tmp/backup// " backups
set directory=~/.vim/tmp/swap// " swap files
set backup " enable backup

" No more stretching for navigating files
"noremap h ;
"noremap j h
"noremap k gj
"noremap l gk
"noremap ; l

set showmatch " show matching brackets

" print empty <a> tag
map! ;h <a href=""></a><ESC>5hi

8. I Don't Want to Use the Terminal

Me neither -- at least not more than I have too -- there are a variety of dedicated Vim editors which provide a more Windows/Mac GUI-like experience.

These editors will provide support for the sorts of keystrokes that are ingrained into your body, such as "Command + F" to search, or "Command + W" to close the current window.


Remember how, in school, sometimes, the student sitting next to you was able to explain and help you understand some difficult concept more than the teacher? The reason is because, once you’re mastered a craft, it’s difficult to recall what personally gave you the most trouble. From September to October, I embarked on a four-week challenge, which I call “Venturing into Vim.” After hearing countless extremely talented developers praise this seemingly decade-old code editor, I decided that it was worth a month of my time to figure out why so many people consider Vim to be the best editor on the planet.


10. I Can't Edit Files on My Server

Of course you can, though, admittedly, it's not quite as user-friendly as, say, Coda's remote server feature. Speaking of Panic, if you're a Transmit user, you might consider installing the Transmit FTP plugin.

"This script allows you to upload the current file via Transmit directly from Vim. For it to work, you need to be working on a file that's tied to a Transmit connection, and this connection must have "DockSend" enabled."

With this plugin, when editing a file that has a Transmit connection (open file via Transmit), you only need to press Control + U to push those updates back to your remote server. It's a cinch!

Truthfully, though, you should try to adopt a better build/deployment process. This way, rather than using FTP, you can simply git push.


Sure - there are a handful of reasons not to use Vim. It has a steep learning curve, and requires a complete rethinking of how a code editor should function. That said, there are hundreds of reason why you should use Vim. The only question is: why aren't you?

Related Posts
  • Computer Skills
    OS X
    Introducing Markdown on OS XMarkdown
    Markdown is a popular formatting syntax that lets you easily write plain text that can be converted into a number of other formats; mainly HTML. In this tutorial I will show you how to use the Markdown syntax on OS X.Read More…
  • Computer Skills
    App Training
    Getting Spiffy With PowerlinePowerline
    That line at the bottom of your editor and the prompt for your favorite shell have one thing in common: information! But, it's not always clear, and it's often hard to change. It's much easier now than ever before to do just that with Powerline. I'll show you how easy it is to take charge of the status line and make it into what you want it to be.Read More…
  • Design & Illustration
    Photoshop
    Create a 3D, Fruit-Textured, Text EffectOrange text preview 400x277
    Use a combination of Modo, Photoshop, and Illustrator to create a text effect made of oranges.Read More…
  • Code
    Web Development
    Using Polymer to Create Web ComponentsPolymer wide retina preview
    Polymer will help you to easily create Web Components for your web apps, using a similar syntax to HTML.Read More…
  • Code
    Creative Coding
    Creating a TextMate Bundle to Easily Package Your WordPress ProjectTextmate bundle 400
    If you've developed a WordPress theme or plugin, then you know how fast your codebase can grow in just a short amount of time. You start off with just a few PHP, JavaScript and CSS files. Then you find yourself creating image assets and adding them to your codebase together with their Photoshop file counterparts. After a while you'll be including other third-party PHP libraries as well. Then when you're finally ready to ship your WordPress project, you'll end up having a web of multiple file types like *.php, *.css, *.scss, *.js, *.min.js, *.png and *.psd, maybe you'll have some hidden files like .DS_Store. You would eventually need to clean your codebase up before zipping them all up for your users. Normally, this just means that you duplicate your files, then scan each and every directory deleting all the unwanted files. You'd then archive your project then perhaps use a third party app to remove the hidden files. This article will teach you on how to automate this packaging process by building a TextMate Bundle that would do everything for us with a single shortcut key. [tip]This article teaches about some tips regarding TextMate, an awesome code editor for Macs. You can find more information on TextMate from MacroMate's website[/tip]Read More…
  • Code
    Articles
    What Are You Using?Preview 400
    We spend a lot of time following the thought leaders in web development, in many cases using the tools and libraries they've built, reading the posts they've written, articulating cool techniques they've learned, and in some cases, attending the defining conference for a specific language. But wouldn't it be great to learn what they focus on and what they use to build such awesomeness?Read More…