set background=dark set t_RB= colorscheme molokai if &compatible set nocompatible endif " --------------------------------------------------------------------------- " Leader key " --------------------------------------------------------------------------- let mapleader = " " let maplocalleader = " " nnoremap set timeoutlen=500 " --------------------------------------------------------------------------- " Sensible interface defaults " --------------------------------------------------------------------------- syntax enable filetype plugin indent on set number set relativenumber set cursorline set ruler set showcmd set showmode set laststatus=2 set showtabline=2 set wildmenu set wildmode=longest:full,full set scrolloff=4 set sidescrolloff=4 set splitbelow set splitright set hidden set confirm set mouse=a " Search as you type; keep searches case-sensitive only when capitals are used. set incsearch set hlsearch set ignorecase set smartcase " Comfortable editing defaults. set backspace=indent,eol,start set autoindent set smartindent set expandtab set tabstop=2 set shiftwidth=2 set softtabstop=2 " Python convention: four-space indentation. augroup simple_indentation autocmd! autocmd FileType python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4 augroup END " Use the desktop clipboard when this Vim build supports it. if has('clipboard') set clipboard=unnamedplus endif " --------------------------------------------------------------------------- " Built-in file browser (netrw): tree-style sidebar, no plugin required " --------------------------------------------------------------------------- let g:netrw_banner = 0 let g:netrw_liststyle = 3 let g:netrw_winsize = 25 let g:netrw_keepdir = 1 let g:netrw_altv = 1 " Space-e: toggle the file browser on the left. nnoremap e :Lexplore " Useful keys while the file browser is focused: " Enter open file or directory " t open file in a new tab " - go to the parent directory " R rename selected file " D delete selected file " % create a new file " d create a new directory " --------------------------------------------------------------------------- " Tabs " --------------------------------------------------------------------------- nnoremap gt nnoremap gT nnoremap t :tabnew nnoremap c :tabclose " Move the current tab left or right. nnoremap [ :tabmove -1 nnoremap ] :tabmove +1 " --------------------------------------------------------------------------- " Everyday shortcuts (each is Leader plus one key) " --------------------------------------------------------------------------- nnoremap s :write nnoremap q :quit nnoremap x :write:quit nnoremap / :nohlsearch " Move between split windows. nnoremap h h nnoremap j j nnoremap k k nnoremap l l " --------------------------------------------------------------------------- " Project navigation " --------------------------------------------------------------------------- " Search recursively from Vim's current directory. set path+=** " Ignore generated and dependency directories. set wildignore+=*/.git/* set wildignore+=*/node_modules/* set wildignore+=*/dist/* set wildignore+=*/build/* set wildignore+=*/coverage/* " Space-f: type part of a filename and press Tab. nnoremap f :find " Space-o: show previously opened files. nnoremap o :browse oldfiles " Space-b: list buffers, then type the buffer number and press Enter. nnoremap b :buffers:buffer function! ProjectSearch() let l:query = input('Search project: ') if empty(l:query) return endif execute 'silent vimgrep /\V' \ . escape(l:query, '/\') \ . '/gj **/*' copen endfunction nnoremap g :call ProjectSearch() nnoremap n :cnext nnoremap p :cprevious function! ReindentFile() let l:view = winsaveview() normal! gg=G call winrestview(l:view) endfunction nnoremap i :call ReindentFile() xnoremap i =gv " Save all modified files. nnoremap a :wall " Splits. nnoremap v :vsplit nnoremap - :split nnoremap = = " Reload the configuration after editing ~/.vimrc. nnoremap r :source $MYVIMRC " Quickly leave Insert mode. inoremap jk " --------------------------------------------------------------------------- " Keymap help screen " --------------------------------------------------------------------------- function! ShowKeymapHelp() " Open the help in a temporary buffer. enew silent file Vim-Keymaps setlocal buftype=nofile setlocal bufhidden=wipe setlocal noswapfile setlocal nobuflisted setlocal nomodified setlocal wrap setlocal nonumber setlocal norelativenumber setlocal nocursorline call setline(1, [ \ 'VIM KEYMAPS', \ '===========', \ '', \ '', \ 'FILES AND BROWSING', \ ' Space e Toggle file browser', \ ' Space f Find project file', \ ' Space o Recently opened files', \ ' Space b List open buffers', \ ' Space a Create new parent directories and files', \ '', \ 'Inside the file browser:', \ ' Enter Open file or directory', \ ' t Open file in new tab', \ ' - Parent directory', \ ' R Rename file', \ ' D Delete file', \ '', \ 'TABS', \ ' Tab Next tab', \ ' Shift-Tab Previous tab', \ ' Space t Create new tab', \ ' Space c Close current tab', \ ' Space [ Move tab left', \ ' Space ] Move tab right', \ '', \ 'SEARCH', \ ' Space g Search text in project', \ ' Space n Next search result', \ ' Space p Previous search result', \ ' Space / Clear search highlighting', \ '', \ 'EDITING', \ ' Space s Save current file', \ ' Space a Save all files', \ ' Space i Auto-indent entire file', \ ' jk Leave Insert mode', \ '', \ 'WINDOWS', \ ' Space v Vertical split', \ ' Space - Horizontal split', \ ' Space = Equalize split sizes', \ ' Space h Move to left window', \ ' Space j Move to lower window', \ ' Space k Move to upper window', \ ' Space l Move to right window', \ '', \ 'VIM', \ ' Space q Quit', \ ' Space x Save and quit', \ ' Space r Reload .vimrc', \ ' Space ? Show this screen', \ '', \ 'Press q or Esc to close this screen.', \ 'Press Space e to open the file browser.', \ ]) setlocal nomodifiable normal! gg " These mappings work only inside this help screen. nnoremap q :bdelete! nnoremap :bdelete! endfunction " Show the help screen manually. nnoremap ? :call ShowKeymapHelp() " Detect whether Vim is receiving piped input. let g:vim_reading_stdin = 0 augroup keymap_help_startup autocmd! autocmd StdinReadPre * let g:vim_reading_stdin = 1 " Show the help only when Vim starts without a filename. autocmd VimEnter * \ if argc() == 0 && !g:vim_reading_stdin | \ call ShowKeymapHelp() | \ endif augroup END " --------------------------------------------------------------------------- " Space+a inside filemanager create parent directories and files " --------------------------------------------------------------------------- function! NetrwAddPath() let l:base = b:netrw_curdir let l:name = input('New path (end with / for directory): ') if empty(l:name) return endif let l:path = simplify(l:base . '/' . l:name) " A trailing slash means: create directories only. if l:name =~# '/$' try call mkdir(l:path, 'p') silent execute 'keepalt edit! ' . fnameescape(l:base) "silent execute "normal \" echo 'Created directory: ' . l:path catch echohl ErrorMsg echomsg 'Cannot create directory: ' . v:exception echohl None endtry return endif " Create all missing parent directories. let l:parent = fnamemodify(l:path, ':h') try call mkdir(l:parent, 'p') " Create only when the file does not already exist. if getftype(l:path) ==# '' call writefile([], l:path, 'b') elseif isdirectory(l:path) throw 'Path is a directory: ' . l:path endif catch echohl ErrorMsg echomsg 'Cannot create file: ' . v:exception echohl None return endtry " Refresh the Netrw file listing. "silent execute "normal \" silent execute 'keepalt edit! ' . fnameescape(l:base) " Find the main editing window. let l:target_window = winnr('#') " Find the main editing window. let l:target_window = winnr('#') if l:target_window == 0 \ || getbufvar(winbufnr(l:target_window), '&filetype') ==# 'netrw' let l:target_window = 0 for l:window in range(1, winnr('$')) if getbufvar(winbufnr(l:window), '&filetype') !=# 'netrw' let l:target_window = l:window break endif endfor endif " Open the new file in the main editing area. if l:target_window > 0 execute l:target_window . 'wincmd w' endif execute 'edit ' . fnameescape(l:path) endfunction augroup netrw_custom_mappings autocmd! autocmd FileType netrw nnoremap a :call NetrwAddPath() augroup END