Skip to content

Instantly share code, notes, and snippets.

@snuggs snuggs/.vimrc
Last active Dec 8, 2017

What would you like to do?
VIM "$ mkdir ~/.tmp # ensure directory exists first
" Author: Snuggs
" Screencasts:
" Gist:
" Tutorial:
set encoding=utf-8
" ********************************************************************************
" Files
" ********************************************************************************
filetype off
filetype plugin indent on " if you switch off filetype plugin detection,
" the plugins will not be loaded
" --------------------------------------------------------------------------------
" Markdown
" --------------------------------------------------------------------------------
autocmd BufNewFile,BufRead *.md set textwidth=200 formatoptions=ta
" --------------------------------------------------------------------------------
" Javascript
" --------------------------------------------------------------------------------
autocmd BufNewFile,BufRead *.test set filetype=javascript
autocmd BufNewFile,BufRead *.impl set filetype=javascript
" --------------------------------------------------------------------------------
" --------------------------------------------------------------------------------
autocmd BufNewFile,BufRead *.json set filetype=javascript
" --------------------------------------------------------------------------------
" Ruby
" --------------------------------------------------------------------------------
autocmd BufNewFile,BufRead Gemfile set filetype=ruby
" --------------------------------------------------------------------------------
" (Web)IDL
" --------------------------------------------------------------------------------
autocmd BufNewFile,BufRead *.webidl set filetype=idl
" ********************************************************************************
" Settings ***********************************************************************
" ********************************************************************************
" [ENTER] Opens Nerdtree
autocmd VimEnter * NERDTreeToggle | wincmd p
syntax enable
syntax on " turns syntax highlighting on
if has("gui_running")
set guioptions=egmrt " remove ugly toolbar :-)
set t_Co=256 " enable 256 colors
set pastetoggle=<F2> " Allow toggle of code indention
set cursorline " highlight current cursor line
set cursorcolumn " highlight current cursor column
set nocompatible " (cp) use Vim defaults (much better)
set showcmd " (sc) display an incomplete command in the lower right
set hlsearch " Highlight found search results
set ttyfast " Send more characters for redraws
set mouse=a " Enable mouse use in all modes
set ttymouse=xterm2 " Set this to the name of your terminal that supports mouse codes.
" Must be one of: xterm, xterm2, netterm, dec, jsbterm, pterm
set wrap
set backupdir=~/.tmp " $ mkdir ~/.tmp # ensure directory exists first
set directory=~/.tmp " $ mkdir ~/.tmp # ensure directory exists first
set runtimepath^=~/.vim/bundle/ctrlp.vim " used for ctrl+p
set wildmenu "
set wildmode=list:longest,full
set ruler
set number
set autoindent
set showtabline=2
set foldenable "Enable indent folding
set foldmethod=indent
set tabstop=2 "set tab character to 2 characters
set shiftwidth=2 "indent width for autoindents
set smarttab
set expandtab "turn tabs into whitespace
set list " allows you to view hidden characters
set listchars=tab:>>,trail:- " tab will show as >- and whitespace will show as -
set listchars+=precedes:< " (lcs) when 'nowrap', character to indicate that line continues off the page
set listchars+=extends:>
set fileformats=unix,dos " sets <LF> (unix) first, then tries <CR><LF> (dos) next
set path=$PWD/**
set backspace=2 " allows deletion of characters prior to insertion mode
" backspace will delete CRLF at beginning of line
" space key will wrap to next line at end of line
" left and right arrow will wrap to previous and next lines at end of line
" (in normal mode & insertion mode)
set whichwrap=b,s,<,>,[,]
"Have 3 lines of offset (or buffer) when scrolling
set scrolloff=3
set so=0 " allows buffer to scroll before reaching border
set sidescroll=10 " amount buffer is scrolled to when on a word outside of viewing range
" (Hopefully) Stop VIM from crashing
:setlocal synmaxcol=200
" ********************************************************************************
" COLORS & HIGHLIGHTS ************************************************************
" ********************************************************************************
colorscheme jellybeans
" * can use hexidecimal values for gui (e.g. guibg=#000000)
" (defaults) - todo, error
" The following are the preferred 16 colors for your terminal
" Colors Bright Colors
" Black #4E4E4E #7C7C7C
" Red #FF6C60 #FFB6B0
" Green #254bb8 #CEFFAB
" Yellow #000000 #FFFFCB
" Blue #00ff00 #FFFFCB
" Magenta #FF73FD #FF9CFE
" gui / cterm display modes (none,(i)talic,(b)old,(s)tandout, (u)nderline, under(c)url)
"hi Example guifg=NONE guibg=#ff0000 gui=NONE ctermfg=NONE ctermbg=NONE cterm=NONE
highlight NonText ctermfg=7 guifg=gray
highlight StatusFileName ctermbg=black guibg=black ctermfg=gray guifg=gray
highlight Cursor guifg=black ctermfg=black guibg=white ctermbg=white
highlight iCursor guifg=white ctermfg=black guibg=steelblue ctermbg=white
highlight Search guibg=LightBlue
" ********************************************************************************
" Cursor
" ********************************************************************************
" set cursor configuration
"set guicursor=n-v-c:block-Cursor
"set guicursor+=i:ver100-iCursor
"set guicursor+=n-v-c:blinkwait10
"set guicursor+=i:blinkwait90
set guicursor=n-v-c:block-Cursor
set guicursor+=i:ver100-iCursor
set guicursor+=n-v-c:blinkwait10
set guicursor+=i:blinkwait90
" ********************************************************************************
" Status Message
" ********************************************************************************
set laststatus=2 " Always show status line
set statusline = " clear out status line
set statusline+=%#StatusFileName# " 'todo' colorscheme
set statusline+=[%Y] " file type
set statusline+=%r\ " read only mode
set statusline+=%-10F " full file name
set statusline+=%* " default colorscheme
set statusline+=%= " right justify everything after this line
set statusline+=[%3c,%-3l\ " cursor column position
set statusline+=of\ %L\ lines\ " cursor line, total lines
set statusline+=(%p%%)]\ \ \ " percentage
set statusline+=[%{g:colors_name}\ ->\ " colorscheme
set statusline+=(%{&term})\ " terminal color setting name
set statusline+=%{&t_Co}\ Color\ Scheme] " color count
" ********************************************************************************
" Mappings
" ********************************************************************************
map <C-b> :tabp<CR>
map <C-n> :tabn<CR>
map <C-t> :tabnew
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
" Toggle NERDTree
map <C-m> :NERDTreeToggle<CR>
" normal mode: save
nnoremap <c-s> :w<CR>
" insert mode: escape to normal and save
inoremap <c-s> <Esc>:w<CR>
" visual mode: escape to normal and save
vnoremap <c-s> <Esc>:w<CR>
map ; :
map , :ZoomWin<CR>
"nnoremap <esc> :nohlsearch<return>
"nnoremap <esc> :nohlsearch<return><esc>:<backspace>
nmap <C-G> :call BOLEOL()<cr>
"Set space to toggle a fold
nnoremap <space> za
" ********************************************************************************
" Functions
" ********************************************************************************
" --------------------------------------------------------------------------------
" Toggle placing cursor to (soft) beginning / (soft) ending of line
" --------------------------------------------------------------------------------
let s:BOLEOL=1
function! BOLEOL()
if (s:BOLEOL == 1)
:norm $
let s:BOLEOL = 0
:norm 0
let s:BOLEOL = 1
" --------------------------------------------------------------------------------
" Toggle status line color
" --------------------------------------------------------------------------------
function! InsertStatuslineColor(mode)
if a:mode == 'i'
highlight statusline guibg=green ctermbg=green
elseif a:mode == 'r'
highlight statusline guibg=white ctermbg=white
highlight statusline guibg=red ctermbg=red
au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertLeave * hi statusline guibg=gray ctermbg=gray
" default the statusline to green when entering Vim
highlight statusline guibg=gray ctermbg=gray
" --------------------------------------------------------------------------------
" Vim Airline
" --------------------------------------------------------------------------------
" --------------------------------------------------------------------------------
" Pathogen
" --------------------------------------------------------------------------------
call pathogen#infect()
TMUX Line -
NerdTree -
Pathogen -
Vim Gutter -
Syntastic -
Vim Jade -
Vim Airline (Status Bar) -
Rails.vim -
ZoomWin.vim -
endwise.vim -
Minitest Highlighting -
Surround.vim -
css.vim -
VIM SugarSS -
closetag.vim -
" colorschemes
:echo g:colors_name
" ---------------------
Jellybeans.vim (colorscheme) -
"+y[movement] " copy to clipboard buffer (including double quote)
" Similarly, to paste from clipboard, "+p
vim -p [file1 file2 file3 fileN] # vim opens file collection in tabs
vim -o [file1 file2 file3 fileN] # vim opens file collection in split buffers
* k – navigate upwards
* j – navigate downwards
* l – navigate right side
* h – navigate left side
go down by 10 lines, then type “10j”.
:n Jump to line number n. For example, to jump to line 42, you'd type :42
0 – go to the starting of the current line.
^ – go to the first non blank character of the line.
$ – go to the end of the current line.
g_ – go to the last non blank character of the line.
H – Go to the first line of current screen.
M – Go to the middle line of current screen.
L – Go to the last line of current screen.
* ctrl+f – Jump forward one full screen.
* ctrl+b – Jump backwards one full screen
* ctrl+d – Jump forward (down) a half screen
* ctrl+u – Jump back (up) one half screen
% – Go to the matching braces, or parenthesis inside code.
N% – Go to the Nth percentage line of the file.
NG – Go to the Nth line of the file.
G – Go to the end of the file.
” – Go to the position where you were in NORMAL MODE while last closing the file.
^ – Go to the position where you were in INSERT MODE while last closing the file.
g – Go to the beginning of the file.
e – go to the end of the current word.
E – go to the end of the current WORD.
b – go to the previous (before) word.
B – go to the previous (before) WORD.
w – go to the next word.
W – go to the next WORD.
zt - move current cursor placement to top
zb - move current cursor placement to bottom
zz - move current cursor placement to center screen
WORD – WORD consists of a sequence of non-blank characters, separated with white space.
word – word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* – single WORD
* – seven words.
# Vim Paragraph Navigation
* { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
* } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.
Editing blocks of text
Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text.
Vim command Action
~ Change the case of characters. This works both in visual and command mode.
In visual mode, change the case of highlighted characters.
In command mode, change the case of the character uder cursor.
> (V) Shift right (indent).
< (V) Shift left (de-indent).
c (V) Change the highlighted text.
y (V) Yank the highlighted text. In Windows terms, "copy the selected text to clipboard."
yy or :y or Y Yank the current line. You don't need to highlight it first.
d (V) Delete the highlighted text. In Windows terms, "cut the selected text to clipboard."
x Delete characters under the cursor.
X Delete characters before the cursor.
dd or :d Delete the current line. Again, you don't need to highlight it first. Deleting text
p Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard".
Put characters after the cursor. Put lines below the current line.
P Put characters before the cursor. Put lines above the current line.
u Undo the last action.
U Undo all the latest changes that were made to the current line.
Ctrl + r Redo.
v Start highlighting characters. Use the normal movement keys and commands
to select text for highlighting.
V Start highlighting lines.
:%! nl -ba # Line Numbers
Finding Files
:pwd # print working directory
:cd # change directory
:find # file name (within path)
* /i – Search for a pattern which will you take you to the next occurrence of it.
* ?i – Search for a pattern which will you take you to the previous occurrence of it.
* * - Go to the next occurrence of the current word under the cursor.
* # - Go to the previous occurrence of the current word under the cursor.
re-apply - The . command repeats the last change. A change, in this context, is inserting, deleting or replacing text. Being able to repeat this is a very powerful mechanism. If you organise your editing around it, many changes will become a matter of hitting just that . key. Watch out for making other changes in between, because it will replace the change that you were repeating. Instead you might want to mark the location with the m command, continue your repeated change and come back there later.
registers - When you are typing a phrase or sentence multiple times, there is an even quicker approach. Vim has a mechanism to record a macro. You type qa to start recording into register 'a'. Then you type your commands as usual and finally hit q again to stop recording. When you want to repeat the recorded commands you type @a. There are 26 registers available for this.
:tabnew [filename] # and Vim will load the file in the new tab (or defaults to empty buffer)
:tabs # displays all tabs
:tabn and :tabp switch between tabs, or you can use gt while you're in normal mode.
If you have a lot of tabs open, you can use :tabfirst, or just :tabfir, to jump to the first tab, and :tablast to jump to the last tab that's open.
if you don't like the existing shortcuts for the tab commands, you can add your own. For instance, if you want to make it easy to open a new tab, you might insert this into your .vimrc:
- Rearranging tabs
If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot.
Vim starts tab numbering from 0, so if you have six tabs open, you'll have tab 0 through tab 5. So, if you're in the first tab and want to move it to the fourth position, you'd run :tab 3.
map <leader>tn :tabnew %<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
:split [file] # Splits the window horizontally.
:vsplit [file] # Splits the window vertically.
<Ctrl>-w # (mnemonic: control window) To move between windows you use <Ctrl>-w (mnemonic: control window).
# To move in a specific direction, add the relevant movement key. So, to move upwards: <Ctrl>-w+k.
# To close the active window use :q, just as you would to close a window normally.
<Ctrl>-w [HJKL] # to actually move buffer (i.e. K will move current buffer to top. * notice caps*)
:only # if split buffers, close all buffers except current one.
You can reduce/enlarge the size of the current window with <Ctrl>-w+- and <Ctrl>-w++, respectively. To specify the size of a window when you open it, prefix the :split command with the desired height/width in lines. For example, to show README in a window of 5 lines high: :5 :split README.
When splitting a window you can prepend a modifier command to tell where the
window is to appear:
:leftabove {cmd} left or above the current window
:aboveleft {cmd} idem
:rightbelow {cmd} right or below the current window
:belowright {cmd} idem
:topleft {cmd} at the top or left of the Vim window
:botright {cmd} at the bottom or right of the Vim window
CTRL-W CTRL-X Without count: Exchange current window with next one. If there
is no next window, exchange with previous window.
With count: Exchange current window with Nth window (first
window is 1). The cursor is put in the other window.
Vim command Action
:rs/foo/bar/a Substitute foo with bar. r determines the range and a determines the arguments.
The range (r) can be
nothing Work on current line only.
number Work on the line whose number you give.
% The whole file.
Arguments (a) can be
g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
i Ignore case for the search pattern.
I Don't ignore case.
c Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution.
:452s/foo/bar/ Replace the first occurrence of the word foo with bar on line number 452.
:s/foo/bar/g Replace every occurrence of the word foo with bar on current line.
:%s/foo/bar/g Replace every occurrence of the word foo with bar in the whole file.
:%s/foo/bar/gi The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on.
:%s/foo/bar/gc Confirm every substitution.
:%s/foo/bar/c For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.
# Sessions
:mksession my_session.vim # creates session with current layout info
:source my_session.vim # restore session from file (or :~>vim -S my_session.vim)
:bn[ext] # Next buffer
:bp[revious] # Previous Buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.