Skip to content

Instantly share code, notes, and snippets.

@yosugi
Created March 8, 2019 08:42
Show Gist options
  • Select an option

  • Save yosugi/bba3439492e98ebd964807bfebfe0829 to your computer and use it in GitHub Desktop.

Select an option

Save yosugi/bba3439492e98ebd964807bfebfe0829 to your computer and use it in GitHub Desktop.
"" options {{{1 ------------------------------------------------------------------------------------
" indent
set autoindent
set expandtab
set shiftwidth=4
set smarttab
set tabstop=4
" search
set hlsearch
set ignorecase
set incsearch
set smartcase
" parent
set matchtime=1
set showmatch
" etc
set ambiwidth=double
set display=lastline
set hidden
set laststatus=2
set modeline
set showcmd
set visualbell
set wildmenu
" dirs
let s:tmpdir=$HOME . '/tmp'
if !isdirectory(s:tmpdir)
let s:tmpdir='/tmp'
end
if isdirectory(s:tmpdir)
let &backupdir=s:tmpdir
let &directory=s:tmpdir
let &undodir=s:tmpdir
else
set nobackup
set noswapfile
set noundofile
end
"" display {{{1 ------------------------------------------------------------------------------------
syntax on
set background=dark
set number
highlight LineNr ctermfg=DarkGray guifg=Grey guibg=Grey90
" cf. https://stackoverflow.com/questions/10746750/set-vim-bracket-highlighting-colors/10746829
"hi MatchParen cterm=bold ctermfg=none ctermbg=Magenta
hi MatchParen cterm=bold,reverse ctermbg=none ctermfg=Magenta
"colorscheme elflord
"colorscheme koehler
" show em space
highlight JpSpace cterm=underline ctermfg=Blue guifg=Blue
autocmd BufRead,BufNew * match JpSpace / /
" show special chars
set list
set listchars=tab:>-,trail:-,nbsp:-,extends:>,precedes:<,
set statusline=%<%f\ %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,%c%V%8P
"" key bindings {{{1 -------------------------------------------------------------------------------
" select to end of line
nnoremap Y v$
" do not jump
" cf. https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump
nnoremap * *``
nnoremap # #``
" CTRL-C and CTRL-Insert are Copy (from mswin.vim)
vnoremap <C-C> "+y
vnoremap <C-Insert> "+y
" leave insert mode & save
inoremap <silent> jj <ESC>:<C-u>up<CR>
" clear search highlight
nnoremap <silent> <C-L> :noh<CR>:redraw<CR>:echo ""<CR>
" https://www.kickbase.net/entry/vim-basic-keymap03
nnoremap <silent> <C-L> :<C-U>nohlsearch<CR><C-L>
" sequential paste
vnoremap p "0p
" Use CTRL-Q to do what CTRL-V used to do
nnoremap <C-Q> <C-V>
" jump last modified line
nnoremap gc `.
" select buffer
nnoremap gb :ls<CR>:b
" recent file
" cf. https://qiita.com/todashuta/items/1362654c6276e5b69abc
nnoremap go :<C-u>$/ oldfiles<Home>browse filter /
nnoremap gO go
" wqa
"nnoremap <silent> ZZ :<C-U>xa<CR>
"nnoremap <silent> ZQ :<C-U>qa!<CR>
"" prefix-key {{{1 ---------------------------------------------------------------------------------
nnoremap [prefix] <nop>
nmap <Space> [prefix]
nnoremap <silent>[prefix]w :<C-u>up<CR>
nnoremap <silent>[prefix]r :e! %<CR>
" switch alternative buffer
nnoremap [prefix]<Space> <c-^>
" paste mode
"autocmd InsertLeave * setlocal nopaste
nnoremap [prefix]pi :setlocal paste<CR>i
nnoremap [prefix]po :setlocal paste<CR>o
nnoremap [prefix]pt :setlocal paste! paste?<CR>
augroup PasteInsert
autocmd!
autocmd InsertLeave * setlocal nopaste
augroup END
" very magic search
" cf. https://www.kickbase.net/entry/vim-basic-keymap04
nnoremap [prefix]/ /\v
nnoremap [prefix]? ?\v
" grep in buffers or arguments
nnoremap [prefix]gb :cexpr []<CR>:bufdo vimgrepadd // %<Left><Left><Left>
nnoremap [prefix]ga :cexpr []<CR>:argdo vimgrepadd // %<Left><Left><Left>
"" pairs {{{1 -------------------------------------------------------------------------------
" inspired
" [tpope/vim\-unimpaired: unimpaired\.vim: Pairs of handy bracket mappings](https://github.com/tpope/vim-unimpaired)
" quickfix
nnoremap <silent> [q :keepalt cprevious<CR>
nnoremap <silent> ]q :keepalt cnext<CR>
nnoremap <silent> [Q :keepalt cfirst<CR>
nnoremap <silent> ]Q :keepalt clast<CR>
" buffers
nnoremap <silent>[b :keepalt bprev<CR>
nnoremap <silent>]b :keepalt bnext<CR>
nnoremap <silent>[B :keepalt bfirst<CR>
nnoremap <silent>]B :keepalt blast<CR>
" argument list
nnoremap <silent>[a :keepalt prev<CR>
nnoremap <silent>]a :keepalt next<CR>
nnoremap <silent>[A :keepalt first<CR>
nnoremap <silent>]A :keepalt last<CR>
" change list
nnoremap <silent> [c g;
nnoremap <silent> ]c g,
nnoremap <silent> [C 999g;
nnoremap <silent> ]C 999g,
" tagstack
nnoremap <silent>[s :keepalt pop<CR>
nnoremap <silent>]s :keepalt tag<CR>
" tags match list
nnoremap <silent>[m :keepalt tprev<CR>
nnoremap <silent>]m :keepalt tnext<CR>
nnoremap <silent>[M :keepalt tfirst<CR>
nnoremap <silent>]M :keepalt tlast<CR>
" display number
nnoremap <silent>[n :setlocal number<CR>
nnoremap <silent>]n :setlocal nonumber<CR>
nnoremap <silent>[N :bufdo :setlocal number<CR>
nnoremap <silent>]N :bufdo :setlocal nonumber<CR>
" display cursol line
nnoremap <silent>[l :setlocal cursorline<CR>
nnoremap <silent>]l :setlocal nocursorline<CR>
" wrap
nnoremap <silent>[w :setlocal wrap<CR>
nnoremap <silent>]w :setlocal nowrap<CR>
"" commands {{{1 ----------------------------------------------------------------------------------
" json
" cf. https://qiita.com/tekkoc/items/324d736f68b0f27680b8
function! s:Jq(...)
setlocal filetype=json
syntax on
setlocal foldmethod=syntax
setlocal foldlevel=10
if 0 == a:0
let l:arg = "."
else
let l:arg = a:1
endif
execute "%! jq \"" . l:arg . "\""
endfunction
command! -nargs=? Jq call s:Jq(<f-args>)
" grep in buffers or arguments
command! -nargs=1 GB cexpr [] | bufdo vimgrepadd /\C<args>/ %
command! -nargs=1 GBI cexpr [] | bufdo vimgrepadd /\c<args>/ %
command! -nargs=1 GA cexpr [] | argdo vimgrepadd /\C<args>/ %
command! -nargs=1 GAI cexpr [] | argdo vimgrepadd /\c<args>/ %
"" terminal {{{1 -----------------------------------------------------------------------------------
" cf. http://blog-sk.com/vim/neovim-settings/
" neovim terminal mapping
if has('nvim')
" launch terminal on new tab
nnoremap tc :tabe<CR>:terminal<CR>
" to normal mode
tnoremap <C-o> <C-\><C-n>
endif
"" tab page {{{1 -----------------------------------------------------------------------------------
" cf. https://qiita.com/wadako111/items/755e753677dd72d8036d
if v:version >= 700 || has('nvim')
nnoremap t <Nop>
" Tab jump
for n in range(1, 9)
execute 'nnoremap <silent> t'.n ':<C-u>tabnext'.n.'<CR>'
endfor
" crate new tab right end
nnoremap <silent>tw :tablast<bar> :tabnew<CR>
nnoremap <silent>tx :tabclose<CR>
nnoremap <silent>tn :tabnext<CR>
nnoremap <silent>tp :tabprevious<CR>
" delete other tab
nnoremap <silent>to :tabonly<CR>
" copy current tab
nnoremap <silent>ts :tab split<CR>
endif
"" autocmd {{{1 ------------------------------------------------------------------------------------
autocmd FileType * setlocal formatoptions-=ro
autocmd BufRead,BufNewFile *.ts set filetype=javascript
" cf. https://mask.hatenadiary.com/entry/2015/06/03/153911
autocmd BufNewFile,BufRead *.twig set filetype=htmljinja
augroup GolangSettings
autocmd!
autocmd filetype go setlocal listchars=tab:\ \ ,trail:-,nbsp:-,extends:>,precedes:<,
autocmd filetype go setlocal noexpandtab makeprg=gofmt\ -w\ %
augroup END
"" Quickfix {{{1 -----------------------------------------------------------------------------------
" cf. https://thinca.hatenablog.com/entry/20130708/1373210009
autocmd filetype qf noremap <buffer> <CR> <CR>zz
autocmd filetype qf setlocal statusline=%t%{exists('w:quickfix_title')?\ '\ '.w:quickfix_title\ :\ ''}\ %=%l/%L
autocmd filetype qf noremap <buffer> J j<CR>zt<C-w>p
autocmd filetype qf noremap <buffer> K k<CR>zt<C-w>p
autocmd QuickFixCmdPost *grep*,make cwindow
if executable('rg')
set grepprg=rg\ --vimgrep\ --no-heading
set grepformat=%f:%l:%c:%m,%f:%l:%m
elseif executable('ag')
set grepprg=ag\ --nogroup\ -iS
set grepformat=%f:%l:%m
endif
" find files with quickfix
" cf. https://www.reddit.com/r/vim/comments/8p51l6/get_result_of_find_command_up_in_a_quickfix/
function! FindFilesToLocationList(...)
let l:pattern = '.*'
if a:0 > 0
let l:pattern = a:1
endif
let l:command = "find . -type d -name .git -prune -o -type f -print | grep '" . l:pattern . "' | xargs -I{} awk '$1 != \"\" && n < 1 {print FILENAME\":1:1:\"$0; n++}' {}"
if executable('fd')
let l:command = "fd --type f --no-ignore '" . l:pattern . "' | xargs -I{} awk '$1 != \"\" && n < 1 {print FILENAME\":1:1:\"$0; n++}' {}"
endif
cgetexpr system(l:command)
let w:quickfix_title = l:command
echo l:command
endfunction
command! -nargs=? FF :call FindFilesToLocationList(<f-args>) | :cw
function! FindGitFilesToLocationList(...)
let l:pattern = '.*'
if a:0 > 0
let l:pattern = a:1
endif
let l:command = "find . -type d -name .git -prune -o -type f -print | grep '" . l:pattern . "' | xargs -I{} awk '$1 != \"\" && n < 1 {print FILENAME\":1:1:\"$0; n++}' {}"
if executable('fd')
let l:command = "fd --type f '" . l:pattern . "' | xargs -I{} awk '$1 != \"\" && n < 1 {print FILENAME\":1:1:\"$0; n++}' {}"
elseif executable('rg')
let l:command = "rg --files | grep '" . l:pattern . "' | xargs -I{} awk '$1 != \"\" && n < 1 {print FILENAME\":1:1:\"$0; n++}' {}"
endif
cgetexpr system(l:command)
let w:quickfix_title = l:command
endfunction
command! -nargs=? FG :call FindGitFilesToLocationList(<f-args>) | :cw
"" plugins {{{1 ------------------------------------------------------------------------------------
" install
" mkdir -p ~/.vim/pack/plugins/start/ && cd $_
" cat << EOS | xargs -n1 -P5 git clone
" https://github.com/junegunn/fzf.git
" https://github.com/junegunn/fzf.vim.git
" https://github.com/tpope/vim-abolish.git
" https://github.com/tpope/vim-surround.git
" EOS
"
" update
" cd ~/.vim/pack/plugins/start/
" ls | xargs -P5 -I{} git -C {} pull
if has('nvim') && isdirectory($HOME . '/.vim/pack/plugins/start/')
set runtimepath+=~/.vim/pack/plugins/start/*
endif
if isdirectory($HOME . '/.vim/pack/plugins/start/fzf.vim')
" fzf.vim
nnoremap [prefix]ff :Files<CR>
nnoremap [prefix]fg :GFiles<CR>
nnoremap [prefix]fG :GFiles?<CR>
nnoremap [prefix]b :Buffers<CR>
nnoremap [prefix]fb :Buffers<CR>
nnoremap [prefix]fa :Ag<CR>
nnoremap [prefix]fr :Rg<CR>
nnoremap [prefix]o :History<CR>
nnoremap [prefix]fh :History<CR>
nnoremap [prefix]f: :History:<CR>
nnoremap [prefix]f/ :History/<CR>
endif
"" abbrev {{{1 ---------------------------------------------------------------------------------------
" current datetime
" cf. https://vim.fandom.com/wiki/Insert_current_date_or_time
iabbrev <expr> dtf. strftime("%Y-%m-%d %H:%M:%S")
iabbrev <expr> dtm. strftime("%Y-%m-%d %H:%M")
iabbrev <expr> dtn. strftime("%Y%m%d%H%M%S")
iabbrev <expr> dt. strftime("%Y-%m-%d")
iabbrev <expr> ts. strftime("%H:%M:%S")
iabbrev <expr> tm. strftime("%H:%M")
" current dir
" cf. http://vimblog.hatenablog.com/entry/vimrc_key_mapping_examples
" cf. [自分のvimでやってきたことのまとめ \- Qiita](https://qiita.com/ykyk1218/items/ab1c89c4eb6a2f90333a)
cabbrev <expr> %% fnameescape(expand('%:h'))
cabbrev ggb. let g:gitgutter_diff_base = 'HEAD'<Left>
"" local settings {{{1 -----------------------------------------------------------------------------
if filereadable($HOME . "/.vimrc_local")
source ~/.vimrc_local
endif
"" }}}1
"" modeline
" cf. https://qiita.com/naoty_k/items/674787bc2d9885f81a0b
" vim: foldmethod=marker
" vim: foldcolumn=0
" vim: foldlevel=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment