VIM
VI Improved.
See vim adventure for a game to learn vim, and stackoverflow most useful vim shortcuts.
Commands
Quit with Error
Useful for aborting mid-commit without submitting.
:cq
Insert Unicode Characters
i
ctrl+v
u####
Increment/Decrement A Number
ctrl+A # Increment
ctrl+X # Decrement
Visual Mode
v # character
V # line
ctrl+v # block
Indentation
v
>
<
Goto Last Edit
gi
Delete to End of Line
d$ # Delete only
c$ # Delete and enter insert mode
Macros
qq # start recording
{PERFORM ACTIONS}
q # stop recording
@q # repeat recorded actions (first time)
@@ # report recorded actions (every time after)
20@@ # repeat 20 times
Troublshooting
VIM ‘Frozen’
Generally happens when stopping terminal output with control floww.
ctrl+s # stops terminal output (causes freeze) (XON)
ctrl+q # starts terminal output (XOFF)
Customize VIM
Displayed without comments / unchanged lines.
1"" https://dougblack.io/words/a-good-vimrc.html#colors
2set nocompatible " Do not use vi compatible settings
3set viminfo=%,'50,\"1000,/50,:0,h,f0,n~/.vim/.viminfo
4" | | | | | | | + viminfo file path
5" | | | | | | + File marks 0-9,A-Z 0=NOT stored
6" | | | | | + Disable 'hlsearch' loading viminfo
7" | | | | + Max command-line history saved
8" | | | + Max search history saved
9" | | + Max register lines saved
10" | + lines saved for each register
11" + save/restore buffer list
12"" https://stackoverflow.com/questions/23012391/how-and-where-is-my-viminfo-option-set
13
14"" COLOR SCHEME:
15"" -------------
16set t_Co=256 " Use 256 colors. termguicolors (24bit) doesn't work with tmux
17colorscheme monokai " Use custom monokai theme
18syntax enable " Use colorscheme syntax highlighting
19set printoptions+=syntax:y " Use colorscheme syntax highlighting when printing to paper
20highlight ColorColumn ctermbg=235 guibg=#2c2d27 " Vertical column highlighting
21highlight VertSplit ctermfg=222 ctermbg=238 guifg=#ffd787 guibg=#444444 " Vertical split highlighting
22highlight User1 ctermfg=010 ctermbg=241 guifg=#00ff00 guibg=#626262 " %1* color
23highlight User6 ctermfg=241 ctermbg=239 guifg=#626262 guibg=#4e4e4e " %6* color (inverted %1*)
24highlight User2 ctermfg=009 ctermbg=239 guifg=#ff0000 guibg=#4e4e4e " %2* color
25highlight User7 ctermfg=239 ctermbg=237 guifg=#4e4e4e guibg=#3a3a3a " %7* color (inverted %7*)
26highlight User3 ctermfg=222 ctermbg=237 guifg=#ffd787 guifg=#3a3a3a " %3* color
27highlight User8 ctermfg=237 ctermbg=235 guifg=#3a3a3a guifg=#262626 " %8* color (inverted %3*)
28highlight User4 ctermfg=239 ctermbg=235 guifg=#4e4e4e guifg=#262626 " %4* color
29highlight User9 ctermfg=235 ctermbg=233 guifg=#262626 guifg=#121212 " %9* color (inverted %4*)
30
31"" FILE OPTIONS:
32"" -------------
33"" If python (or any files) are tabbing incorrectly, modify the
34"" /usr/share/vim/vim74/ftplugin/(filetype).vim file and comment out the
35"" setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
36filetype on " Enable filetype detection
37filetype indent on " Load type specific indent files
38
39"" SEARCHING:
40"" ----------
41set incsearch " Search as characters are entered
42set hlsearch " Highlight search matches
43set ignorecase " Ignore case when searching
44set smartcase " If search contains a captial letter, make search case-sensitive
45nnoremap <silent> \ :nohlsearch<CR> " \ clears search highlights
46
47"" GUI OPTIONS:
48"" ------------
49set guioptions=agirt " No menu or toolbar in GUI
50set guifont=SF\ Mono " Use SF Mono font in GUI
51
52"" CONSOLE OPTIONS:
53"" ----------------
54set mouse=a " Mouse will be on all the time
55set belloff=all " Turn off visual/audio bells
56set encoding=utf8 " Encode non-ASCII to UTF8
57set tabstop=2 " Tab control characters are rendered with 5 spaces
58set softtabstop=2 " Number of spaces inserted when inserting a tab control character
59set expandtab " Expand tab control character to spaces
60set shiftwidth=2 " Shifting (<< >>) is 2 spaces
61set backspace=2 " Make backspace behave like other editors
62set textwidth=0 " Disable hard text wrapping
63set wrapmargin=0 " Disable soft text wrapping
64set wildmenu " Visual autocomplete for command menus
65set number " Enable lines numbers by default (nonumber disable)
66set showcmd " Continue to show last command until new one is entered
67set cursorline " Highlight current cursor line
68set showmatch " Showing matching closure
69set ruler " Display position information
70set autoread " Auto-reload files if they change on disk
71set autoindent " Copy indentation from current line when starting new line
72
73"" SPLIT MANAGEMENT:
74"" -----------------
75set splitbelow " Horizontal splits always created below
76set splitright " Vertical splits always created on right
77nnoremap <silent> <leader>\| :vsplit<CR> " \| Create vertical split
78nnoremap <silent> <leader>- :split<CR> " - Create horizontal split
79nnoremap <silent> <leader><left> <c-w><c-h> " left arrow select split left
80nnoremap <silent> <leader><right> <c-w><c-l> " right arrow select split right
81nnoremap <silent> <leader><up> <c-w><c-k> " up arrow select split up
82nnoremap <silent> <leader><down> <c-w><c-j> " down arrow select split down
83set fillchars+=vert:│ " Use solid line for vertical split
84
85"" NETRW FILE BROWSER:
86"" -------------------
87"" https://shapeshed.com/vim-netrw/#netrw---the-unloved-directory-browser
88let g:netrw_liststyle=3 " Tree view as default list view (i to rotate through)
89let g:netrw_sort_sequence='[\/]$,*' " List directories on top, files below
90let g:netrw_browse_split=3 " Open files in previous window (was 3, tab)
91let g:netrw_altv=1 " Open files in vertical split
92let g:netrw_banner=0 " Disable the starting banner
93let g:netrw_winsize=25 " Only use 25% of screen for netrw
94
95"" STATUSBAR:
96"" ---------
97"" Match coloring from tmux status bar
98set laststatus=2 " Always show the status bar
99set statusline= " Clear status line
100set statusline+=%1*%f%6* " File path
101set statusline+=%2*\ %{&encoding}%7* " File encoding
102set statusline+=%3*\ %{&fileformat}%8* " File format
103set statusline+=%4*\ %m%r%9* " [+] (modified file), [RO] (readonly)
104set statusline+=%= " Items before this are left-aligned, after right-aligned
105set statusline+=%9*%4*0x%B\ " Hex code of character under cursor
106set statusline+=%8*%3*%c\ " column number
107set statusline+=%7*%2*%p%%\ " Percent through the file
108set statusline+=%6*%1*[%l/%L] " Show current line, total lines
109
110"" FOLDING:
111"" --------
112set foldenable " Enable code folding
113set foldlevelstart=10 " Open 10 levels of folds by default
114set foldnestmax=10 " Max 10 nested fold level
115set foldmethod=indent " Fold based on indentation level
116
117"" COMMENTING:
118"" ----------
119map ,# :s/^/#/<CR>:nohlsearch<CR> " Add # comment start block
120map ,/ :s/^/\/\//<CR>:nohlsearch<CR> " Add // comment start block
121map ,> :s/^/> /<CR>:nohlsearch<CR> " Add > comment start block
122map ," :s/^/\"/<CR>:nohlsearch<CR> " Add " comment start block
123map ,% :s/^/%/<CR>:nohlsearch<CR> " Add % comment start block
124map ,! :s/^/!/<CR>:nohlsearch<CR> " Add ! comment start block
125map ,; :s/^/;/<CR>:nohlsearch<CR> " Add ; comment start block
126map ,- :s/^/--/<CR>:nohlsearch<CR> " Add -- comment start block
127map ,c :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>:nohlsearch<CR> " Clear comment start block
128"" Wrapping comments
129map ,* :s/^\(.*\)$/\/\* \1 \*\//<CR>:nohlsearch<CR> " Insert /* */ for line
130map ,( :s/^\(.*\)$/\(\* \1 \*\)/<CR>:nohlsearch<CR> " Insert (* *) for line
131map ,< :s/^\(.*\)$/<!-- \1 -->/<CR>:nohlsearch<CR> " Insert <!-- --> for line
132map ,d :s/^\([/(]\*\\|<!--\) \(.*\) \(\*[/)]\\|-->\)$/\2/<CR>:nohlsearch<CR> " Clear wrapped comment
133
134let mapleader=' ' " Use space instead of \ for leader key
135nnoremap <silent> <leader>h :source ~/.vimrc<CR> " h to reload .vimrc
136nnoremap <silent> <leader>r :set relativenumber!<CR> " m Toggle relative line numbers
137nnoremap <silent> <leader>n :set number!<CR> " n Toggle line numbers on/off
138nnoremap <silent> <leader>s :call StripTrailingWhitespace()<CR> " s Strip EOL/EOF trailing whitespace
139nnoremap <silent> <leader>g :call ColorRightGutters()<CR> " g Toggle right gutter highlighting
140nnoremap <silent> <Leader><Leader> :e#<CR> " Toggle between last opened file
141nnoremap <silent> <leader>f za " f open/closes fold
142
143"" Jump to last known position when opening file.
144autocmd BufReadPost *
145\ if expand("<afile>:p:h") !=? $TEMP |
146\ if line("'\"") > 0 && line("'\"") <= line("$") |
147\ exe "normal g`\"" |
148\ let b:doopenfold = 1 |
149\ endif |
150\ endif
151
152"" When in GVIM don't do it when the position is invalid or when inside an event
153"" handler, delay using "zv" until after reading the modelines.
154autocmd BufWinEnter *
155\ if exists("b:doopenfold") |
156\ unlet b:doopenfold |
157\ exe "normal zv" |
158\ endif
159
160"" Always do a full syntax refresh when loading
161autocmd BufEnter * syntax sync fromstart
162
163"" Strip EOL/EOF trailing whitespace
164function! StripTrailingWhitespace()
165 let save_cursor = getpos('.')
166 silent! %s/\s\+$//e " EOL whitespace
167 silent! %s#\($\n\)\+\%$## " EOF whitespace
168 call setpos('.', save_cursor)
169endfunction
170
171"" Toggle right gutter highlighting
172function! ColorRightGutters()
173 if &colorcolumn == ""
174 let &colorcolumn=join(range(81,120),",").join(range(120,999),",")
175 else
176 set colorcolumn=
177 endif
178endfunction