Skip to main content

.vimrc

Important:

  • this vimrc works for vim and neovim, but is intended to be used mainly in neovim
  • when running in neovim, this vimrc expects vim-plug

The famous .vimrc:

set nocp " this vimrc is not compatible

""""""""""""""""""""""""""""""""
"   TEMP TRAINING REMAPPINGS   "
""""""""""""""""""""""""""""""""
" stop hitting escape to exit insert mode
"inoremap <esc> <nop>





"""""""""""""""""""""""""
"   PLUGIN MANAGEMENT   "
"""""""""""""""""""""""""
if has('nvim')
    " vim-plug
    call plug#begin()
        " nice color scheme
        Plug 'tomasiser/vim-code-dark'
        " new movement: leap
        Plug 'ggandor/leap.nvim'
        " better repeat
        Plug 'tpope/vim-repeat'
        " snippets, ie enter "test" and it auto-fills a template for a java test
        "Plug 'SirVer/ultisnips'
        " to look into:
        "kevinhwang91/nvim-ufo
        "tpope/vim-surround or machakann/vim-sandwich
        "tpope/vim-fugitive
        "neoclide/coc.nvim
        "lightline
        

    call plug#end()

    " set color scheme
    colorscheme codedark

    " leap load default keybinding
    lua require('leap').set_default_mappings()

    " UltiSnip config
    "let g:UltiSnipsExpandTrigger="<tab>"
    "let g:UltiSnipsJumpForwardTrigger="<C-b>"
    "let g:UltiSnipsJumpBackwardTrigger="<C-z>"
    "let g:UltiSnipsEditSplit="vertical" " split window with :UltiSnipsEdit
    "let g:UltiSnipsSnippetsDir = "~/.config/nvim/ultisnippets" " set snippet directory
    "let g:UltiSnipsSnippetDirectories=["ultisnippets"]
endif






"""""""""""""""
"   IDEAVIM   "
"""""""""""""""
if has('ide')
    set noideadelaymacro
endif





"""""""""""""""
"   OPTIONS   "
"""""""""""""""
"  Pretty
colorscheme codedark                " set color scheme
set number                          " show line numbers
set cursorline                      " show highlight/underline
" set highlight color
hi CursorLine ctermbg=237
syntax on                           " show syntax highlighting
set showmatch                         " shows matching parenthesis
filetype plugin on                  " automatically highlight based on filetype
set listchars=tab:┌─┐,nbsp:○,trail:-,eol:⏎,space:· " characters to use to represent whitespace when they are show (use set list to see this)
set scrolloff=5                       " hard to explain, always keep x lines above or below the cursor visually, except when you hit the top of the document
set incsearch                         " highlight matches (while typing)
set hlsearch                          " highlight matches (occurs after pressing Enter)
set lazyredraw                        " only redraw screen when necessary
set linebreak                         " only wrap text at breakat characters (don't wrap in the middle of a word)
"set breakindent                       " visual indent on lines that have been wrapped, don't think this works with showbreak
" the indent to use for breakindent
set showbreak=⤷\ 
"set termguicolors                     " use truecolor (warning, may make life terrible)



"  Utility
" persist undo to file with 1000 undos per file
set undofile
set undodir=~/.config/nvim/undo
set undolevels=1000
if !has('ide')
    " tabs
    set expandtab                         " tabs are now spaces
    set tabstop=4                         " for above, tabs are 4 spaces
    "set softtabsstop=4                    " 4 spaces are treated as a tab
    set shiftwidth=4                      " how many columns text is shifted with the shift operations (<< or >>)
    set smarttab                          " ?
    set autoindent
    set smartindent                       " autodetect what indentation to use
    filetype indent on                    " load filetype-specfic indent lines
else
    " tabs
    set noexpandtab
endif

set wildmenu                          " autocomplete for command window
set gdefault                          " make /g the default behavior for :%s/search/replace (replace every occurence on every line)
"set clipboard=unnamed                 " allow y/p to system clipboard, use "+y or "+p
set mouse=a                           " use the mouse to visually select text, switch tabs, resize splits
set omnifunc=syntaxcomplete#Complete

" splits
set splitright
set splitbelow

" folding
let g:markdown_folding = 1
let g:markdown_enable_folding = 1

" config
let g:BASH_Ctrl_j = 'off'             " disable default Ctrl+j newline behavior
let mapleader = " "                   " important: see Leader remappings

" Netrw
let g:netrw_banner = 0                " hide netrw banner, press I to temporarily show
let g:netrw_keepdir = 0               " sync netrw directory, and current directory
"let g:netrw_browse_split = 2          " default to vertical split opening files
let g:netrw_altv=1                    " open splits right, not left
let g:netrw_liststyle = 3             " set netrw view style




""""""""""""""""""
"   REMAPPINGS   "
""""""""""""""""""
" Leader
nnoremap <space> <nop>
vnoremap <space> <nop>

" MOVEMENT
"move to the beginning of the line
nnoremap H ^
vnoremap H ^
"move to the end of the line
nnoremap L $
vnoremap L $
"inverse of movements
nnoremap E ge
vnoremap E ge
nnoremap W b
vnoremap W b
"nicer visuals for searches
nnoremap n nzz
nnoremap N Nzz

"make movement more convenient on german keyboard
nnoremap ö ;
nnoremap Ö ,

" remap scrolling
"override previous behavior
nnoremap <C-j> <S-j>
nnoremap <C-k> <S-k>
vnoremap <C-j> <S-j>
vnoremap <C-k> <S-k>
"set wanted behavior
nnoremap <S-j> <C-e>
nnoremap <S-k> <C-y>
vnoremap <S-j> <C-e>
vnoremap <S-k> <C-y>

" UTILITY
" alternatives to escape
inoremap jk <esc>
inoremap kj <esc>
inoremap jjj <esc>
inoremap kkk <esc>

" more ways to quit :D
"write/quit remaps
nnoremap <leader>q :q<cr>
nnoremap <leader>Q :q!<cr>
nnoremap <leader>w :w<cr>
nnoremap <leader>W :wq<cr>

" easier copy/paste
nnoremap <leader>y "+y
nnoremap <leader>p "+p
nnoremap <leader>P "+P
nnoremap <leader>d "+d
vnoremap <leader>y "+y
vnoremap <leader>p "+p
vnoremap <leader>P "+P
vnoremap <leader>d "+d

" tab movement
nnoremap <leader>t gt
nnoremap <leader>T gT

" easier split switching
nnoremap <leader>j <C-w>j
nnoremap <leader>k <C-w>k
nnoremap <leader>h <C-w>h
nnoremap <leader>l <C-w>l

" code utils
if has('ide')
    nnoremap <leader>c :action CommentByLineComment<cr>
    vnoremap <leader>c :action CommentByLineComment<cr><esc>
else
    nnoremap <leader>c :LineComment<cr>
    vnoremap <leader>c :LineComment<cr>
endif

" file Browser
nnoremap <leader>b :Lexplore<cr>

" autocomplete
"function! ContextAwareTab()
"   if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
"      return "\<tab>"
"   else
"      return "\<C-N>"
"   endif
"endfunction
"inoremap <tab> <C-R>=ContextAwareTab()<cr>
if has('ide')
    command! SourceVimrc :source ~/.ideavimrc

    " no support for pumvisible
else
    inoremap <expr> j pumvisible() ? "\<C-n>" : 'j'
    inoremap <expr> k pumvisible() ? "\<C-p>" : 'k'
    inoremap <expr> <C-M> pumvisible() ? "\<C-y>" : '<cr>'
endif

" VISUAL?
"hide previous search results
nnoremap <leader>/ :noh<cr>

"toggle list character
nnoremap <leader>s :set list!<cr>

"toggle underline
nnoremap <leader>u :hi CursorLine cterm=underline<cr>
nnoremap <leader>U :hi CursorLine cterm=NONE<cr>

"center screen after search (https://stackoverflow.com/questions/39892498/center-cursor-position-after-search-in-vim)
cnoremap <silent><expr> <enter> index(['/', '?'], getcmdtype()) >= 0 ? '<enter>zz' : '<enter>'

" EDITING
" easier changing :D
"change whole line
nnoremap cc 0C

" clear line
nnoremap - 0D

" OPERATOR
"to end of sentence
onoremap . t.
"to end of sentence part
onoremap , t,
"to end of line
onoremap ; t;

" TERMINAL
tnoremap jk <C-\><C-N>
tnoremap kj <C-\><C-N>



""""""""""""""""
"   DIGRAPHS   "
""""""""""""""""
dig _i 9432   " info icon: ⓘ
dig _w 9888   " warning icon: ⚠
dig _e 9940   " error sign: ⛔ (actually no entry)
dig _o 9989   " ok: ✅
dig _b 10060  " not ok: ❌
dig cr 128308 " red 🔴
dig co 128992 " orange 🟠
dig ck 9899   " black ⚫
dig cw 9898   " white ⚪
dig cp 128995 " purple 🟣
dig cg 128994 " green 🟢
dig cy 128993 " yellow 🟡
dig cu 128309 " blue 🔵
dig cn 128996 " brown 🟤



""""""""""""""""
"   COMMANDS   "
""""""""""""""""
" more ways to quit :D
command Q q!
command Exit q

" edit .vimrc
command EditVimrc vsplit ~/.vimrc
command EditVimrcTab tabe ~/.vimrc
"command EditVim vsplit $MYVIMRC " I never use this, it's just annoying

" comments
command! LineComment :execute "'<,'>norm gI//" | :execute "'<,'>s/\/\/\/\///e"

" clear mappings
function RemoveMappings()
    mapclear | mapclear <buffer> | mapclear! | mapclear! <buffer>
endfunction
command RemoveMappings call RemoveMappings()

" source .vimrc
command SourceVimrc source $MYVIMRC
command WriteAndSourceVimrc w | source $MYVIMRC
command SourceVimrcWithRemove call RemoveMappings() | source $MYVIMRC

" open helpful file
command Helpful vsplit ~/helpful
" open notes for x
command! -nargs=1 Notes :vsp ~/dev/worknotes/<args>.md
" help for IntelliJ, which refuses to open new files unless they exist -_-
command! -nargs=1 PrepNotes !touch ~/dev/worknotes/<args>.md

" open a notes file
" moved because ideavim
"command! -nargs=1 Notes :vsp ~/.config/nvim/notes/<args><cr>

" open terminal
if has('nvim')
    command Terminal vsplit term://zsh
    command TerminalTab tabe term://zsh
else
    command Terminal vert term
    command TerminalTab tab ter
endif





""""""""""""""""""""
"   AUTOCOMMANDS   "
""""""""""""""""""""
" Automatically save and reload views (see fold)
" TODO this isn't working for some files, and it's annoying
"au BufWritePost,BufLeave,WinLeave ?* mkview
"au BufWinEnter ?* silent loadview





""""""""""""""""""""""""""
"   GROUPS / GROUPINGS   "
""""""""""""""""""""""""""
" Markdown
function! SetMarkdownHeader(level)
    " Check if the parameter is a positive number
    if a:level < 1
        echo "Error: The level must be a positive number."
        return
    endif

    " Get the current line
    let l:line = getline('.')

    " Remove leading pound symbols
    let l:line = substitute(l:line, '^\s*#\+\s*', '', '')

    " Add the specified number of pound symbols
    let l:new_line = repeat('#', a:level) . ' ' . l:line

    " Set the new line
    call setline('.', l:new_line)
endfunction
function! MarkdownSettings()
    nnoremap <buffer> <leader>1 :call SetMarkdownHeader(1)<cr>
    nnoremap <buffer> <leader>2 :call SetMarkdownHeader(2)<cr>
    nnoremap <buffer> <leader>3 :call SetMarkdownHeader(3)<cr>
    nnoremap <buffer> <leader>4 :call SetMarkdownHeader(4)<cr>
    nnoremap <buffer> <leader>5 :call SetMarkdownHeader(5)<cr>
endfunction
augroup markdown
    autocmd!
    autocmd FileType markdown call MarkdownSettings()
augroup END

" TeX (LaTeX)
let g:tex_flavor = "latex"  " set .tex files to latex or '.tex' type, instead of plaintex
function! TexSettings()
    nnoremap <buffer> <leader>j /section<cr>
    nnoremap <buffer> <leader>k ?section<cr>

    command! Compile w | echom system("pdflatex -interaction=nonstopmode " . expand("%:p") . " | grep -A 1 !")
    command! CompileDebug w | !pdflatex -interaction=nonstopmode %:p
    nnoremap <buffer> <leader>c :Compile<cr>
    nnoremap <buffer> <leader>C :Compile<cr>:Compile<cr>

    nnoremap <buffer> <leader># a\comment{}<esc>i
    vnoremap <buffer> <leader># <esc>`>a}<esc>`<i\comment{<esc>
    nnoremap <buffer> <leader>n a\todo{}<esc>i
    vnoremap <buffer> <leader>n <esc>`>a}<esc>`<i\todo{<esc>

    command! -nargs=1 Progress echo wordcount().words - <f-args>

    function! T1()
        let fig = "\\being{enumerate}\n    \\item \n\\end{enumerate}"
        let @" = fig
    endfunction
    function! T2()
        let fig = "%\\newpage\n\\begin{figure}[!h]\n    \\includegraphics[width=\\linewidth]{file.ext}\n    \\caption{PLACEHOLDER}\n    \\label{fig:PLACEHOLDER}\n\\end{figure}"
        let @" = fig
    endfunction
    function! T3()
        let fig = "\\lstset{escapechar=!,style=<style>}\n\\begin{lstlisting}[caption={<caption>}]\n<code>\n\\end{lstlisting}"
        let @" = fig
    endfunction
    nnoremap <buffer> <leader>0 :echo "1: List, 2: Figure, 3: Listing"<cr>
    nnoremap <buffer> <leader>1 o<esc>:call T1()<cr>pjA
    nnoremap <buffer> <leader>2 o<esc>:call T2()<cr>pj$hvi{
    nnoremap <buffer> <leader>3 o<esc>:call T3()<cr>pf<vf>
endfunction
augroup tex
    autocmd!
    autocmd FileType tex call TexSettings()
augroup END

function! NetrwMapping()
    " netrw mappings
endfunction
augroup netrw_mapping
  autocmd!
  autocmd filetype netrw call NetrwMapping()
augroup END


" NeoVim only settings
if has('nvim')

    " Terminal Buffer (only needs adjustment in NeoVim)
    function! TerminalSettings()
        setlocal nonumber
        normal a
    endfunction
    augroup terminal
        autocmd!
        autocmd TermOpen * call TerminalSettings()
    augroup END

endif