Ask HN: What's in your .vimrc file?

123 points by Xichekolas ↗ HN
Other than the default stuff of course. As you can see, my additions are quite pathetic so far (only been into vim for a few months):

  set expandtab
  set sts=2
  if v:progname =~? "gvim"
    colorscheme twilight
  endif

68 comments

[ 2.6 ms ] story [ 97.7 ms ] thread
perhaps of interest:

  " for vertical split, with the pipe dividers hidden
  :hi clear vertsplit
  :hi vertsplit ctermbg=Black ctermfg=Black

  " quit quits all
  :nmap :q :qa
  :nmap :wq :wqa

  " H/L go to beginning/end of line without moving fingers
  :nmap H 0
  :nmap L $

  " turn off matching paren highlighting
  let loaded_matchparen = 1
Why don't you use paren matching?

  "tab completion of words in insert mode
  function InsertTabWrapper()
        let col = col('.') - 1
        if !col || getline('.')[col - 1] !~ '\k'
            return "\<tab>"
        else
            return "\<c-p>"
        endif
  endfunction

  inoremap <tab> <c-r>=InsertTabWrapper()<cr>
Most of what I use was copied from this excellent site: http://www.vi-improved.org/vimrc.php
I wonder why you were voted down (not answering OP question to the letter?) -- that's one of the nicest references I've seen.
Downvoting seems to be in fashion lately. I've noticed a lot of perfectly good comments voted down without any apparent cause, only to float back up later on.

This is something of the last two weeks or so, before that it was happening too, but much less frequently.

Could it have something to do with the mechanism for voting and the prevalence of iPhones? I think it would be the cats meow if when detecting the iPhone or any mobile browser the down vote arrow was moved to the right of the title bar area. I say this because my fat fingered attempt at upvoting a comment has the reverse effect of my intent on many occasion.

PG, Are you still looking for a specific iPhone interface?

Me too, I've done this on occasion on my iPhone. The arrows are so small and close together. If you see capitalized words in the middle of a sentence Like this, that too is the iPhone's fault.
(comment deleted)
Some I like:

  " Keep cycled-away buffers open (preserving undo, 
  " allowing buffer switch without write)
  set hidden

  " Facilities for handling pasting into vim, preserving
  " indentation of the pasted text.
  " This will make <F4> start paste mode and <F5> stop paste mode.
  " Note that typing <F5> in paste mode inserts <F5>, since in paste
  " mode everything is inserted literally, except the 'pastetoggle' key
  " sequence.
  map <F4> :set paste<CR>
  map <F5> :set nopaste<CR>
  imap <F4> <C-O>:set paste<CR>
  imap <F5> <nop>
  set pastetoggle=<F11>

  " Toggle line-numbers with key sequence Ctrl-N-N:
  nmap <C-N><C-N> :set invnumber <CR>

  " Consolidate swapfiles to keep working directories clean
  set directory=~/.vim/swap
Map some "leader" commands for common operations, eg:

  let mapleader=','
  let g:mapleader=','

  nmap <leader>w :w!<cr>
  nmap <leader>q :q<cr>
  nmap <leader>f :find<cr>
  map <leader>d :execute 'NERDTreeToggle ' . getcwd()<CR>
  nmap <leader>m :make<cr>

  " cut & paste
  map <leader>c "+y
  map <leader>x "+d
  map <leader>v "+p
Also quicker window navigation:

  map <C-j> <C-W>j
  map <C-k> <C-W>k
  map <C-h> <C-W>h
  map <C-l> <C-W>l
Mainly for python scripting, but works decently for most editing I do.

  syntax on " syntax highlighting
  set tabstop=4 " PEP-8 uses 4 spaces per indentation level
  set shiftwidth=4 " shifting (PEP-8)
  set expandtab " spaces instead of tabs (PEP-8, and just bettter in general)
  filetype on " file type detection
  filetype indent on " special indentation rules for file type
  filetype plugin on " auto-completion rules for file type
  set hls " highlight search terms (:noh to turn off temporarily)
  set ignorecase " ignore case for searches (:set noignorecase to turn off)
  set incsearch " search as you type
  colorscheme darkblue " slightly nicer colour scheme
  set scrolloff=15 " keep 15 lines of context on both sides of cursor when scrolling
http://github.com/paulgb/settings/blob/9ebf793bcf202589c74f3...
I just keep swap files and backup ones in my .vim/ directory

  set backupdir=~/.vim/backup
  set backup
  set directory=~/.vim/tmp
This to see line numbers on the left (i find it a bit more readable instead of watching at the bottom-right)

  set number
added a simple way to speedup my writing with (I know it's crappy but it works ...until you need to add just one bracket/quote)

  " autocomplete parenthesis, brackets and braces
  inoremap ( ()<Left>
  inoremap [ []<Left>
  inoremap { {}<Left>
  
  " autocomplete quotes
  inoremap	'  '<Esc>:call QuoteInsertionWrapper("'")<CR>a
  inoremap	"  "<Esc>:call QuoteInsertionWrapper('"')<CR>a
  inoremap	`  `<Esc>:call QuoteInsertionWrapper('`')<CR>a
  
  function! QuoteInsertionWrapper (quote)
    let col = col('.')
    if getline('.')[col-2] !~ '\k' && getline('.')[col] !~ '\k'
      normal ax
      exe "normal r".a:quote."h"
    end
  endfunction
and mapped f1/f2 to switch buffer

  noremap <f1> :bprev!<CR>
  noremap <f2> :bnext!<CR>
Some of this wall of text was stolen from a nice, but a bit too big, vimrc i found somewhere

If you need something specific try http://vim.wikia.com/

I do a lot of copying/pasting from my browser (and other random places), so these mappings make my life easier:

  " copy/paste with the system clipboard
  map ^P "+gP
  map ^C "+y
Also this, for dealing with DOS-style stuff:

  " map <ctrl>+s to remove ^M from the ends of lines
  map ^S :%s\/^M\/\/g

  map ^C "+y
Ugh. I couldn't do that. C-c is a shortcut to exit Insert Mode or to stop a command (similar to C-g usage in Emacs or mutt).

  map ^S :%s\/^M\/\/g
Be careful with that on terminals. C-s is usually mapped in with flow control on terminals.
It's a little outdated, but I spent some time on tweaking it: http://amix.dk/vim/vimrc.html

I specially like:

  map <space> /
  map <c-space> ?
And the parenthesis/bracket expanding.
Nothing.

I work on many different systems from time to time. I don't like to get too used to things that aren't universal. I have found out (the hard way) that relying on convenience can bite you in the ass when it's 3AM and you've been up for 67 consecutive hours and you execute some keystrokes from habit and they don't do what you expected.

I'll admit, I probably cripple my productivity overall for working this way, but it's what I'm used to.

Out of curiosity, why not just wget or curl -O a file from a webserver?
Most of my work the last few years has been in the compliance and IT or Physical security (or converged) space.

The machines I am working on are not my own, and often have no access to the Internet directly.

However, I'm generally doing more "editing" than "coding" so it's not a major limitation anyway.

> I have found out (the hard way) that relying on convenience can bite you in the ass when it's 3AM and you've been up for 67 consecutive hours and you execute some keystrokes from habit and they don't do what you expected.

You really could say that about anything. You could say that one shouldn't program in dynamically-typed languages because 'one day' you'll have to program in a statically-typed language and all your habits will 'bite you in the ass.' I think that you should really blame this part:

> and you've been up for 67 consecutive hours

Unless you're trying out for the SEALS, I don't think this is normally part of any job description. And I hardly think this is some sort of 'common case' that you should be planning for. There's a difference between preparing for the worst and over-preparing for the worst.

Don't get me wrong, there are many conveniences that I DO use.

However, for editors I've just always preferred to stay as basic and reliable as possible. vi is quick, easy, simple and has been on every *nix machine that I've ever walked up to. Relying on customizitions beyond that is, for me, sub-optimal.

I have had more than a handful of workdays that spanned 2 or 3 or 4 continuous days with little or no sleep at all (although thankfully not in several years). In those cases we were usually recovering from planned maintenance gone wrong. In one case I was trying to edit/update some config files, force software upgrades into a bunch core switches and dispatch pilots with spare parts. It was helpful, to me, to be able to rely on the basics as I hopped through SSH sessions from server to server (but, I did use host keys to speed my logins).

My case is moreso for edits and small customizations and not long sessions of coding, so I really don't NEED a whole lot beyond the basics of what I can memorize.

> when it's 3AM and you've been up for 67 consecutive hours

That can't be healthy. My longest stretch (age 17) was something like that and I fell down half a flight of stairs without waking up when going home.

Please be careful.

Mine's not very advanced; .gvimrc for MacVim:

  set guifont=Pragmata:h14
  set guioptions=egmrLt
  set nohlsearch
  set number
  set ts=3
  colors vividchalk
  syntax on
Pragmata is a delicious, delicious font.
90 Euros for a font? Whoa.
Characters. Lots of characters.

  syntax on
  set background=dark
  set et
  set ts=4
  set ruler
  set shiftwidth=4
Most of my .vim folder and .vimrc came from andreiz and tomasr. I have it committed to github for easy download on any server I'm working on. http://github.com/bnmrrs/dotfies if anybody would like to check it out.

  map ,q :q!<CR>
  map ,s :w<CR>
  map ,w :x<CR>
  nmap <silent> <F6> :set number!<CR>
and ctags are especially useful
Consider doing this instead:

  let mapleader = ","    " 
  let g:mapleader = ","

  map <Leader>q :q!<CR>
  map <Leader>s :w<CR>
  map <Leader>w :x<CR>
The mapleader is basically used to give your custom keybindings a separate namespace. It looks like that's what your doing here, but using the variable makes it easier to change later f you want.
Good idea, I'll have to add that.
Do this to prevent yourself from learning the arrow keys while learning:

  map <down> <nop>
  map <left> <nop>
  map <right> <nop>
  map <up> <nop>   

  imap <down> <nop>
  imap <left> <nop>
  imap <right> <nop>
  imap <up> <nop> 
(There's actually a recently reported bug that causes the up arrow to do some strange things, but you get the idea).
As you say, that's a good way to force your fingers to learn the way of h, j, k and l.

I started with vi so long ago that I remember being surprised around five years ago when I accidentally discovered that vim knows about the arrow keys. So I've never had to un-learn the arrow keys.

You may want to turn them back on eventually. It's nice to be able to move the cursor without thinking about it, whether your hands are on the keyboard, over the arrow keys or on the mouse, sort of like being able to play the chord you want no matter where your hand is on the fretboard.

As for my .vimrc, there are years of settings and commands I never use and can't remember what for, like junk DNA. I enjoy discovering and sharing the occasional treat, but I've gone pretty much meat and potatoes over the years.

Depending on your setup, your ruler (:set ruler) may or may not always show the file. This will make it always show:

set statusline=%<%F\ %h%m%r%=%-14.(%l,%c%V%)\ %P

I like the tabula colorscheme, which you can find on vim.org:

colorscheme tabula

I like smartcase: set ic set smartcase

and I like the ability to override it when I search: /\Cfindonlylowercaseinstances (:help, and then look in chapter usr_27.txt)

A good line to memorize:

  :set et sts=4 sw=4 ts=4

  et  = expandtab (spaces instead of tabs)
  ts  = tabstop (the number of spaces that a tab equates to)
  sw  = shiftwidth (the number of spaces to use when indenting
        -- or de-indenting -- a line)
  sts = softtabstop (the number of spaces to use when expanding tabs)
Also you might want to move 'colorscheme twilight' into the ~/.gvimrc file instead; it makes the .vimrc file cleaner when you separated out all of the gui-specific stuff.

My full vimrc and setup is here: http://github.com/bsandrow/vimc

{edit} Another good one:

  noremap Y y$
By default Y is mapped to the equivalent of yy. This makes Y act like the 'copy/yank' equivalent of D. {/edit}
I think :set wildmenu is great, especially for relative newbies such as myself:

  command! CD cd %:p:h " change to current buffer's directory
  set incsearch " Incremental search
  set hlsearch " Highlight search
  set guifont=Bitstream\ Vera\ Sans\ Mono\ 12
  set nowritebackup " no stupid backup files
  set noswapfile    " no stupid recovery files
  set wildmenu " it's wild
  set visualbell
  set fileformat=unix
This crap has accumulated over the years, so I'm not sure if any of it is obsolete or unneeded, but here's what I found in mine:

  set expandtab         " Convert tabs to spaces
  set tabstop=4         " <TAB> four spaces
  set shiftwidth=4      " Shift width four spaces (for auto indent)
  set noautoindent      " Turn off autoindent by default
  set smartindent       " Use smart indent instead
  set incsearch         " Use incremental searches (cool)
  set backspace=2       " Set backspace mode to allow backspacing in insert mode
  set ruler             " Show position of cursor in status line
  set showmatch         " Show matching parens/braces when writing code
  set wh=55             " Minimum window height
  set textwidth=78      " Maximum line width when writing comments

  " Speed up response to ESC key
  set notimeout
  set ttimeout
  set timeoutlen=100

  " Make completion more like bash
  set wildmode=longest,list

  " Cure hangs during compiles?
  set swapsync=

  " For fuck's sake, don't throw away the indent when i hit #
  inoremap # X^H#

  " Disable auto-commenting of // in C/C++
  au FileType c,cpp setlocal comments-=://

  " Highlight trailing whitespace
  hi TrailingSpace ctermbg=1
  au filetype c,cpp,python match TrailingSpace "\s\+\n"
Sorry about how long this is!! This is the culmination of about 1.5 years using VIM as my exclusive text editor for web development. When I started I was using PHP and now I'm using Ruby on Rails. If you have any general questions about VIM, specific questions about my .vimrc, or anything related, ask in the comments and I'll try to answer:

set fo=tcqln ic nohls nu sc scs sm tm=200 wim=longest,list nonumber

let g:explDetailedList=1

" store all of your vim swp files in one place, make sure this directory exists

set backupdir=/Users/{YOUR_USERNAME}/vim_swp

set directory=/Users/{YOUR_USERNAME}/vim_swp

set bs=indent,eol,start

set hlsearch

" Use incremental searching

set incsearch

" Set standard setting for PEAR coding standards

set tabstop=4

set shiftwidth=4

" Auto expand tabs to spaces

set expandtab

" Auto indent after a {

set autoindent

set smartindent

" Linewidth to endless

set textwidth=0

" Do not wrap lines automatically

set wrap

" DO NOT Show line numbers by default

set nonumber

" Jump 5 lines when running out of the screen

set scrolljump=5

" Indicate jump out of the screen when 3 lines before end of the screen

set scrolloff=3

" Repair wired terminal/vim settings

set backspace=start,eol

" This function determines, wether we are on the start of the line text (then tab indents) or

" if we want to try autocompletion

function InsertTabWrapper()

    let col = col('.') - 1

    if !col || getline('.')[col - 1] !~ '\k'

        return "\<tab>"

    else

        return "\<c-p>"

    endif
endfunction

" Remap the tab key to select action with InsertTabWrapper

inoremap <tab> <c-r>=InsertTabWrapper()<cr>

" set list

" set listchars=tab:>-,trail:-

" set listchars=tab:>-,trail:-,eol:$

set ignorecase " caseinsensitive searches

set showmode " always show command or insert mode

set ruler " show line and column information

set showmatch " show matching brackets

set formatoptions=tcqor

set whichwrap=b,s,<,>,[,]

syntax on

" Added by chris

" from http://items.sjbach.com/319/configuring-vim-right

set hidden

set history=1000

nnoremap ' `

nnoremap ` '

set smartcase "is case sensitive only if there's a capital letter

set title

" enables matching if/elseif/else/etc., sort of works

runtime macros/matchit.vim

"from http://blog.learnr.org/post/59098925/configuring-vim-some-mo...

map H ^

map L $

" End Added by chris

let loaded_matchparen = 1

nmap <F6> <ESC>:call LoadSession()<CR>

let s:sessionloaded = 0

function LoadSession()

  source Session.vim

  let s:sessionloaded = 1
endfunction

function SaveSession()

  if s:sessionloaded == 1

    mksession!

  end
endfunction

autocmd VimLeave * call SaveSession() "Reopen your session with 'vim -S Session.vim'

I love the scrolljump and scrolloff, never seen those before...also I think you may have just changed my life by showing me how to use sessions.