Skip to content

Instantly share code, notes, and snippets.

@neanias
Last active July 3, 2020 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neanias/764d2dd6d42574cbbcf5a307635c4f2e to your computer and use it in GitHub Desktop.
Save neanias/764d2dd6d42574cbbcf5a307635c4f2e to your computer and use it in GitHub Desktop.
General purpose vimrc for Ruby & Rails editing

General purprose vimrc

Designed for Ruby & Rails editing

Installation

There are 2 options for this:

1. Clone the repo

  1. Clone this repo using the link in the box above.
  2. Copy vimrc into .vimrc in your home directory. a. cp vimrc $HOME/.vimrc from within the repo

2. Copy vimrc

  1. Copy the contents of the vimrc file below
  2. Create a new file in your home directory called .vimrc and paste the contents into it.

Once you've completed either of these options, run vim and press Enter to begin installing the plugin manager, vim-plug.

" Disable Vi-compat mode
set nocompatible " be iMproved, required
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" PLUGINS "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Load vim-plug
if empty(glob("~/.vim/autoload/plug.vim"))
" Installs vim-plug if not already installed
execute '!curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.github.com/junegunn/vim-plug/master/plug.vim'
endif
call plug#begin('~/.vim/plugged')
""""""""""""""""""""""""""
" WHOLE EDITOR PLUGINS "
""""""""""""""""""""""""""
" Nice file browsing
Plug 'scrooloose/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'
" Helpful status bar
Plug 'vim-airline/vim-airline'
" Clear bracket highlighting
Plug 'junegunn/rainbow_parentheses.vim'
" EditorConfig
Plug 'editorconfig/editorconfig-vim'
" Fuzzy finding
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
" Auto-closing brackets :D
Plug 'Raimondi/delimitMate'
""""""""""""""""""""""""""""
" LANGUAGES & FRAMEWORKS "
""""""""""""""""""""""""""""
" Linter
Plug 'w0rp/ale'
" Multi-language plugin
Plug 'sheerun/vim-polyglot'
" Rails engine
Plug 'tpope/vim-rails'
" Git plugin
Plug 'tpope/vim-fugitive'
call plug#end() " required
"""""""""""""""""""""""""""""
" ENABLE SYNTAX & PLUGINS "
"""""""""""""""""""""""""""""
" Required. Enables plugins and proper indentation.
filetype plugin indent on
" Enable syntax highlighting
syntax enable
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" SETTINGS "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""
" SANE DEFAULTS "
"""""""""""""""""""
" Show line numbers
set number
" Show line & char position
set ruler
" Copy indent from previous line
set autoindent
" Do smart autoindenting when starting a new line.
set smartindent
" Use spaces instead of tabs
set expandtab
" Related to above
set smarttab
" Show us which mode we're in
set showmode
" Show us what command we're plugging in
set showcmd
" Size of a tab in spaces
set tabstop=4
" Number of spaces used for autoindent
set shiftwidth=2
" Update a file if changes outside of Vim
set autoread
" Don't redraw the window until ready
set lazyredraw
" Highlight the matching bracket when we insert a new one.
set showmatch
" But only for 2 seconds
set mat=2
" Don't create a spare file, clogs up Git if not configured properly
set noswapfile
" Use comma as the leader
let mapleader=","
" Encode in UTF-8
set encoding=utf8
" Support all file endings
set ffs=unix,dos,mac
" Fix backspace not working
set backspace=indent,eol,start
" Don't hard wrap the text
set nowrap
set spelllang=en_gb
" Always show the status line
set laststatus=2
""""""""""""""""""""
" REMAPPING KEYS "
""""""""""""""""""""
" Remap VIM 0 to first non-blank character
map 0 ^
" Create window splits easier. The default
" way is Ctrl-w,v and Ctrl-w,s. I remap
" this to vv and ss
nnoremap <silent> vv <C-w>v
nnoremap <silent> ss <C-w>s
" 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
"""""""""""""""""""""
" SEARCH SETTINGS "
"""""""""""""""""""""
" Highlight all search matches
set hlsearch
" Ignore case when searching
set ignorecase
" Unless we have a capital letter
set smartcase
" Add magic characters to search
set magic
" Disable highlight when // is pressed
map <silent> // :noh<CR>
"""""""""""""""""""""
" PLUGIN SETTINGS "
"""""""""""""""""""""
" ALE linter
" Show errors in statusline
let g:airline#extensions#ale#enabled = 1
" Whitelist ruby linters
let g:ale_linters = {
\ 'ruby': ['rubocop', 'ruby', 'brakeman']
\}
" Execute through bundle
let g:ale_ruby_rubocop_executable = 'bundle'
" Rainbow brackets
au VimEnter * RainbowParentheses
" NERDTree
" Open the project tree and expose current file in the nerdtree with Ctrl-\
nnoremap <silent> <C-\> :NERDTreeFind<CR>:vertical res 30<CR>
" Handy key bindings for surrounding things
" ,# Surround a word with #{ruby interpolation}
map ,# ysiw#
vmap ,# c#{<C-R>"}<ESC>
" ," Surround a word with "quotes"
map ," ysiw"
vmap ," c"<C-R>""<ESC>
" ,' Surround a word with 'single quotes'
map ,' ysiw'
vmap ,' c'<C-R>"'<ESC>
" ,) or ,( Surround a word with (parens)
" The difference is in whether a space is put in
map ,( ysiw(
map ,) ysiw)
vmap ,( c( <C-R>" )<ESC>
vmap ,) c(<C-R>")<ESC>
" ,[ Surround a word with [brackets]
map ,] ysiw]
map ,[ ysiw[
vmap ,[ c[ <C-R>" ]<ESC>
vmap ,] c[<C-R>"]<ESC>
" ,{ Surround a word with {braces}
map ,} ysiw}
map ,{ ysiw{
vmap ,} c{ <C-R>" }<ESC>
vmap ,{ c{<C-R>"}<ESC>
map ,` ysiw`
" ================ Persistent Undo ==================
" Keep undo history across sessions, by storing in file.
" Only works all the time.
if has('persistent_undo')
silent !mkdir ~/.vim/backups > /dev/null 2>&1
set undodir=~/.vim/backups
set undofile
endif
""""""""""""""""""""""
" HELPER FUNCTIONS "
""""""""""""""""""""""
" via: http://rails-bestpractices.com/posts/60-remove-trailing-whitespace
" Strip trailing whitespace
function! <SID>StripTrailingWhitespaces()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
%s/\s\+$//e
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
" Strip trailing whitespace on write
autocmd BufWritePre * call <SID>StripTrailingWhitespaces()
" Use Q to intelligently close a window
" (if there are multiple windows into the same buffer)
" or kill the buffer entirely if it's the last window looking into that buffer
nnoremap <silent> Q :call CloseWindowOrKillBuffer()<CR>
function! CloseWindowOrKillBuffer()
let number_of_windows_to_this_buffer = len(filter(range(1, winnr('$')), "winbufnr(v:val) == bufnr('%')"))
" We should never bdelete a nerd tree
if matchstr(expand("%"), 'NERD') == 'NERD'
wincmd c
return
endif
if number_of_windows_to_this_buffer > 1
wincmd c
else
bdelete
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment