" ======================================== " General vim sanity improvements " ======================================== " " " alias yw to yank the entire word 'yank inner word' " even if the cursor is halfway inside the word " FIXME: will not properly repeat when you use a dot (tie into repeat.vim) nnoremap ,yw yiww " ,ow = 'overwrite word', replace a word with what's in the yank buffer " FIXME: will not properly repeat when you use a dot (tie into repeat.vim) nnoremap ,ow "_diwhp "make Y consistent with C and D nnoremap Y y$ function! YRRunAfterMaps() nnoremap Y :YRYankCount 'y$' endfunction " Make 0 go to the first character rather than the beginning " of the line. When we're programming, we're almost always " interested in working with text rather than empty space. If " you want the traditional beginning of line, use ^ nnoremap 0 ^ nnoremap ^ 0 " ,# Surround a word with #{ruby interpolation} map ,# ysiw# vmap ,# c#{"} " ," Surround a word with "quotes" map ," ysiw" vmap ," c""" " ,' Surround a word with 'single quotes' map ,' ysiw' vmap ,' c'"' " ,) or ,( Surround a word with (parens) " The difference is in whether a space is put in map ,( ysiw( map ,) ysiw) vmap ,( c( " ) vmap ,) c(") " ,[ Surround a word with [brackets] map ,] ysiw] map ,[ ysiw[ vmap ,[ c[ " ] vmap ,] c["] " ,{ Surround a word with {braces} map ,} ysiw} map ,{ ysiw{ vmap ,} c{ " } vmap ,{ c{"} map ,` ysiw` " gary bernhardt's hashrocket imap => "Go to last edit location with ,. nnoremap ,. '. "When typing a string, your quotes auto complete. Move past the quote "while still in insert mode by hitting Ctrl-a. Example: " " type 'foo " " the first quote will autoclose so you'll get 'foo' and hitting will " put the cursor right after the quote imap wa " ==== NERD tree " Open the project tree and expose current file in the nerdtree with Ctrl-\ " " calls NERDTreeFind iff NERDTree is active, current window contains a modifiable file, and we're not in vimdiff function! OpenNerdTree() if &modifiable && strlen(expand('%')) > 0 && !&diff NERDTreeFind else NERDTreeToggle endif endfunction nnoremap :call OpenNerdTree() " ,q to toggle quickfix window (where you have stuff like Ag) " ,oq to open it back up (rare) nmap ,qc :cclose nmap ,qo :copen "Move back and forth through previous and next buffers "with ,z and ,x nnoremap ,z :bp nnoremap ,x :bn " ============================== " Window/Tab/Split Manipulation " ============================== " Move between split windows by using the four directions H, L, K, J " NOTE: This has moved to vim/settings/vim-tmux-navigator.vim. " nnoremap h " nnoremap l " nnoremap k " nnoremap j " Make gf (go to file) create the file, if not existent nnoremap f :sp +e nnoremap gf :tabe " Zoom in map ,gz o " Create window splits easier. The default " way is Ctrl-w,v and Ctrl-w,s. I remap " this to vv and ss nnoremap vv v nnoremap ss s " create <%= foo %> erb tags using Ctrl-k in edit mode imap <%= %>3hi " create <%= foo %> erb tags using Ctrl-j in edit mode imap <% %>2hi " ============================ " Shortcuts for everyday tasks " ============================ " copy current filename into system clipboard - mnemonic: (c)urrent(f)ilename " this is helpful to paste someone the path you're looking at nnoremap ,cf :let @* = expand("%:~") nnoremap ,cr :let @* = expand("%") nnoremap ,cn :let @* = expand("%:t") "Clear current search highlight by double tapping // nmap // :nohlsearch "(v)im (c)ommand - execute current line as a vim command nmap ,vc yy:p "(v)im (r)eload nmap ,vr :so % " Type ,hl to toggle highlighting on/off, and show current value. noremap ,hl :set hlsearch! hlsearch? " These are very similar keys. Typing 'a will jump to the line in the current " file marked with ma. However, `a will jump to the line and column marked " with ma. It’s more useful in any case I can imagine, but it’s located way " off in the corner of the keyboard. The best way to handle this is just to " swap them: http://items.sjbach.com/319/configuring-vim-right nnoremap ' ` nnoremap ` ' " ============================ " SplitJoin plugin " ============================ nmap sj :SplitjoinSplit nmap sk :SplitjoinJoin " Get the current highlight group. Useful for then remapping the color map ,hi :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" . " FG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"fg#") " ,hp = html preview map ,hp :!open -a Safari % "paste text in insertion mode inoremap "+pa "paste text in normal mode nnoremap "+p" "------------------------------------------------------------------------------ " Misc "------------------------------------------------------------------------------ " Toggle paste mode on and off map pp :setlocal paste! " copy all out of vim nnoremap gg"+yG " copy text in visual mode vnoremap "+yy " copy all inside vim nnoremap ggyG " edit file nmap ev :tabedit $MYVIMRC'tzo nmap em :tabedit makefile " move around compile errors nnoremap n :cnext nnoremap p :cprevious nnoremap l :redraw! imap cb 『』 nnoremap n :cn nnoremap p :cp " Fast Saving map w :w imap w :w vmap w :w " :W sudo saves the file " (useful for handling the permission-denied error) if !exists(':W') command W w !sudo tee % > /dev/null endif " Highlight current line - allows you to track cursor position more easily set cursorline " map CTRL-L to piece-wise copying of the line above the current one imap @@@hhkywjl?@@@P/@@@3s " Allow to copy/paste between VIM instances "copy the current visual selection to ~/.vbuf vmap y :w! ~/.vbuf "copy the current line to the buffer file if no visual selection vmap y :.w! ~/.vbuf "paste the contents of the buffer file nmap p :r ~/.vbuf "------------------------------------------------------------------------------ " Moving around, tabs, windows and buffers "------------------------------------------------------------------------------ " Switch between the last two files nnoremap " easy way to edit reload .vimrc nmap V :source $MYVIMRC nmap v :vsp $MYVIMRC " Quickly open a buffer for scribble map q :e ~/buffer " Quickly open a markdown buffer for scribble map x :e ~/buffer.md " Remove the Windows ^M - when the encodings gets messed up noremap m mmHmt:%s///ge'tzt'm "------------------------------------------------------------------------------ " Spell checking "------------------------------------------------------------------------------ " Pressing ,ss will toggle and untoggle spell checking map ss :setlocal spell! " visual shifting (does not exit Visual mode) vnoremap < >gv " Switch CWD to the directory of the open buffer map cd :cd %:p:h:pwd " Treat long lines as break lines (useful when moving around in them) map j gj map k gk " Close the current buffer (w/o closing the current window) map bd :bd " Close all the buffers map bda :1,1000 bd! " Useful mappings for managing tabs map tn :tabnew map to :tabonly map tc :tabclose map tm :tabmove map tj :tabnext map tk :tabprevious " Let 'tl' toggle between this and the last accessed tab let g:lasttab = 1 nmap tl :exe "tabn ".g:lasttab au TabLeave * let g:lasttab = tabpagenr() "------------------------------------------------------------------------------ " Visual mode related "------------------------------------------------------------------------------ " Visual mode pressing * or # searches for the current selection " Super useful! From an idea by Michael Naumann vnoremap * :call VisualSelection('f', '') vnoremap # :call VisualSelection('b', '') noremap :echo '戒黄赌毒及上下左右键!' noremap :echo '戒黄赌毒及上下左右键!' noremap :echo '戒黄赌毒及上下左右键!' noremap :echo '戒黄赌毒及上下左右键!' inoremap fn =expand("%:t") function! TogglePaste() if &paste setlocal nopaste else setlocal paste endif endfunction function! ToggleRspecTest() let l:line=getline('.') if l:line =~? ',\s*now:\s.*true' call setline('.', substitute(l:line, "\,\\s*now:\\s*true", "", "")) else call setline('.', substitute(l:line, "\\([^\\s]\\)\\s*do", "\\1, now: true do", "")) endif endfunction " set paste nnoremap tp :call TogglePaste() nnoremap ,tt :call ToggleRspecTest() vmap js :!python -m json.tool