Skip to content

Instantly share code, notes, and snippets.

@souvikhaldar
Created December 12, 2021 07:41
Show Gist options
  • Save souvikhaldar/5dbfac7e93dcb3018c5ade677f9da9e1 to your computer and use it in GitHub Desktop.
Save souvikhaldar/5dbfac7e93dcb3018c5ade677f9da9e1 to your computer and use it in GitHub Desktop.
vimrc
" install vim-plug
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
" Plug installed plugins
call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
" Plug 'govim/govim'
Plug 'preservim/nerdtree'
" Plug 'vim-airline/vim-airline'
Plug 'itchyny/lightline.vim'
Plug 'tpope/vim-fugitive'
Plug 'terryma/vim-multiple-cursors'
Plug 'vim/killersheep'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'AndrewRadev/splitjoin.vim'
Plug 'jiangmiao/auto-pairs'
Plug 'morhetz/gruvbox'
Plug 'skywind3000/asyncrun.vim'
Plug 'vimwiki/vimwiki'
Plug 'suan/vim-instant-markdown', {'for': 'markdown'}
Plug 'itchyny/vim-cursorword'
Plug 'python-mode/python-mode', { 'for': 'python', 'branch': 'develop' }
Plug 'sonph/onehalf', { 'rtp': 'vim' }
Plug 'rust-lang/rust.vim'
Plug 'shumphrey/fugitive-gitlab.vim'
Plug 'tpope/vim-rhubarb'
Plug 'nvim-lua/plenary.nvim'
call plug#end()
"------------------------------------------------------------------------------
set t_Co=256
"set colorcolumn=80
" gruvbox related changes
"colorscheme gruvbox
colorscheme onehalfdark
if exists('+termguicolors')
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
" lightline
let g:lightline = { 'colorscheme': 'onehalfdark' }
set encoding=UTF-8
" Neovim terminal coloring can be enabled as by adding the following above the plugin includes:
let g:doom_one_terminal_colors = v:true
" vim hardcodes background color erase even if the terminfo file does
" not contain bce (not to mention that libvte based terminals
" incorrectly contain bce in their terminfo files). This causes
" incorrect background rendering when using a color theme with a
" background color.
let &t_ut=''
" general changes
nnoremap ; :
nnoremap : ;
let mapleader = "'"
set background=dark
" toggle between tabs
nnoremap <Leader>tn :tabn<CR>
nnoremap <Leader>tp :tabp<CR>
"use <ctrl>+j/k/h/l to switch the right direction just like you use the j/k/h/l to move the cursor
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
" from missing semester class of MIT
set nocompatible
filetype plugin on
" Turn on syntax highlighting.
syntax on
" Disable the default Vim startup message.
set shortmess+=I
" Show line numbers.
set number
set relativenumber
" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2
" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can't backspace before the insertion point set with 'i'.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start
" By default, Vim doesn't let you hide a buffer (i.e. have a buffer that isn't
" shown in any window) that has unsaved changes. This is to prevent you from
" forgetting about unsaved changes and then quitting e.g. via `:qa!`. We find
" hidden buffers helpful enough to disable this protection. See `:help hidden`
" for more information on this.
set hidden
" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase
" Enable searching as you type, rather than waiting till you press enter.
set incsearch
" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " 'Q' in normal mode enters Ex mode. You almost never want this.
" Disable audible bell because it's annoying.
set noerrorbells visualbell t_vb=
" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a
nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>
" ...and in insert mode
inoremap <Left> <ESC>:echoe "Use h"<CR>
inoremap <Right> <ESC>:echoe "Use l"<CR>
inoremap <Up> <ESC>:echoe "Use k"<CR>
inoremap <Down> <ESC>:echoe "Use j"<CR>
"-----------------------------------------------------------------------------
" FZF related
" Fuzzy search and open file
nnoremap <Leader>o :FZF<CR>
" Fuzzy search on output of ripgrep
nnoremap <Leader>rg :Rg<CR>
"------------------------------------------------------------------------------
" golang specific settings
let g:go_fmt_command = "goimports"
let g:go_auto_type_info = 1
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
"------------------------------------------------------------------------------
" NERDTree plugin specific commands
:nnoremap <C-g> :NERDTreeToggle<CR>
" automatically close a tab if the only remaining window is NerdTree
"there is some error in the following line so commenting it out
" autocmd bufenter * if (winnr(“$”) == 1 && exists(“b:NERDTreeType”) && b:NERDTreeType == “primary”) | q | endif
"
" Automatically delete the buffer of the file you just deleted with NerdTree
let NERDTreeAutoDeleteBuffer = 1
" Making it preetier
let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1
"------------------------------------------------------------------------------
" vim airline specific settings
let g:airline_powerline_fonts = 1
"let g:airline#extensions#tabline#enabled = 1
" show time
"let g:airline_section_b = '%{strftime("%H:%M")}'
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
"------------------------------------------------------------------------------
" customize the autocomplete
au filetype go inoremap <buffer> . .<C-x><C-o>
:set completeopt=longest,menuone
:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
" The above mapping will change the behavior of the <Enter> key when the popup
" menu is visible. In that case the Enter key will simply select the
" highlighted menu item, just as <C-Y> does.
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
\ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
\ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
nnoremap <Leader>mc :Gvdiffsplit!<CR>
nnoremap <Leader>gf :GFiles!<CR>
" LATEX
nnoremap <Leader>cp :AsyncRun latexmk -pvc -pdf %<CR>
" vimwiki
let g:vimwiki_list = [{'path': '~/vimwiki/',
\ 'syntax': 'markdown',
\ 'ext': '.md',
\'auto_diary_index': 1}]
" Spell-check Markdown files and Git Commit Messages
autocmd FileType markdown setlocal spell
autocmd FileType gitcommit setlocal spell
autocmd FileType tex setlocal spell
" Enable dictionary auto-completion in Markdown files and Git Commit Messages
autocmd FileType markdown setlocal complete+=kspell
autocmd FileType gitcommit setlocal complete+=kspell
" edit vimrc file
nnoremap <leader>vc :tabnew ~/.vimrc<CR>
" copy the selected characters to system register
" it is quivalent to system's ctrl+c
nnoremap <C-c> "*y<CR>
" pasting screenshots
nnoremap <silent> <leader>p :call SaveFile()<cr>
function! SaveFile() abort
let targets = filter(
\ systemlist('xclip -selection clipboard -t TARGETS -o'),
\ 'v:val =~# ''image''')
if empty(targets) | return | endif
let outdir = expand('%:p:h') . '/img'
if !isdirectory(outdir)
call mkdir(outdir)
endif
let mimetype = targets[0]
let extension = split(mimetype, '/')[-1]
let tmpfile = outdir . '/savefile_tmp.' . extension
call system(printf('xclip -selection clipboard -t %s -o > %s',
\ mimetype, tmpfile))
let cnt = 0
let filename = outdir . '/image' . cnt . '.' . extension
while filereadable(filename)
call system('diff ' . tmpfile . ' ' . filename)
if !v:shell_error
call delete(tmpfile)
break
endif
let cnt += 1
let filename = outdir . '/image' . cnt . '.' . extension
endwhile
if filereadable(tmpfile)
call rename(tmpfile, filename)
endif
let @* = '![](' . fnamemodify(filename, ':.') . ')'
normal! "*p
endfunction
" markdown preview
" Not allowing preview to pop up automatically, instead preview
" needs to be invoked by :InstantMarkDownPreview and :InstantMarkdownStop
let g:instant_markdown_autostart = 0
" lightline configuration
let g:lightline = {
\ 'active': {
\ 'left': [ [ 'mode', 'paste' ],
\ [ 'gitbranch', 'readonly', 'filename', 'modified' ] ]
\ },
\ 'component_function': {
\ 'gitbranch': 'FugitiveHead'
\ },
\ }
"------------------------------------------------------------------------------
" govim related
" gd goes to def, but I want to go to def in spits if I want
" NOTE: NOT USING GOVIM CURRENTLY HENCE COMMENTED IT OUT
"
"nnoremap <Leader>ds :set switchbuf=split \| GOVIMGoToDef<CR>
"nnoremap <Leader>dv :set switchbuf=vsplit \| GOVIMGoToDef<CR>
" automatically format .sql file
" Prerequisite: pip install sqlparse
nnoremap <Leader>fs :%!sqlformat --reindent --keywords upper --identifiers lower -<CR>
" Terminal Function
" https://www.reddit.com/r/vim/comments/8n5bzs/using_neovim_is_there_a_way_to_display_a_terminal/dzt3fix
let g:term_buf = 0
let g:term_win = 0
function! TermToggle(height)
if win_gotoid(g:term_win)
hide
else
botright new
exec "resize " . a:height
try
exec "buffer " . g:term_buf
catch
call termopen($SHELL, {"detach": 0})
let g:term_buf = bufnr("")
set nonumber
set norelativenumber
set signcolumn=no
endtry
startinsert!
let g:term_win = win_getid()
endif
endfunction
" Toggle terminal on/off (neovim)
"nnoremap <A-t> :call TermToggle(12)<CR>
nnoremap <Leader>t :call TermToggle(12)<CR>
inoremap <A-t> <Esc>:call TermToggle(12)<CR>
tnoremap <A-t> <C-\><C-n>:call TermToggle(12)<CR>
" Terminal go back to normal mode
tnoremap <Esc> <C-\><C-n>
tnoremap :q! <C-\><C-n>:q!<CR>
" Clear the highlights
nnoremap <Leader><space> :noh<cr>
" format the current JSON file
nnoremap <Leader>fj :%!jq<CR>
" joplin sync
nnoremap <Leader>js :AsyncRun joplin --profile ~/.config/joplin-desktop/ sync<CR>
" setup mapping to call :LazyGit
nnoremap <Leader>gg :Gitit
command! -nargs=1 Gitit exec "silent AsyncRun! git add .&& git commit -m '".<q-args>."'"." && git push origin HEAD"
" for enabling GBrowse for private gitlab instance
let g:fugitive_gitlab_domains = {'ssh://my-ssh.gitlab.com': 'https://my.gitlab.com'}
" Configure VIM to run chezmoi apply whenever you save a dotfile
autocmd BufWritePost ~/.local/share/chezmoi/* ! chezmoi apply --source-path "%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment