Top 10 Pitfalls When Switching to Vim
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.
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 type5j
to select five lines down. Finally, you'd pressd
, 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
- A Starting Guide to Vim from TextMate
- From TextMate to Vim for Rails Coders
- Everyone who Tried to Convince me to use Vim was Wrong
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:
1 |
|
2 |
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):
1
2
" .vimrc File
3
" Maintained by: Jeffrey Way
4
" jeffrey@jeffrey-way.com
5
" https://code.tutsplus.com
6
"
7
8
"Forget compatibility with Vi. Who cares.
9
set nocompatible
10
11
"Enable filetypes
12
filetype on
13
filetype plugin on
14
filetype indent on
15
syntax on
16
17
"Write the old file out when switching between files.
18
set autowrite
19
20
"Display current cursor position in lower right corner.
21
set ruler
22
23
"Want a different map leader than \
24
"set mapleader = ",";
25
26
"Ever notice a slight lag after typing the leader key + command? This lowers
27
"the timeout.
28
set timeoutlen=500
29
30
"Switch between buffers without saving
31
set hidden
32
33
"Set the color scheme. Change this to your preference.
34
"Here's 100 to choose from: http://www.vim.org/scripts/script.php?script_id=625
35
colorscheme twilight
36
37
"Set font type and size. Depends on the resolution. Larger screens, prefer h20
38
set guifont=Menlo:h14
39
40
"Tab stuff
41
set tabstop=3
42
set shiftwidth=3
43
set softtabstop=3
44
set expandtab
45
46
"Show command in bottom right portion of the screen
47
set showcmd
48
49
"Show lines numbers
50
set number
51
52
"Prefer relative line numbering?
53
"set relativenumber"
54
55
"Indent stuff
56
set smartindent
57
set autoindent
58
59
"Always show the status line
60
set laststatus=2
61
62
"Prefer a slightly higher line height
63
set linespace=3
64
65
"Better line wrapping
66
set wrap
67
set textwidth=79
68
set formatoptions=qrn1
69
70
"Set incremental searching"
71
set incsearch
72
73
"Highlight searching
74
set hlsearch
75
76
" case insensitive search
77
set ignorecase
78
set smartcase
79
80
"Hide MacVim toolbar by default
81
set go-=T
82
83
"Hard-wrap paragraphs of text
84
nnoremap <leader>q gqip
85
86
"Enable code folding
87
set foldenable
88
89
"Hide mouse when typing
90
set mousehide
91
92
"Shortcut to fold tags with leader (usually \) + ft
93
nnoremap <leader>ft Vatzf
94
95
" Create dictionary for custom expansions
96
set dictionary+=/Users/jeff_way/.vim/dict.txt
97
98
"Opens a vertical split and switches over (\v)
99
nnoremap <leader>v <C-w>v<C-w>l
100
101
"Split windows below the current window.
102
set splitbelow
103
104
" session settings
105
set sessionoptions=resize,winpos,winsize,buffers,tabpages,folds,curdir,help
106
107
"Set up an HTML5 template for all new .html files
108
"autocmd BufNewFile * silent! 0r $VIMHOME/templates/%:e.tpl
109
110
"Load the current buffer in Firefox - Mac specific.
111
abbrev ff :! open -a firefox.app %:p<cr>
112
113
"Map a change directory to the desktop - Mac specific
114
nmap <leader>d :cd ~/Desktop<cr>:e.<cr>
115
116
"Shortcut for editing vimrc file in a new tab
117
nmap <leader>ev :tabedit $MYVIMRC<cr>
118
119
"Change zen coding plugin expansion key to shift + e
120
let g:user_zen_expandabbr_key = '<C-e>'
121
122
"Faster shortcut for commenting. Requires T-Comment plugin
123
map <leader>c <c-_><c-_>
124
125
"Saves time; maps the spacebar to colon
126
nmap <space> :
127
128
"Automatically change current directory to that of the file in the buffer
129
autocmd BufEnter * cd %:p:h
130
131
"Map code completion to , + tab
132
imap <leader><tab> <C-x><C-o>
133
134
" More useful command-line completion
135
set wildmenu
136
137
"Auto-completion menu
138
set wildmode=list:longest
139
140
"http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
141
set completeopt=longest,menuone
142
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
143
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
144
\ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
145
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
146
\ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
147
148
"Map escape key to jj -- much faster
149
imap jj <esc>
150
151
"Delete all buffers (via Derek Wyatt)
152
nmap <silent> ,da :exec "1," . bufnr('$') . "bd"<cr>
153
154
"Bubble single lines (kicks butt)
155
"http://vimcasts.org/episodes/bubbling-text/
156
nmap <C-Up> ddkP
157
nmap <C-Down> ddp
158
159
"Bubble multiple lines
160
vmap <C-Up> xkP`[V`]
161
vmap <C-Down> xp`[V`]
162
163
" Source the vimrc file after saving it. This way, you don't have to reload Vim to see the changes.
164
if has("autocmd")
165
augroup myvimrchooks
166
au!
167
autocmd bufwritepost .vimrc source ~/.vimrc
168
augroup END
169
endif
170
171
" easier window navigation
172
nmap <C-h> <C-w>h
173
nmap <C-j> <C-w>j
174
nmap <C-k> <C-w>k
175
nmap <C-l> <C-w>l
176
177
"------------------------"
178
"NERDTREE PLUGIN SETTINGS
179
"------------------------"
180
"Shortcut for NERDTreeToggle
181
nmap <leader>nt :NERDTreeToggle <CR>
182
183
"Show hidden files in NerdTree
184
let NERDTreeShowHidden=1
185
186
"autopen NERDTree and focus cursor in new document
187
autocmd VimEnter * NERDTree
188
autocmd VimEnter * wincmd p
189
190
"Helpeful abbreviations
191
iab lorem Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
192
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.
193
194
"Spelling corrects. Just for example. Add yours below.
195
iab teh the
196
iab Teh The
197
198
" Get to home dir easier
199
" <leader>hm is easier to type than :cd ~
200
nmap <leader>hm :cd ~/ <CR>
201
202
" Alphabetically sort CSS properties in file with :SortCSS
203
:command! SortCSS :g#\({\n\)\@<=#.,/}/sort
204
205
" Shortcut to opening a virtual split to right of current pane
206
" Makes more sense than opening to the left
207
nmap <leader>bv :bel vsp
208
209
" Saves file when Vim window loses focus
210
au FocusLost * :wa
211
212
" Backups
213
set backupdir=~/.vim/tmp/backup// " backups
214
set directory=~/.vim/tmp/swap// " swap files
215
set backup " enable backup
216
217
" No more stretching for navigating files
218
"noremap h ;
219
"noremap j h
220
"noremap k gj
221
"noremap l gk
222
"noremap ; l
223
224
set showmatch " show matching brackets
225
226
" print empty <a> tag
227
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?