Skip to content

Instantly share code, notes, and snippets.

@anvanvan
Last active September 1, 2020 13:33
Show Gist options
  • Save anvanvan/c83b4f464e7821b063c36fca6d2ce51c to your computer and use it in GitHub Desktop.
Save anvanvan/c83b4f464e7821b063c36fca6d2ce51c to your computer and use it in GitHub Desktop.
.vimrc for working on remote server
" 1. performance settings
set synmaxcol=300
set noshowmatch
set lazyredraw
set ttyfast
set nocursorline
" leader key
let mapleader = "\<space>"
set timeoutlen=1000 ttimeoutlen=0
" 2. Directories
" Save temporary/backup files not in the local directory, but in your ~/.local
" directory, to keep them out of git repos.
set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
" Keep undo history across sessions by storing it in a file
if has('persistent_undo')
set undodir=~/.vim/undo//
set undofile
set undolevels=1000
set undoreload=10000
endif
augroup tempfile
autocmd!
autocmd BufWritePre /tmp/* setlocal noundofile
augroup END
function! s:DiffWithSaved()
let filetype=&ft
diffthis
vnew | r # | normal! 1Gdd
diffthis
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
command! DiffSaved call s:DiffWithSaved()
" 3. Layout
" Default position for new windows
set splitright splitbelow
" Space above/beside cursor from screen edges
set scrolloff=5
set sidescrolloff=5
" Keep the line number gutter narrow so three digits is cozy.
set numberwidth=2
" display the absolute line number at the line you're on
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave,WinEnter * set nu rnu
autocmd BufLeave,FocusLost,InsertEnter,WinLeave * set nu nornu
augroup END
if has('nvim')
augroup numbertoggle_term
" Disables number lines on terminal buffers
autocmd BufEnter,FocusGained,InsertLeave,WinEnter term://* set nu nornu
autocmd BufLeave,FocusLost,InsertEnter,WinLeave term://* set nonu nornu
autocmd TermOpen * :set nonu nornu
augroup END
end
" Highlight search results
set hlsearch
autocmd cursormoved * set nohlsearch
autocmd cursorhold * set hlsearch
" Incremental search, search as you type
set incsearch
" Ignore case when searching
set ignorecase smartcase
" highlight tabs and trailing spaces
set list listchars=tab:\|\ ,trail:·
set showbreak=↪\ \ \
" folding
set nofoldenable
set foldmethod=syntax
set foldlevelstart=1 " folds with level 1 aren't closed
nmap <leader><leader> za
vmap <leader><leader> za
" visual stuff
let g:indentLine_setColors = 1
let g:indentLine_char = '│'
" better display for messages
set cmdheight=2
set laststatus=2
" theme
syntax enable
set background=dark
colorscheme evening
set linespace=3
" 4. File types
filetype plugin indent on
" Set standard file encoding
set encoding=utf8
" 2 spaces for tabs and shifts
set tabstop=2 softtabstop=2 shiftwidth=2 expandtab smarttab
set autoindent
" treat dash as word
set iskeyword+=-
" Remember last location in file, but not for commit messages.
" see :help last-position-jump
au BufReadPost * if &filetype !~ '^git\c' && line("'\"") > 0 && line("'\"") <= line("$")
\| exe "normal! g`\"" | endif
" php
autocmd FileType php setlocal commentstring=//\ %s
hi phpUseNamespaceSeparator guifg=#808080 guibg=NONE gui=NONE
hi phpClassNamespaceSeparator guifg=#808080 guibg=NONE gui=NONE
syn match phpParentOnly "[()]" contained containedin=phpParent
hi phpParentOnly guifg=#f08080 guibg=NONE gui=NONE
" vimrc
autocmd FileType vim setlocal commentstring=\"\ %s
" 5. Terminal
" allow terminal to run in background
set hidden
" neovim terminal
if has('nvim')
" Terminal go back to normal mode
tnoremap <Esc> <C-\><C-n>
tnoremap :q! <C-\><C-n>:q!<CR>
" still be able to send Esc to terminal programs
tnoremap <C-v><Esc> <Esc>
" windows movement
" tnoremap <a-h> <c-\><c-n><c-w>h
" tnoremap <a-j> <c-\><c-n><c-w>j
" tnoremap <a-k> <c-\><c-n><c-w>k
" tnoremap <a-l> <c-\><c-n><c-w>l
" distinguish terminal cursor
" highlight! link TermCursor Cursor
" highlight! TermCursorNC guibg=white guifg=red ctermbg=15 ctermfg=1
augroup neovim_terminal
autocmd!
" Enter Terminal-mode (insert) automatically
autocmd TermOpen * startinsert
augroup END
endif
" nnoremap <Leader>t :let $WORKING_DIR=expand('%:p:h')<CR>:terminal<CR>a cd $WORKING_DIR<CR>
" nnoremap <leader>g :Tig<cr>
" nnoremap <leader>t :tabnew +terminal<cr>
nnoremap <leader><cr> :terminal ++curwin<cr>
nnoremap <c-g> :!git
au FileType gitcommit execute "normal! O" | startinsert
" 6. Mappings
" folder wide search
" set grepprg=rg\ --vimgrep\ --glob=!tags
nmap <leader>g :grep
" Set // to search the current visual selection
vnoremap * y/\V<C-r>=escape(@",'/\')<CR><CR>N
" Search and Replace
nmap <leader>s :%s///g<Left><Left>
nmap <leader>r *Ncgn
" Make the dot command work as expected in visual mode (via
" https://www.reddit.com/r/vim/comments/3y2mgt/do_you_have_any_minor_customizationsmappings_that/cya0x04)
vnoremap . :norm.<CR>
" copy and paste
" noremap <Leader>y "+y
noremap <leader>p "+p
noremap <leader>d "+d
noremap <leader>y "+y
" noremap 0p "0p
" paste in insert and command mode
imap <C-v> <C-r>+
cmap <C-v> <C-r>+
" delete in insert mode
inoremap <C-d> <Del>
" toggle paste in cmd only
" nnoremap <Leader>i :set invpaste<CR>
" Enable mouse support
set mouse=a
" Set this to the name of your terminal that supports mouse codes.
" Must be one of: xterm, xterm2, netterm, dec, jsbterm, pterm
if !has('nvim')
set ttymouse=xterm2
endif
" Quickly select the text that was just pasted. This allows you to, e.g.,
" indent it after pasting.
noremap gV `[v`]
" Stay in visual mode when indenting. You will never have to run gv after
" performing an indentation.
vnoremap < <gv
vnoremap > >gv
" Make Ctrl-e jump to the end of the current line in the insert mode. This is
" handy when you are in the middle of a line and would like to go to its end
" without switching to the normal mode.
" Map Ctrl-A -> Start of line, Ctrl-E -> End of line
inoremap <C-a> <C-o>0
inoremap <C-e> <C-o>$
" Moving lines up/down
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
" Make cursor move by visual lines instead of file lines (when wrapping)
noremap k gk
noremap j gj
" Mappings to access buffers (don't use "\p" because a
" delay before pressing "p" would accidentally paste).
" \l : list buffers
" \b \f \g : go back/forward/last-used
" \1 \2 \3 : go to buffer 1/2/3 etc
nnoremap <leader>b :buffers<cr>:buffer *
" nnoremap <leader>b :Buffers<cr>
nnoremap <leader>l :ls<cr>
nnoremap <leader>q :bd!<cr>
" nnoremap <leader>q :bp<bar>sp<bar>bn<bar>bd!<CR>
"nnoremap <Leader>g :e#<CR>
nnoremap <leader>t :tabnew<cr>
noremap <leader><bs> :close<cr>
nnoremap <Tab> :tabn<CR>
nnoremap <s-Tab> :tabp<CR>
" Jump back to last edited buffer
nnoremap <C-b> <C-^>
inoremap <C-b> <esc><C-^>
" don't use arrow keys
" inoremap <Up> <NOP>
" inoremap <Down> <NOP>
" inoremap <Left> <NOP>
" inoremap <Right> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
" Use arrow keys to resize panes instead
nnoremap <Left> :vertical resize +1<CR>
nnoremap <Right> :vertical resize -1<CR>
nnoremap <Up> :resize -1<CR>
nnoremap <Down> :resize +1<CR>
" Open splits
nmap <leader>\| :vsplit<cr>
nmap <leader>- :split<cr>
" save
nnoremap <C-s> :w<cr>
inoremap <C-s> <esc>:w<cr>
" Quick edit vimrc
nnoremap <leader>ev :e $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
" abbreviations
iabbrev pp \|>
" finding files
" search down into subfolders
" provides tab-complettion for all file-related tasks
set path=.,**
set wildignore+=**/node_modules/**
set wildignore+=**/.git/**
set wildignore+=**/deps/**
set wildignore+=**/_build/**
nnoremap <leader>f :e **/*
map - :e %:h
" display all matching files when we tab complete
set wildmenu
" MUcomplete
set completeopt+=menuone
set completeopt+=noselect
" set completeopt+=noinsert
set shortmess+=c " Shut off completion messages
set belloff+=ctrlg " If Vim beeps during completion
nmap <leader>1 1gt
nmap <leader>2 2gt
nmap <leader>3 3gt
nmap <leader>4 4gt
nmap <leader>5 5gt
nmap <leader>6 6gt
nmap <leader>7 7gt
nmap <leader>8 8gt
nmap <leader>9 9gt
" quickfix shortcuts
nmap ]q :cnext<cr>
nmap ]Q :clast<cr>
nmap [q :cprev<cr>
nmap [Q :cfirst<cr>
" save as new file in same dir
nnoremap <leader>c :w %:h/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment