My .vimrc file

Ear­lier today I wrote a very basic post about cus­tomiz­ing Vim via your .vimrc file. In this post I want to get into more spe­cific details about my .vimrc settings.

There are actu­ally three dif­fer­ent places you can set your pref­er­ences in Vim. Your ~/.vimrc file is global—the set­tings there will apply no mat­ter the file you are work­ing on (a .tex file or a .css file) whether you are work­ing in your ter­mi­nal emu­la­tor (e.g., Terminal.app for us Mac folks) or in MacVim/gVim. You can also have a ~/.gvimrc file. The set­tings in this file will apply to any file you work on in MacVim or gVim, but not when you run Vim in the ter­mi­nal. (More on my .gvimrc will fol­low shortly.) Finally, you can set spe­cific set­tings by file type in your ~/.vim/ftplugin folder. So you might want some spe­cific set­tings for your .tex files that you don’t want for your .css files. In that case, you might put those set­tings in ~/.vim/ftplugin/tex.vim. I’m going to talk about that file next week when I talk about my favorite LaTeX plu­gin for Vim.

My .gvimrc

The .gvimrc file gov­erns pref­er­ences spe­cific to MacVim or gVim. There are only a few things I want to be dif­fer­ent when I open a file in MacVim v. when I open it in Vim. The main things are the size of the win­dow and the color scheme I am using.

Vim typ­i­cally fills the ter­mi­nal win­dow. If your ter­mi­nal win­dow is full-screen, Vim will be too. MacVim and gVim, on the other hand, have really small default win­dow sizes. I want some­thing big­ger than that when I’m writ­ing a paper or a long blog entry. But I don’t want my ter­mi­nal to resize every time I want to quickly edit a file in Vim. So I put my win­dow set­tings in my .gvimrc file. My ter­mi­nal doesn’t seem to dis­play the full range of col­ors that MacVim does. So I have a basic color scheme for Vim, and then I over­ride it with a nicer one in my .gvimrc.

With­out fur­ther 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 any­thing after a double-quote is ignored.

My .vimrc

My .vimrc is ever-changing. I’m always find­ing some­thing new to add or some­thing to delete. If you like, you can always check out my most recent .vimrc by look­ing at my github account. I actu­ally put all my dot­files there (all my con­fig­u­ra­tion files), save those that are computer-specific or sen­si­tive. So my .gvimrc file isn’t there, since I need a dif­fer­ent sized win­dow for MacVim on my lap­top than I do on my iMac. But the .vimrc is there, as are a num­ber of other files. The rule on github, if I under­stand cor­rectly, is that any­thing I put up there is fair game for any­one to use. Even if that isn’t quite right, please help your­self if you find any­thing inter­est­ing (unless you find a password—in that case, please tell me!).

Back to my .vimrc. I’m just going to cut and paste every­thing I think is inter­est­ing below. I’m going to cut out some plu­gin spe­cific stuff, stuff you don’t need unless you have a spe­cific plu­gin. In the future I’ll write about my favorite plu­g­ins, and when I do I’ll be sure to men­tion the parts of the .vimrc you need to make them work. I’ll add com­ments where I can below, so things make a bit of sense if you don’t know what you are look­ing 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 com­mands that are LaTeX spe­cific, but I’m going to save those until I talk about my favorite LaTeX plu­gin for Vim.

Key map­pings

As I close, there is one thing I want to draw your atten­tion to. In your .vimrc (or .gvimrc or ~/.vim/ftplugin/insert_filetype_here.vim) file you can define key map­pings. So if you don’t want to hit ESCAPE to enter nor­mal mode, and don’t want to, e.g., remap your Caps Lock key to ESCAPE as some have sug­gested, you can just define ;; to reg­is­ter as ESCAPE. You could define just about any key to reg­is­ter as any other. And you can make sin­gle key­strokes or a series of key­strokes expand to any­thing you like. This comes in very hand for LaTeX. More on that next time.

Posted Thursday, July 22nd, 2010 under Uncategorized.

8 comments

  1. Nice. I’ll have to study this.

    I like to have line num­bers too, but it annoys me that they are shown with the same color as my file. So you can put in some­thing like:

    highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=Black gui=NONE guifg=DarkGrey guibg=Black
    

    (Pick col­ors appro­pri­ately dif­fer­ent from your col­orscheme to set them apart.)

    You can also put in a sign to show that a line has been wrapped. I like to use the Uni­code sign ↳, e.g.:

    set showbreak=↳
    

    By default this goes after the line num­bers, but it would be bet­ter to have them line up with the line num­bers instead. For this you can do:

    set cpoptions+=n
    set showbreak=\ \ \ ↳ 
    

    The “\ ” are escaped spaces to push the sign ↳ to the end of where the line num­bers are.

    The only thing I don’t know is how to con­trol the color of the show­break char­ac­ters. I‘d rather they (or at least their back­ground) matched the line num­bers. But I’m a vim new­bie, so maybe I’ll fig­ure it out.

    The only other things I have in mine not in yours are

    set hlsearch

    to high­light searched-for terms and a line to set the GUI font

    set guifont=DejaVu\ Sans\ Mono\ 12

    .

    But I think I can learn from yours!

  2. Oh another thing: you have it there don’t call atten­tion to it:

    set display=lastline
    

    This is key when line wrap is on, because it allows it to show par­tial lines wrapped past the bot­tom of the screen. With­out this you get very annoy­ing @‘s indi­cat­ing that the line can’t be dis­played because it doesn’t fit in its entirety.

  3. Hey Kevin, The showbreak is nice. I hadn’t seen that and might have to try it out. Thanks for sug­gest­ing the hlsearch as well–that is a help­ful one that I used to include. The prob­lem with it was that I couldn’t remem­ber how to turn the high­light off, so I’d have high­lighted words when I didn’t want them!

    Re: the gui font. I worked with that one today and found out that you actu­ally do some­thing slightly dif­fer­ent with MacVim:

    set guifont=Mensch:h14
    

    The dif­fer­ence, of course, is that the size comes after a colon and an ‘h’. (You would still need to escape spaces with back­slashes, as you have done.) I don’t know if the ‘h’ value is pix­els or not. It seems kind of big to be pix­els to me, but I don’t know.

    And thanks for point­ing out the last­line bit. I for­got to men­tion that because I cut lots of related stuff out (at one point I had my git branch dis­played there, but an update to git broke that and I haven’t got­ten around to fig­ur­ing out how to fix it). But that is nice. If noth­ing else, it puts a lit­tle [+] after the file name if you have unsaved changes, so there is a lit­tle reminder about what is clean and what isn’t.

  4. Hello, since you’re dis­cussing line wrap set­tings here, may I ask how you gen­er­ally han­dle line wraps in LaTeX doc­u­ments? In emacs, Alt-Q nicely refor­mats the present para­graph, respect­ing LaTeX envi­ron­ments, inden­ta­tions, lists, etc. Is there any­thing like this in Vim? –Q with ‘par’ makes a mess of LaTeX envi­ron­ments within the para­graph, as do the stan­dard hard wrap options in Vim. Do you just use soft wrap? This doesn’t seem very appeal­ing either. It means that the line (which in Vim’s com­mand struc­ture is rather cen­tral) is no longer a mean­ing­ful unit at all: for pure text para­graphs, a line is a para­graph; but para­graphs with LaTeX envi­ron­ments are sev­eral lines. And doesn’t that make debug­ging harder, because an error on “line 123″ may now be any­where within a lengthy paragraph?

    • Hi Wolf­gang,

      You’re right that gqip and gwip make a mess of any in-paragraph envi­ron­ments. I agree with you that shorter lines are bet­ter than longer ones (for the rea­son you give). So I have a sep­a­rate file to gov­ern the tex file­type (‘~/.vim/ftplugin/tex.vim’). In that I have spec­i­fied two rel­e­vant options:

      
      setlocal textwidth=72
      setlocal formatoptions+=tcwa
      

      The first does just what you would think—it sets the line length. The sec­ond takes care of keep­ing my para­graphs together. The rel­e­vant 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 para­graph as the next line (so a line that ter­mi­nates in a non-whitespace char­ac­ter ter­mi­nates the para­graph). The ‘a’ option tells Vim to auto­mat­i­cally adjust lines within a para­graph. 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 char­ac­ters in length. Like­wise if I add text this forces a line break where you’d want, instead of wait­ing for you to deal with it manually.

      This might not be per­fect (at the moment it is giv­ing me trou­ble for some rea­son), but it seems to do the job for me. (The Vim­cast on hard wrap­ping text talks about these options and shows how they work. I highly rec­om­mend it if you haven’t seen it.)

  5. Thanks Char­lie! 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 ter­mi­nat­ing white­space to my lines — time to look at how Vim does reg­u­lar expressions…

  6. 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 Inser­tEn­ter * high­light LineNr guifg=DarkGray guibg=gray30 au InsertLeave * high­light LineNr guifg=black guibg=gray30

Leave a Reply