Skip to content

Instantly share code, notes, and snippets.

@SammysHP
Created April 17, 2013 17:56
Show Gist options
  • Save SammysHP/5406372 to your computer and use it in GitHub Desktop.
Save SammysHP/5406372 to your computer and use it in GitHub Desktop.
.vimrc
" Remove ALL autocommands for the current group.
:autocmd!
" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
" Start pathogen
call pathogen#infect()
" In many terminal emulators the mouse works just fine, thus enable it.
set mouse=a
" Sets how many lines of history VIM has to remember
set history=50
" Enable filetype plugins
filetype plugin on
filetype indent on
" Reread files when they are changed from the outside (and if there are
" no changes in the buffer)
set autoread
" With a map leader it's possible to do extra key combinations
let mapleader = ","
let g:mapleader = ","
" Fast saving
nmap <leader>w :w<cr>
" Show n lines to the cursor - when moving vertically using j/k
set scrolloff=2
" Commandline completion and extra line with suggestions
set wildmenu
set wildmode=longest:full,full
" Always show current position and line numbers
set ruler
set number
" A buffer becomes hidden when it is abandoned TODO
set hidden
" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap=b,s,<,>,[,],h,l
" Display incomplete commands
set showcmd
" Ignore case when searching, but be smart (if uppercase search, search
" uppercase). Highlight search and search already while typing.
set ignorecase
set smartcase
set hlsearch
set incsearch
" Don't redraw while executing macros (good performance config) TODO
set lazyredraw
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
syntax enable
set background=dark
" Set extra options when running in GUI mode
if has("gui_running")
set guioptions-=T
set guioptions+=e
" remove scrollbars
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=b
set guioptions-=m
set guitablabel=%M\ %t
colorscheme desert
endif
set encoding=utf8
set fileformats=unix,dos,mac
" Turn backup off TODO
set nobackup
set nowritebackup
" set noswapfile
set directory=~/.vim/swap//,.
" Use spaces instead of tabs
set expandtab
" set smarttab
set shiftwidth=4
set tabstop=8
set softtabstop=4
set autoindent
set smartindent "TODO
set wrap
set linebreak
" Don't show the splash screen when starting
set shortmess+=I
" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :call VisualSelection('f')<CR>
vnoremap <silent> # :call VisualSelection('b')<CR>
" Move up/down in long lines
noremap j gj
noremap k gk
noremap gj j
noremap gk k
" block selection after line ending
set virtualedit=block
" Disable highlight when <leader><cr> is pressed
map <silent> <leader><cr> :noh<cr>
" Smart way to move between windows TODO
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
" " Close the current buffer TODO
" map <leader>bd :Bclose<cr>
"
" " Close all the buffers
" map <leader>ba :1,1000 bd!<cr>
" " Useful mappings for managing tabs
" map <leader>tn :tabnew<cr>
" map <leader>to :tabonly<cr>
" map <leader>tc :tabclose<cr>
" map <leader>tm :tabmove
" " Opens a new tab with the current buffer's path
" " Super useful when editing files in the same directory
" map <leader>te :tabedit <c-r>=expand("%:p:h")<cr>/
" " Switch CWD to the directory of the open buffer
" map <leader>cd :cd %:p:h<cr>:pwd<cr>
" Specify the behavior when switching between buffers TODO
try
set switchbuf=useopen,usetab,newtab
set stal=1
catch
endtry
" Return to last edit position when opening files
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Don't use Ex mode, use Q for formatting
map Q gq
" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>
" Switch ^ and 0
noremap 0 ^
noremap ^ 0
" Use ß as $
map ß $
" remap word-completion from <C-P> to <C-space>
imap <Nul> <C-P>
" Toggle NERDtree faster
map <silent> <leader>f :NERDTreeToggle<CR>
" Custom leader for the zen coding plugin so that <C-Y> is not overwritten
let g:user_zen_leader_key = '<leader>y'
" When you press gv you vimgrep after the selected text
vnoremap <silent> gv :call VisualSelection('gv')<CR>
" Open vimgrep and put the cursor in the right position
map <leader>g :vimgrep // **/*.<left><left><left><left><left><left><left>
" Vimgreps in the current file
noremap <leader>/ :vimgrep // <C-R>%<C-B><right><right><right><right><right><right><right><right><right>
" When you press <leader>r you can search and replace the selected text
vnoremap <silent> <leader>r :call VisualSelection('replace')<CR>
" When you search with vimgrep…
" - display results in cope
" - go to the next result
" - go to the previous result
map <leader>cc :botright cope<cr>
map <leader>n :cn<cr>
map <leader>N :cp<cr>
" Toggle paste mode on and off
map <leader>pp :setlocal paste!<cr>
" The following will make tabs and trailing spaces visible
set listchars=tab:>-,trail:·,eol:$
nmap <silent> <leader>s :set nolist!<CR>
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis
endif
" Set extra filetype detection
autocmd BufRead,BufNewFile *.less set filetype=css
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Helper functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! CmdLine(str)
exe "menu Foo.Bar :" . a:str
emenu Foo.Bar
unmenu Foo
endfunction
function! VisualSelection(direction) range
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", '\\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n$", "", "")
if a:direction == 'b'
execute "normal ?" . l:pattern . "^M"
elseif a:direction == 'gv'
call CmdLine("vimgrep " . '/'. l:pattern . '/' . ' **/*.')
elseif a:direction == 'replace'
call CmdLine("%s" . '/'. l:pattern . '/')
elseif a:direction == 'f'
execute "normal /" . l:pattern . "^M"
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
" Returns true if paste mode is enabled
function! HasPaste()
if &paste
return 'PASTE MODE '
en
return ''
endfunction
" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
let l:currentBufNum = bufnr("%")
let l:alternateBufNum = bufnr("#")
if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif
if bufnr("%") == l:currentBufNum
new
endif
if buflisted(l:currentBufNum)
execute("bdelete! ".l:currentBufNum)
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment