Earlier today I wrote a very basic post about customizing Vim via your .vimrc file. In this post I want to get into more specific details about my .vimrc settings.
There are actually three different places you can set your preferences in Vim. Your ~/.vimrc file is global—the settings there will apply no matter the file you are working on (a .tex file or a .css file) whether you are working in your terminal emulator (e.g., Terminal.app for us Mac folks) or in MacVim/gVim. You can also have a ~/.gvimrc file. The settings in this file will apply to any file you work on in MacVim or gVim, but not when you run Vim in the terminal. (More on my .gvimrc will follow shortly.) Finally, you can set specific settings by file type in your ~/.vim/ftplugin folder. So you might want some specific settings for your .tex files that you don’t want for your .css files. In that case, you might put those settings in ~/.vim/ftplugin/tex.vim. I’m going to talk about that file next week when I talk about my favorite LaTeX plugin for Vim.
My .gvimrc
The .gvimrc file governs preferences specific to MacVim or gVim. There are only a few things I want to be different when I open a file in MacVim v. when I open it in Vim. The main things are the size of the window and the color scheme I am using.
Vim typically fills the terminal window. If your terminal window is full-screen, Vim will be too. MacVim and gVim, on the other hand, have really small default window sizes. I want something bigger than that when I’m writing a paper or a long blog entry. But I don’t want my terminal to resize every time I want to quickly edit a file in Vim. So I put my window settings in my .gvimrc file. My terminal doesn’t seem to display the full range of colors that MacVim does. So I have a basic color scheme for Vim, and then I override it with a nicer one in my .gvimrc.
Without further ado, here is my .gvimrc file:
"set the height of my window: set lines=50 "set the width: set columns=100 "default MacVim colorscheme: colorscheme inkpot "while I like to spell check my .tex files, etc., I don't it going when I'm doing quick stuff in Vim so I set it just for MacVim here set spell
That is it. Note that the double-quote sign is the comment-leader for .vimrc files. So anything after a double-quote is ignored.
My .vimrc
My .vimrc is ever-changing. I’m always finding something new to add or something to delete. If you like, you can always check out my most recent .vimrc by looking at my github account. I actually put all my dotfiles there (all my configuration files), save those that are computer-specific or sensitive. So my .gvimrc file isn’t there, since I need a different sized window for MacVim on my laptop than I do on my iMac. But the .vimrc is there, as are a number of other files. The rule on github, if I understand correctly, is that anything I put up there is fair game for anyone to use. Even if that isn’t quite right, please help yourself if you find anything interesting (unless you find a password—in that case, please tell me!).
Back to my .vimrc. I’m just going to cut and paste everything I think is interesting below. I’m going to cut out some plugin specific stuff, stuff you don’t need unless you have a specific plugin. In the future I’ll write about my favorite plugins, and when I do I’ll be sure to mention the parts of the .vimrc you need to make them work. I’ll add comments where I can below, so things make a bit of sense if you don’t know what you are looking at.
"A basic colorscheme:
colorscheme desert
""""""""""""""""""""""""""""""""""""""""""
"" SEARCH, HIGHLIGHT, SPELLING, ETC.
""""""""""""""""""""""""""""""""""""""""""
"The following lets searches be incremental. So in normal mode, /sec will go to the first 'section', for example. I don't have to type /section for that:
set incsearch
"Turn syntax highlighting on. (This helps you know when you leave a brace open!):
syntax on
" Vim window stuff
"I'm pretty sure this makes wrapped lines break at spaces, not in the middle of words:
set linebreak
"I like to see line numbers:
set number
"Highlight the line that the cursor is on:
set cursorline
"paragraph formatting stuff:
"This tells Vim to use an external program, Par, to format my paragraphs. This comes from www.vimcasts.org
set formatprg=par
"I don't recall where this came from, but it makes Vim put all its backup and temporary files in places I don't mind:
set backupdir=~/.vim/vim-tmp,~/.tmp,~/tmp,~/var/tmp,/tmp
set directory=~/.vim/vim-tmp,~/.tmp,~/tmp,~/var/tmp,/tmp
""""""""""""""""""""""""""""""
" GENERIC PLUGIN BEHAVIOR """"
""""""""""""""""""""""""""""""
"Allows for plugins to do smart indentation (I think!):
filetype plugin indent on
"I think this lets you use filetype specific plugins:
filetype plugin on
"To be frank, offhand I don't know exactly what these two do. The first does something with the Omni Complete function, which is very helpful. The second does something when you have a file open in Vim and then open it with another program and make changes. I think this makes Vim update its version of the file:
set ofu=syntaxcomplete#Complete
set autoread
""""""""""""""
" SPACING """"
""""""""""""""
"If you are indented and start a new line, this makes the new line indented, too:
set autoindent
"This it makes some sort of autoindenting occur, like when you have an open { at the end of a line. I'm not sure I love this, so it might not last long:
set smartindent
"These deal with what the TAB key inserts. I set my tabs to be only two spaces wide (default is 4). The second one makes sure the shift function knows this (you use that by selecting some text and hitting > for multiple lines and >> for a single line. The last one converts TAB characters into spaces instead of TAB characters. (Apparently this is big to programmers, I use it to follow conventions in the Ruby programming language.
set tabstop=2 shiftwidth=2 expandtab
"""""""""""""""
"""" FOLDS """"
"""""""""""""""
"Specify a column-width for fold markers:
set foldcolumn=4
"I want to mark folds manually. So in a .tex file I can write %{{{ to start a section and %}}} to close it. Then a zc in normal mode will shrink the whole section to one line. Very tidy.
set foldmethod=marker "alternatives: indent, syntax, marker (uses `{{{` to open and `}}}` to close)
""""""""""""""""
" KEYMAP STUFF "
""""""""""""""""
"I don't want to have to find the ESCAPE key to switch to normal mode. This makes two quick semi-colons mean 'ESCAPE'. You could do ii instead or anything you want. Just don't do letters you want to type in succession often or you'll be very annoyed!
inoremap ;;
"I don't know where I got this from, but it all governs soft-wrapped lines. This creates a function such that, in normal mode, \w will turn soft-wrapping on and off. (\ is the default . Some people set it to , but \ works fine for me since I use LaTeX and am used to that reach!
"If you have wrapped lines, k will take you to the next line, which likely means the next paragraph. gk will take you down what you think of as one line. If you want that to be the default, then some of these mappings will do that.
noremap gk
noremap gj
noremap w :call ToggleWrap()
function ToggleWrap()
if &wrap
echo "Wrap OFF"
setlocal nowrap
set virtualedit=all
silent! nunmap
silent! nunmap
silent! nunmap
silent! nunmap
silent! iunmap
silent! iunmap
silent! iunmap
silent! iunmap
else
echo "Wrap ON"
setlocal wrap linebreak nolist
set virtualedit=
setlocal display+=lastline
noremap gk
noremap gj
noremap g
noremap g
inoremap gk
inoremap gj
inoremap g
inoremap g
endif
endfunction
noremap k gk
noremap j gj
noremap 0 g0
noremap $ g$
"This lets you use your mouse
set mouse=a
That is it. There are some other commands that are LaTeX specific, but I’m going to save those until I talk about my favorite LaTeX plugin for Vim.
Key mappings
As I close, there is one thing I want to draw your attention to. In your .vimrc (or .gvimrc or ~/.vim/ftplugin/insert_filetype_here.vim) file you can define key mappings. So if you don’t want to hit ESCAPE to enter normal mode, and don’t want to, e.g., remap your Caps Lock key to ESCAPE as some have suggested, you can just define ;; to register as ESCAPE. You could define just about any key to register as any other. And you can make single keystrokes or a series of keystrokes expand to anything you like. This comes in very hand for LaTeX. More on that next time.

Nice. I’ll have to study this.
I like to have line numbers too, but it annoys me that they are shown with the same color as my file. So you can put in something like:
(Pick colors appropriately different from your colorscheme to set them apart.)
You can also put in a sign to show that a line has been wrapped. I like to use the Unicode sign ↳, e.g.:
By default this goes after the line numbers, but it would be better to have them line up with the line numbers instead. For this you can do:
The “\ ” are escaped spaces to push the sign ↳ to the end of where the line numbers are.
The only thing I don’t know is how to control the color of the showbreak characters. I‘d rather they (or at least their background) matched the line numbers. But I’m a vim newbie, so maybe I’ll figure it out.
The only other things I have in mine not in yours are
to highlight searched-for terms and a line to set the GUI font
.
But I think I can learn from yours!
Oh another thing: you have it there don’t call attention to it:
This is key when line wrap is on, because it allows it to show partial lines wrapped past the bottom of the screen. Without this you get very annoying @‘s indicating that the line can’t be displayed because it doesn’t fit in its entirety.
Hey Kevin, The
showbreakis nice. I hadn’t seen that and might have to try it out. Thanks for suggesting thehlsearchas well–that is a helpful one that I used to include. The problem with it was that I couldn’t remember how to turn the highlight off, so I’d have highlighted words when I didn’t want them!Re: the gui font. I worked with that one today and found out that you actually do something slightly different with MacVim:
The difference, of course, is that the size comes after a colon and an ‘h’. (You would still need to escape spaces with backslashes, as you have done.) I don’t know if the ‘h’ value is pixels or not. It seems kind of big to be pixels to me, but I don’t know.
And thanks for pointing out the lastline bit. I forgot to mention that because I cut lots of related stuff out (at one point I had my git branch displayed there, but an update to git broke that and I haven’t gotten around to figuring out how to fix it). But that is nice. If nothing else, it puts a little [+] after the file name if you have unsaved changes, so there is a little reminder about what is clean and what isn’t.
Hello, since you’re discussing line wrap settings here, may I ask how you generally handle line wraps in LaTeX documents? In emacs, Alt-Q nicely reformats the present paragraph, respecting LaTeX environments, indentations, lists, etc. Is there anything like this in Vim? –Q with ‘par’ makes a mess of LaTeX environments within the paragraph, as do the standard hard wrap options in Vim. Do you just use soft wrap? This doesn’t seem very appealing either. It means that the line (which in Vim’s command structure is rather central) is no longer a meaningful unit at all: for pure text paragraphs, a line is a paragraph; but paragraphs with LaTeX environments are several lines. And doesn’t that make debugging harder, because an error on “line 123″ may now be anywhere within a lengthy paragraph?
Hi Wolfgang,
You’re right that
gqipandgwipmake a mess of any in-paragraph environments. I agree with you that shorter lines are better than longer ones (for the reason you give). So I have a separate file to govern the tex filetype (‘~/.vim/ftplugin/tex.vim’). In that I have specified two relevant options:The first does just what you would think—it sets the line length. The second takes care of keeping my paragraphs together. The relevant options here are the ‘w’ and ‘a’. The ‘w’ option tells Vim that any line that ends in a space is part of the same paragraph as the next line (so a line that terminates in a non-whitespace character terminates the paragraph). The ‘a’ option tells Vim to automatically adjust lines within a paragraph. So if I delete part of a line, this option tells Vim to pull words up from the next line until you get a line around 72 characters in length. Likewise if I add text this forces a line break where you’d want, instead of waiting for you to deal with it manually.
This might not be perfect (at the moment it is giving me trouble for some reason), but it seems to do the job for me. (The Vimcast on hard wrapping text talks about these options and shows how they work. I highly recommend it if you haven’t seen it.)
Thanks Charlie! I didn’t realise the power of the ‘w’ flag. That seems to do the trick. Now I only need to add a lot of terminating whitespace to my lines — time to look at how Vim does regular expressions…
Kevin,
I do this for my line numbers.
When I’m in Insert Mode the color changes:
” change line# color if in insert mode only au InsertEnter * highlight LineNr guifg=DarkGray guibg=gray30 au InsertLeave * highlight LineNr guifg=black guibg=gray30