dotfiles / vimrc

" ~/.vimrc

"""" General settings """""""""""""""""""""""""""""""""""""""""""""""
set nocompatible                " IMprove the editor
set encoding=utf-8              " (we're not in the eighties anymore)

set incsearch
set spelllang=sv
set hidden                      " Abandoning a buffer makes it hidden instead of
                                " unloading it, thus preserving changes/stuff.

set showcmd                     " Show keys pressed in normal mode in last line
set splitright                  " :vsplit splits to right

" Use persistent undo if available
if version >= 703
  set undodir=~/.vim/undodir
  set undofile
  set undolevels=1000
  set undoreload=10000
end


"""" Mappings """""""""""""""""""""""""""""""""""""""""""""""""""""""
"let mapleader = ","
let mapleader = " "

" Bind C-h to fully dedent (clear all leading whitespace)
noremap  <C-h> :s/^\s*//<CR>
inoremap <C-h> <Esc>:s/^\s*//<CR>i

" Better command-line editing shortcuts
cnoremap <C-a> <Home>
cnoremap <C-e> <End>

cnoremap <C-h> <S-Left>
cnoremap <C-l> <S-Right>

cnoremap <C-j> <Down>
cnoremap <C-k> <Up>

" Fast shortcut for opening files
nnoremap <C-e> :e 

" Headerify current line
nnoremap <Leader>== yypv$r=A==<ESC>kI <ESC>Vj:center<CR>
"nnoremap <Leader>-- yypv$r-A--<ESC>kI <ESC>j

" Shortcut for splitting
" NO! use <C-w>s and <C-w>v instead!
"nnoremap <Leader>-  :split<CR>
"nnoremap <Leader>\| :vsplit<CR>

nnoremap <Leader>q :q<CR>

" Make Y behave similar to D
map Y y$

" Repeat last shellscript on <Leader>.
nnoremap <Leader>. :!!<CR>

" Make w!! do a superuser-write of the file
cnoremap w!! w !sudo dd of=%

" Handy shortcut for pasting -- press F8, paste stuff, move on
nmap <F8> :set paste<CR>i
imap <F8> <ESC>:set paste<CR>i<Right>
au InsertLeave * set nopaste
"set pastetoggle=<F8>

" This is a godsend when messing about with syntax rule files
map <F11> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>


"""" Buffer management mappings """""""""""""""""
" Switch to last (alternate) buffer with <Leader><Leader>, and reload current
" file with <Leader>e.
nnoremap <Leader><Leader> <C-^>
nnoremap <Leader>e :e%<CR>


" Switch to buffer N with <Leader>N
nnoremap <Leader>1 :buf 1<CR>
nnoremap <Leader>2 :buf 2<CR>
nnoremap <Leader>3 :buf 3<CR>
nnoremap <Leader>4 :buf 4<CR>
nnoremap <Leader>5 :buf 5<CR>
nnoremap <Leader>6 :buf 6<CR>
nnoremap <Leader>7 :buf 7<CR>
nnoremap <Leader>8 :buf 8<CR>
nnoremap <Leader>9 :buf 9<CR>
nnoremap <Leader>0 :buf 10<CR>


"""" Indentation settings """""""""""""""""""""""""""""""""""""""""""
" Note to self: expandtab, shiftwidth, tabstop, softtabstop, autoindent,
"               smartindent, preserveindent, copyindent, shiftround, smarttab.
" ---- Use :retab for re-applying indentation rules!

set copyindent                  " copy leading whitespace from previous line
set autoindent
set expandtab                   " expand tabs into spaces on <C-t>/<C-d>
"set noexpandtab
set nosmarttab                  " lets <Tab> and <BS> perform predictably.

set tabstop=4                   " let actual tabs show up as 4 chars wide

" Use smart tabs (tabs for indentation, spaces for aligning)
"set shiftwidth=4

" Use 2-space soft tabs
set softtabstop=2
set shiftwidth=2
"set shiftround                  " make (de|in)dents snap to tabstops

" Tab key should always enter a tab character; use <C-t> and <C-d> for
" indentation.
inoremap <Tab> <C-v><Tab>


"""" Formatting, aesthetics """""""""""""""""""""""""""""""""""""""""
set number                      " Use line numbering
set ruler                       " Show scroll percentage in statusline
set cursorline                  " Highlight cursor's line

set textwidth=80
set nowrap sidescroll=5         " Scroll horizontally 5 chars at a time when
                                " exceeding screen size.
set scrolloff=5                 " Keep 5 lines visible vertically

set formatoptions+=n            " Better indent numbered lists in comments
set formatoptions+=l            " Don't wrap lines that were already long
set formatoptions+=j            " Remove comment leaders when joining

" Use pretty characters for vim's rendering of things.
" (fillchar/stlnc doesn't support multibyte chars.. :<)
set list listchars=tab:»\ ,trail:·,nbsp:␣,extends:$,precedes:^
set fillchars=vert:│",stlnc:─╎║┃

set foldmethod=marker

if version >= 703
  set colorcolumn=+1            " Highlight column after the `textwidth`th one
end
syntax on                       " Use syntax highlighting


" Add a few custom filetypes
au BufRead,BufNewFile {*.json}               setfiletype javascript
au BufRead,BufNewFile {/usr/share/X11/xkb/*} setfiletype c
au BufRead,BufNewFile {*.md}                 setfiletype markdown

au BufRead,BufNewFile {*.lecnote}            setfiletype lecnote
au BufRead,BufNewFile {*.selfml}             setfiletype selfml
au BufRead,BufNewFile {*.todo,todo}          setfiletype todo
au BufRead            {*/xchatlogs/*.log}    setfiletype irclog | set readonly nomodifiable

" Add custom filetype-dependent metadata
au FileType lua  setlocal comments=:--


"""" Colour scheme """"""""""""""""""""""""""""""""""""""""""""""""""
if $TERM == "xterm"
  " assume 256-colour terminal
  set t_Co=256
end
"if $TERM != "linux" && $TERM != "screen"
"  set t_Co=256
"end

"color Tomorrow-Night-Bright-ff
let g:jellybeans_background_color_256="16"
let g:jellybeans_overrides =
\     { 'CursorLine'   : { '256ctermbg' : '233'  }
\     , 'StatusLine'   : {
\                          '256ctermfg' : '3'
\                        , '256ctermbg' : '236'  }
\     , 'StatusLineNC' : { '256ctermfg' : '242'
\                        , '256ctermbg' : '234'  }
\     , 'ColorColumn'  : { '256ctermbg' : '233'  }
\     , 'LineNr'       : { '256ctermfg' : '238'  }
\     , 'Folded'       : { '256ctermbg' : '234'  }
\     , 'CursorLineNr' : { '256ctermfg' : '3'
\                        , '256ctermbg' : '234'  }
\     , 'Conceal'      : { '256ctermfg' : '14'
\                        , '256ctermbg' : 'NONE' }
\     , 'ModeMsg'      : { 'attr'       : 'bold'
\                        , '256ctermfg' : '2'    }
\     }
"\                          '256ctermfg' : '253'
color jellybeans


set conceallevel=2
"hi Conceal ctermfg=cyan ctermbg=none


"""" Status line """"""""""""""""""""""""""""""""""""""""""""""""""""
"set laststatus=1                " Only display status line if >1 window
set laststatus=2                " Never mind.  Always display status line.

set statusline=
set statusline+=%*              " --> highlight
set statusline+=%<              " truncation location marker
set statusline+=\ %t\           " space, filename (tail), space
set statusline+=%#StatusLineNC# " <-- NC styling
set statusline+=%y%m%r          " space, info (filetype, edited?, readonly?)
set statusline+=%=              " spacer <==>
set statusline+=[%2n]\          " 2-char bufno, space
set statusline+=%-8.(%l,%c%V%)  " group of line# & col#
set statusline+=\ %*            " --> highlight again (with space)
set statusline+=\ %P\           " space, percentage in file, space

" Make the ruler also display the bufno
set rulerformat=%2*[%2n]\ %-8.(%l,%c%V%)\ %P\ 

" Title string
set titlestring="hello"
set title


""" Dump files to web server """"""""""""""""""""""""""""""""""""""""
" Uses 2html.vim to generate an HTML file and upload it to my server.
let g:html_number_lines = 0

if !exists("*UploadCurrFileHTML")
  fun UploadCurrFileHTML()
    TOhtml
    silent w
    silent !scp % xen:srv/http/up
    silent !rm %
    silent close
    " TODO: Put link in clipboard
  endf

  com Up     call UploadCurrFileHTML()
  com Upload call UploadCurrFileHTML()
endif


"""" Plug-ins """""""""""""""""""""""""""""""""""""""""""""""""""""""
call pathogen#infect()

" VimOrganizer
"filetype plugin indent on
"au! BufRead,BufWrite,BufWritePost,BufNewFile *.org  " Remove existing autocommands
"au  BufEnter *.org  call org#SetOrgFileType()       " Init VimOrganizer

"NERDTree
let g:NERDTreeMinimalUI=1
noremap <F10> :<C-u>NERDTreeToggle<CR>

"netrw
let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro'


""" Plugin keymappings """"""""""""""""
noremap <Leader>a= :Tabularize /=<CR>
noremap <Leader>a: :Tabularize /:<CR>
noremap <Leader>a\| :Tabularize /\|<CR>

nnoremap <Leader>, :source %<CR>


"""" Scratchpad area """"""""""""""""""""""""""""""""""""""""""""""""
""" temp
"if !exists("*JumpToNotesWindow")
"  fun JumpToNotesWindow()
"    let l=line(".")
"    exe "normal \<C-w>l" . l . "G"
"  endf
"
"  noremap <C-x>  :call JumpToNotesWindow()<CR>
"endif

au Syntax vim  let b:comment_prefix='"'
au Syntax lua  let b:comment_prefix='--'
noremap <Leader>gc :exe 's:^ \?:' . b:comment_prefix . ':'<CR>
noremap <Leader>gC :exe 's:^' . b:comment_prefix . '\( \?\):\1\1:'<CR>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.