Skip to content

Instantly share code, notes, and snippets.

@patrickleweryharris
Last active November 28, 2024 12:30
Show Gist options
  • Save patrickleweryharris/8b017cd8ba4b0e23fd78244e2a4e68f0 to your computer and use it in GitHub Desktop.
Save patrickleweryharris/8b017cd8ba4b0e23fd78244e2a4e68f0 to your computer and use it in GitHub Desktop.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Line numbers
set number
" Search highlighting
set hlsearch
" Sets how many lines of history VIM has to remember
set history=500
" Enable filetype plugins
filetype on
filetype plugin on
filetype indent on
" Set to auto read when a file is changed from the outside
set autoread
" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
" Fast saving
nmap <leader>w :w!<cr>
" Search dir for tags
set tags=./tags,**5/tags,tags;~
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM user interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set 7 lines to the cursor - when moving vertically using j/k
set so=7
" Avoid garbled characters in Chinese language windows OS
let $LANG='en'
set langmenu=en
" Turn on the Wild menu
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc
if has("win16") || has("win32")
set wildignore+=.git\*,.hg\*,.svn\*
else
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
endif
"Always show current position
set ruler
" A buffer becomes hidden when it is abandoned
set hid
" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
" Ignore case when searching
set ignorecase
" When searching try to be smart about cases
set smartcase
" Makes search act like search in modern browsers
set incsearch
set shortmess-=S
" For regular expressions turn magic on
set magic
" Show matching brackets when text indicator is over them
set showmatch
" How many tenths of a second to blink when matching brackets
set mat=2
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500
" Set textwidth for markdown
au BufRead,BufNewFile *.md setlocal textwidth=80
" Add a bit extra margin to the left
set foldcolumn=1
syntax enable
" Set utf8 as standard encoding
set encoding=utf-8
set t_Co=256
set fillchars+=stl:\ ,stlnc:\
set term=xterm-256color
set termencoding=utf-8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files, backups and undo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Protect changes between writes. Default values of
" updatecount (200 keystrokes) and updatetime
" (4 seconds) are fine
set swapfile
set directory^=~/tmp/swap//
" protect against crash-during-write
set writebackup
" but do not persist backup after successful write
set nobackup
" use rename-and-write-new method whenever safe
set backupcopy=auto
" patch required to honor double slash at end
if has("patch-8.1.0251")
" consolidate the writebackups -- not a big
" deal either way, since they usually get deleted
set backupdir^=~/tmp/backup//
end
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text, tab and indent related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Use spaces instead of tabs
set expandtab
" Be smart when using tabs ;)
set smarttab
" 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4
" Linebreak on 500 characters
set lbr
set tw=500
set ai "Auto indent
set si "Smart indent
set wrap "Wrap lines
au BufNewFile,BufRead *.py
\ set tabstop=4 |
\ set softtabstop=4 |
\ set shiftwidth=4 |
\ set textwidth=120 |
\ set fileformat=unix |
""""""""""""""""""""""""""""""
" => Visual mode related
""""""""""""""""""""""""""""""
" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs, windows and buffers
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
map <space> /
map <c-space> ?
" Disable highlight when <leader><cr> is pressed
map <silent> <leader><cr> :noh<cr>
" Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
" Close the current buffer
map <leader>bd :Bclose<cr>:tabclose<cr>gT
" Close all the buffers
map <leader>ba :bufdo bd<cr>
map <leader>l :bnext<cr>
map <leader>k :bprevious<cr>
" Useful mappings for managing tabs
map <leader>tn :tabnew<cr>
map <leader>to :tabonly<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
map <leader>t<leader> :tabnext
" Let 'tl' toggle between this and the last accessed tab
let g:lasttab = 1
nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()
" Opens a new tab with the current buffer's path
" Super useful when editing files in the same directory
map <leader>te :tabedit <c-r>=expand("%:p:h")<cr>/
" Switch CWD to the directory of the open buffer
map <leader>cd :cd %:p:h<cr>:pwd<cr>
" Specify the behavior when switching between buffers
try
set switchbuf=useopen,usetab,newtab
set stal=2
catch
endtry
" Return to last edit position when opening files (You want this!)
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugin
" Call PlugInstall after adding to this (need to reload vimrc)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'tpope/vim-fugitive'
Plug 'mhinz/vim-signify'
Plug 'sheerun/vim-polyglot'
Plug 'joshdick/onedark.vim'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'tpope/vim-eunuch'
Plug 'google/vim-searchindex'
Plug 'scrooloose/nerdcommenter'
Plug 'kopischke/vim-fetch'
Plug 'ntpeters/wim-better-whitespace'
Plug 'jceb/vim-orgmode'
call plug#end()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Editing mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remap VIM 0 to first non-blank character
map 0 ^
" Move a line of text using ALT+[jk] or Command+[jk] on mac
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
if has("mac") || has("macunix")
nmap <D-j> <M-j>
nmap <D-k> <M-k>
vmap <D-j> <M-j>
vmap <D-k> <M-k>
endif
" Delete trailing white space on save, useful for some filetypes ;)
fun! CleanExtraSpaces()
let save_cursor = getpos(".")
let old_query = getreg('/')
silent! %s/\s\+$//e
call setpos('.', save_cursor)
call setreg('/', old_query)
endfun
if has("autocmd")
autocmd BufWritePre *.txt,*.js,*.py,*.wiki,*.sh,*.coffee :call CleanExtraSpaces()
endif
" shortcuts for 3-way merge
map <Leader>1 :diffget LOCAL<CR>
map <Leader>2 :diffget BASE<CR>
map <Leader>3 :diffget REMOTE<CR>
vmap <C-S-c> "+yi
vmap <C-S-v> c<ESC>"+p
imap <C-S-v> <C-r><C-o>+
set clipboard=unnamed
" Aliases for common mistakes
cnoreabbrev W w
cnoreabbrev E e
cnoreabbrev Q q
cnoreabbrev WQ wq
cnoreabbrev Wq wq
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Spell checking
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Pressing ,ss will toggle and untoggle spell checking
map <leader>ss :setlocal spell!<cr>
" Shortcuts using <leader>
map <leader>sn ]s
map <leader>sp [s
map <leader>sa zg
map <leader>s? z=
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Misc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remove the Windows ^M - when the encodings gets messed up
noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
" Quickly open a buffer for scribble
map <leader>q :e ~/buffer<cr>
" Quickly open a markdown buffer for scribble
map <leader>x :e ~/buffer.md<cr>
" Toggle paste mode on and off
map <leader>pp :setlocal paste!<cr>
" Jump to end and start of lines with no shift pressing
map <leader>' $
map <leader>[ ^
" Print full path
map <leader>m :echo expand('%:p')<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Helper functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Returns true if paste mode is enabled
function! HasPaste()
if &paste
return '[PASTE]'
endif
return ''
endfunction
" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
let l:currentBufNum = bufnr("%")
let l:alternateBufNum = bufnr("#")
if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif
if bufnr("%") == l:currentBufNum
new
endif
if buflisted(l:currentBufNum)
execute("bdelete! ".l:currentBufNum)
endif
endfunction
function! CmdLine(str)
call feedkeys(":" . a:str)
endfunction
function! VisualSelection(direction, extra_filter) range
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", "\\/.*'$^~[]")
let l:pattern = substitute(l:pattern, "\n$", "", "")
if a:direction == 'gv'
call CmdLine("Ack '" . l:pattern . "' " )
elseif a:direction == 'replace'
call CmdLine("%s" . '/'. l:pattern . '/')
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
fun! TrimWhitespace()
let l:save = winsaveview()
keeppatterns %s/\s\+$//e
call winrestview(l:save)
endfun
nnoremap <leader>t :call TrimWhitespace()<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugin config
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Airline Cusomization
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
" Tabline customization
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_idx_mode = 1
let g:airline#extensions#tabline#formatter = 'unique_tail'
let g:airline#extensions#tabline#show_close_button = 0
let g:airline#extensions#tabline#show_tab_type = 0
" Style
let g:airline_left_sep = ''
let g:airline_left_alt_sep = ''
let g:airline_right_sep = ''
let g:airline_right_alt_sep = ''
let g:airline_symbols.branch = ''
let g:airline_symbols.readonly = ''
let g:airline_symbols.linenr = '¶'
let g:airline_symbols.maxlinenr = ''
" Remove file encoding if it's default
let g:airline#parts#ffenc#skip_expected_string='utf-8[unix]'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Keymapping for plugins
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CtrlP tag search
map <C-o> :CtrlP<CR>
noremap <C-y> "*y
noremap <C-p> "*p
" Pytest
nmap <silent><Leader>f <Esc>:Pytest file<CR>
nmap <silent><Leader>c <Esc>:Pytest class<CR>
nmap <silent><Leader>m <Esc>:Pytest method<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colorscheme and GUI
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
try
set background=dark
let g:onedark_termcolors=16
set termguicolors
colorscheme onedark
let g:airline_theme='onedark'
catch
echom "Onedark colorscheme not found..."
endtry
" Make visual selection the same colour as search
highlight! link Visual Search
set guifont=MesloLGSDZForPowerline-Regular:h13
## ~~~ Aliases ~~~
# For a full list of active aliases, run `alias`.
# Navigation
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
# vim
alias vi='vim'
alias svi='sudo vi'
alias edit='vim'
alias bim='vim'
# grep
alias g='grep -sr'
alias gg='grep -isr \!:1 *'
alias gx='grep -isr --exclude-dir=\!:! \!:2 *'
alias gp="grep --exclude='*[.py[~c]]"
# Bash idioms
alias mkdir='mkdir -p'
alias rm='rm -f'
# other
alias x=ranger
alias pre="bundle exec jekyll serve"
alias ls='ls --color=always'
alias lf='ls -F -l'
alias ctags="`brew --prefix`/bin/ctags"
alias c="open /Applications/PCalc.app"
alias so='source $HOME/.zshrc'
alias edc='vim $HOME/.zshrc'
alias ip='public-ip'
alias speedtest='speedtest-cli --secure'
alias zshconfig='vim ~/.zshrc && rcupdate'
alias monthly='$DOTFILES/setup/monthly'
alias cdf='teach'
alias pins='$SCRIPTS/utils/print_dots.sh python $SCRIPTS/random/pin_tags.py > /dev/null 2> /dev/null'
alias articles='$SCRIPTS/random/article_log.py'
alias clk='tty-clock -c -C 4'
alias feeds='$SCRIPTS/todoist/feed-grabber.py'
alias o='ofd'
alias make='nocorrect make'
alias mkdir='nocorrect mkdir'
alias git='nocorrect git'
alias nis='npm install --save'
alias h='history | grep'
alias cp='cp -v'
alias p='playing'
alias task='$SCRIPTS/todoist/todoist_utils.py'
alias neofetch='neofetch --disable de wm'
alias home='cd ~'
alias diff='colordiff'
alias ping='ping -c4'
alias f='find . -iname'
alias grep='ggrep -ni --colour=always'
alias clear='[ $[$RANDOM % 100] = 0 ] && sl; clear || clear'
alias c='clear'
alias cl='clear && ls'
alias h='history'
alias tags='ctags -R'
alias perf='gperf'
alias songs='python $SCRIPTS/spotify/top_songs.py $SPOTIFY_USERNAME -e $SPOTIPY_TOP_SONGS_URI'
alias install_plug='curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
alias spt='nocorrect spt'
# git
alias st='git status --short'
alias go='git checkout'
alias push='git push'
alias add='git add -u'
alias com='git commit'
alias gd='git diff'
alias gg='git grep'
alias br='git branch'
alias stash='git stash'
alias del='git branch -D'
alias revert='git checkout --'
alias d='git diff'
alias gpr='git pull --rebase'
alias grm='git rebase master'
# python
alias flake=flake8
alias autopep8='autopep8 --in-place --aggressive'
alias stylemeup='autopep8 --recursive .'
alias fix='git ls-files --modified | autopep8'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment