Skip to content

Instantly share code, notes, and snippets.

@roberthamel
Last active November 19, 2020 07:54
Show Gist options
  • Save roberthamel/7d843af80ebb31f40473b8c5f6978015 to your computer and use it in GitHub Desktop.
Save roberthamel/7d843af80ebb31f40473b8c5f6978015 to your computer and use it in GitHub Desktop.

Vim

Finding Your Way Around

Download MacVim (optional)

MacVim

Set up Oh-my-zsh (optional)

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Edit .zshrc

vim ~/.zshrc

iTerm Preferences

CMD + ,

  1. Text: Change Font size to 14

Vim

:w practice.php will create a new file called practice.php :e practice.php edit the practice.php file that was just created :pwd show the present working directory

TAB Autocompletion works the same as on the command line.

  • i enter insert mode
  • ESC enter normal mode
  • h left, l right, j down, k up
  • v enter visual mode to highlight characters
  • V enter visual line to highlight entire lines
  • d delete character (or selection)
  • u undo
  • :e ~/.vimrc open the .vimrc file to configure Vim
  • :colorscheme then tab will cycle through all of the different colorscheme options
  • :so source & % current file [:so %] source the current file.

If editing the .vimrc file inside of Vim, this command will apply all configuration changes.

syntax enable
colorscheme desert
"Make backspace behave like every other editor
set backspace=indent,eol,start

Mapping And Commands


:e ~/.vimrc

  • :echo $MYVIMRC displays the path to .vimrc in home directory
  • :tabedit $MYVIMRC opens .vimrc in a new tab
  • :bd close tab or buffer
  • <cr> carriage return
  • :tabc closes the current tab

, acts as a namespace to all of your configuration. Can be anything that you want.

  • <Leader> alias for backslash (\) and is default.
  • o create a new line
  • / search for text n will show next occurence
  • gg goto top of document
  • V to select full line y yank text (alias for copy) p to paste text at desired spot
  • yy yank entire line
  • . redo last command
  • nohlsearch turns off search highlighting
  • zz centers current line on page
"...
"The default leader is \, but a comma is much better
let mapleader = ','
"activates line numbers
set number
"set line spacing (MacVim specific)
set linespace=15

"---------------Search--------------"
set hlsearch
set incsearch

"---------------Mappings--------------"

"Make it easy to edit the Vimrc file
nmap <Leader>ev :tabedit $MYVIMRC<cr>
"add simple highlight removal
nmap <Leader><space> :nohlsearch<cr>

"--------------Auto-Commands------------"

"Automatically source the Vimrc file on save.
augroup autosourcing
  autocmd!
  autocmd BufWritePost .vimrc source %
augroup END

A Prettier Vim


Vim Atom Dark Theme

cd ~/.vim
mkdir colors
cd colors
wget https://raw.githubusercontent.com/gosukiwi/vim-atom-dark/master/colors/atom-dark-vim
  • :colorscheme atom-dark
" ~/.vimrc
"...
"--------------Visuals--------------
colorscheme atom-dark
set t_CO=256 "forces 256 colors (only use if using terminal Vim)
set guifont=Fira_Code:h17 "Will probably need to be installed
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
"...

Disable Fullscreen Animations (optional)

Open preferences in MacVim (CMD + ,). Uncheck Prefer native full-screen support (requires Mac OS X 10.7).

Optimizing Window Splits


  • :sp split window horizontally
  • :vsp vertical window split
  • :q close the active split window
  • CTRL + w then h j k or l to navigate between the splits.
  • CTRL + ww to switch between open splits
  • :bp return to the previous buffer
  • :ls list all buffers you have open
  • :b3 goto the 3rd buffer, etc. etc.
  • CTRL + w| causes split to temporarily go full screen
  • CTRL + w= normalizes the splits

This is the default. We're going to change this.

  • CTRL + ev will open the .vimrc file
"--------------Split Management--------------
set splitbelow
set splitright
nmap <C-J> <C-W><C-J>
nmap <C-K> <C-W><C-K>
nmap <C-H> <C-W><C-H>
nmap <C-L> <C-W><C-L>

Vundle and Better File Browsing

  • :e . browse the current directory
  • CTRL + ^ return to previous edit point

Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Add the following to to the top of your .vimrc file

set nocompatible

so ~/.vim/plugins.vim "links plugin config that will reside in a separate file
" ~/.vim/plugins.vim
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-vinegar'
Plugin 'scrooloose/nerdtree'

call vundle#end()
filetype plugin indent on
  • :PluginInstall installs newly added plugins

Vinegar

  • - opens the folder that current file resides
    • - again will go up a level
  • d create a directory
  • D delete a directory or file
  • % create a new file

NERDtree

  • :NERDTreeToggle opens a sidebar directory tree
  • ? within sidebar will show quickhelp
"Add to Mappings section in .vimrc

"Make NERDTree easier to toggle.
nmap <D-1> :NERDTreeToggle<cr> "D mean Command

Faster Browsing with CtrlP


It is recommended to install ctags brew install ctags

" ~/.vim/plugins.vim
"...
Plugin 'ctrlpvim/ctrlp.vim'
  • :CtrlP or CTRL + p then start typing to find a file to navigate to

  • :CtrlPBufTag start typing to find variables, functions and class declarations

  • :CtrlPMRUFiles list most recently used files

  • CTRL + d switch between searching through any path or the file name itself

  • :bufdo bd! close everything

" ~/.vimrc
"Add to Mappings section
nmap <c-R> :CtrlPBufTag<cr>
nmap <D-e> :CtrlPMRUFiles<cr>

"--------------Plugins--------------
"CtrlP
let g:ctrlp_custom_ignore = 'node_modules\|DS_Store\|git'
let g:ctrlp_match_window = 'top,order:ttb,min:1,max:30, results:30'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment