How I get started on using Vim, part 1 – my .vimrc explained
For some reason, I’ve had it in my head that as a programmer, I should learn how to use a command-line-based text editor like Vim. It is more universal across devices, so knowing it would at least give me an option to navigate around computers other than my own, is an advice I often hear. It would be more applicable if I were an ops person, but on the infrequent (though increasingly less so) occasions that I have to remotely log in into a server box, I do find that to be true. Furthermore, since for most codebases I deal with, I already have a terminal window open for git and build tools, I figure keeping text editing in the same context will be beneficial.
I tend to agree with Yehuda Katz’s experience learning Vim, that it is something one should learn incrementally. I started down this path almost two years ago, only to decide on several occasions that it wasn’t the right tool for me. It is not until very recently that I feel comfortable enough to use it as my primary text editor. Even then, there are times when I find myself opening up Sublime Text or Atom because they’re more appropriate for what I need to do.
What allowed me to be more comfortable in Vim is less about learning the shortcuts and muscle memory, but more the heavy customization that I have put on top of Vim to give it the convenience editors like Sublime have out of the box, and also the deeper insight into how Vim actually works.
I have been thinking about writing this blog post for a while, but never felt like I was ready to have an “authority” on this topic. But I decided today that it’s time I break down and explain my Vim setup (through the .vimrc
file) before it gets too complicated.
To me, the most fundamental difference between Vim and a GUI editor like Sublime is that Vim is a modal editor. That means Vim has different “modes” that a user could be in at any given time. In Sublime, you’re always in edit mode. Anything you type on the keyboard is recorded in the text buffer. Other actions, such as saving, linting or navigating, are done through special key combinations, such as key on Windows or
Cmd
key on OS X. In Vim, the default mode is NORMAL
, which is where you can do most of these “meta” actions on the document, without modifying the text itself. You need to explicitly enter the INSERT
mode to edit something, and escape out of it to perform those actions. Being aware of the current active mode is important, and will help beginners feel less confused and lost. You can make vim show the mode with the command :set showmode
while in NORMAL
mode, or see the .vimrc
breakdown to see how to set that permanently.
The second most powerful difference in Vim as compared to a GUI editor is the concept of motions, or text objects. There are a lot of great tutorials and resources out there explaining this. I find Vim Text Objects: The Definitive Guide to be a really great place to start, as it was the “ah ha!” moment for me. This way of moving around a document is probably what makes Vim much more appealing and efficient for me.
As I walk through my .vimrc
setup, I will denote the ones that I think are basic developer use as [basic]
, so search for those if you just want a quick way to get started with Vim. My .vimrc
file actually changes quite often as I refine the setup, so take a look at the source code to see the latest version. Here goes:
- An explanation of what this actually does/ why it’s necessary
set nocompatible
- This whole big chunk is about Vundle, one of the most popular Vim plugin managers out there. There are a few, but I find Vundle to be the most consistent across all plugins I come across, and it has worked really well for me.
" Vundle filetype off " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " Let Vundle manage Vundle Plugin 'gmarik/Vundle.vim' Plugin 'chriskempson/base16-vim' Plugin 'tpope/vim-fugitive' Plugin 'airblade/vim-gitgutter' Plugin 'tpope/vim-commentary' Plugin 'wincent/command-t' Plugin 'editorconfig/editorconfig-vim' Plugin 'scrooloose/nerdtree' Plugin 'Xuyuanp/nerdtree-git-plugin' Plugin 'pangloss/vim-javascript' Plugin 'bling/vim-airline' Plugin 'kien/ctrlp.vim' Plugin 'mustache/vim-mustache-handlebars' Plugin 'Raimondi/delimitMate' Plugin 'Yggdroot/indentLine' Plugin 'othree/html5.vim' Plugin 'wincent/terminus' Plugin 'scrooloose/syntastic' Plugin 'Valloric/YouCompleteMe' Plugin 'rking/ag.vim' Plugin 'cakebaker/scss-syntax.vim' Plugin 'hail2u/vim-css3-syntax' Plugin 'ap/vim-css-color' Plugin 'ryanoasis/vim-devicons' Plugin 'elzr/vim-json' Plugin 'tmux-plugins/vim-tmux-focus-events' Plugin 'tmux-plugins/vim-tmux' Plugin 'tpope/vim-surround' Plugin 'moll/vim-bbye' call vundle#end()
In order to install a new plugin, I just append it to the end of this list, then run 2 commands (in Normal mode):
:source ~/.vimrc
and:PluginInstall
. Removing one is just the opposite, andPluginClean
instead of install. - I like the dark Tomorrow Night/ base16 syntax theme, so that’s what I set up here. I use the same theme for Sublime, Atom as well as the terminal.
set background=dark let base16colorspace=256 " Access colors present in 256 colorspace" colorscheme base16-tomorrow
Here’s a screenshot of what my Vim setup looks like visually:
- [basic] This next chunk includes convenience settings that make working with Vim friendlier, especially to someone who’s more comfortable with a GUI editor like me. I have inline comments to explain what each of the configuration does, including some external links for more thorough explanation.
" Allow saving of files as sudo when forgot to start vim using sudo cmap w!! w !sudo tee > /dev/null % " Enable syntax highlighting syntax on " File type based indentation filetype plugin indent on " Show what mode you're currently in set showmode " Show what commands you're typing set showcmd " Allow modelines " http://vim.wikia.com/wiki/Modeline_magic set modeline " Show current line and column position in file set ruler " Show file title in terminal tab set title " Show line numbers set number " Always display the status line set laststatus=2 " Highlight current line set cursorline " Highlight search results as we type set incsearch " ignore case when searching... set ignorecase " ...except if we input a capital letter set smartcase " hides buffer instead of closing them " http://usevim.com/2012/10/19/vim101-set-hidden/ set hidden " Tab stuff set tabstop=4 set shiftwidth=4 set autoindent
- One of the things I like about Sublime Text is its sensible default for whitespace/invisible characters. The setting above tries to imitate some of that to make it feel more familiar.
" Show invisible characters set list set listchars=tab:→ ,trail:·,eol:˧
- [basic] Sublime makes it really easy to split windows and have things side by side. Vim has very good support for that too, but the default shortcut of requiring
<C-W>
, which means pressingcontrol-w
keys before every action, is rather clunky. These mappings making it more fun to navigate around the different panes you have open in Vim." Set split pane direction to be more natural set splitbelow set splitright " Buffer navigation noremap <Leader>] :bnext<CR> noremap <Leader>[ :bprev<CR> " bbye remap to w noremap <Leader>w :Bdelete<CR> " Navigate panes with control nnoremap <C-H> <C-W><C-H> nnoremap <C-J> <C-W><C-J> nnoremap <C-K> <C-W><C-K> nnoremap <C-L> <C-W><C-L>
If you’re lost of the difference between panes/ windows/ buffers, you’re not alone. I’m still confused as hell about them. Throw tabs in the mix and it gets complicated real quick. I find this explanation on buffers vs. tabs to be quite informative. I’m convinced for now to just stick with buffers with the help of airline plugin’s tabline feature, which keeps things simpler and more aligned with Sublime’s way of handling things.
- [basic] This is perhaps the most important key mapping in the entire .vimrc file 😉.
" remap : to ; to avoid pressing Shift nnoremap ; : vnoremap ; :
Since many Vim commands in Normal mode start with
:
, my right pinky gets quite a bit of stretching. If I don’t let go of the Shift key in time, I accidentally uppercase the command a lot, which gets annoying. Mapping;
to:
eliminates the need to press Shift. However, it does take a bit of practice to get used to this, if you’re already used to typing the colon. - Another mapping to save the pinky 🙌.
" remap key let mapleader = '\&lgt;Space>'
The <Leader> key is now the space bar, instead of the default
key, which is just way out there.. The spacebar is also convenient for both thumbs to press.
- These are plugin-specific settings that make these powerful plugins behave more intuitively.
" airline let g:airline#extensions#tabline#enabled = 1 " Show just the filename let g:airline#extensions#tabline#fnamemod = ':t' " NERDTree let NERDTreeShowHidden = 1 let NERDTreeMapOpenSplit = '<C-x>' let NERDTreeMapOpenVSplit = '<C-v>' let NERDTreeMapOpenInTab = '<C-t>' " open NERDTree automatically on vim start, even if no file is specified autocmd StdinReadPre * let s:std_in=1 autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif " open NERDTree with `Ctrl-n` map <C-n> :NERDTreeToggle<CR> " Syntastic recommended settings set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%* let g:syntastic_always_populate_loc_list = 1 let g:syntastic_auto_loc_list = 1 let g:syntastic_check_on_open = 1 let g:syntastic_check_on_wq = 0 let g:syntastic_html_tidy_ignore_errors=[" proprietary attribute " ,"trimming empty <", "unescaped &" , "lacks "action", "is not recognized!", "discarding unexpected"] " make ESC key work for command-t if &term =~ "xterm" || &term =~ "screen" let g:CommandTCancelMap = ['<ESC>', '<C-c>'] endif " Disable JSON quote concealing let g:vim_json_syntax_conceal = 0 " ctrlp " show hidden files let g:ctrlp_show_hidden = 1 " ycm " <Enter> key completion let g:ycm_key_list_select_completion = ['<TAB>', '<Down>', '<Enter>']
While plugins and basic setup establish a friendlier stage for a beginner to Vim to start exploring around, one of the most frustrating things when trying Vim out is not knowing how to do basic developer tasks in Vim. In addition to the “[basic]” setup I have outlined here, I will document more of what those tasks are, such as find/replace, copy/paste, indentation etc. in the second part of this series.
comments powered by Disqus