By Kevin Hou
9 minute read
VIM is configured using a .vimrc file in your root directory (~/.vimrc) and serves as the configuration file. It is similar to a .bashrc or .bash_profile in the sense that it must be sourced to reflect changes, but VIM automatically takes care of when it launches. Ensure that the ~/.vimrc file exists and if not, create one. Now here's how the complete setup:
Copy/paste the contents of the configuration file into your .vimrc file or replace the file itself. This will give you all the configurations that I use. Before you are done, you need to install the packages that are used in the configuration.
Like I said earlier, I'm using vim-plug as my package manager service. Install vim-plug using:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Open VIM - type : to open the VIM command line. You will be using this a lot. Type PlugInstall and press Enter.
VIM has a handful of common "listener keys" that let VIM know you are about to perform a custom key command. This includes :, ,, /, etc.
Note: Sometimes need to source the .vimrc file first:
:source %
:PlugInstall
Install MacVim
Some plugins will require a higher version of VIM than the out-of-the-box built-in default for Macs. Many references online have recommended using MacVim and aliasing its CL interface to "replace" the default. Add this to your .bash_profile after you've installed MacVim:
# Mac VIM Alias
alias vim='/Applications/MacVim.app/Contents/MacOS/Vim'
Your VIM is now properly configured to work with the plugins that we have included in the .vimrc; however, some plugins require additional installations that I will described next.
There are a few plugins that require additional installations that don't get setup automatically using vim-plug. Follow the steps below to configure these libraries:
mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
('AG' stands for the silver element in periodic table)
brew install the_silver_searcher
Dev Box (Ubuntu)
sudo apt-get install -y automake pkg-config libpcre3-dev zlib1g-dev liblzma-dev
$ brew install CMake
$ sudo apt-get install cmake$ npm install -g typescript$ cd ~/.vim/plugged/YouCompleteMe && ./install.py --all(Dark: Solarized | Light: PaperColor)
Install into bundle folder. Create it if the directory doesn't exist.
$ cd ~/.vim/bundle
$ git clone git://github.com/altercation/vim-colors-solarized.git
Set the theme in .vimrc.
syntax enable
" Use 'light' if want the light theme
set background=dark
colorscheme solarized
The current default theme is solarized-dark however, if your iTerm profile name contains the the word light (case insensitive), the theme will be PaperColor with a light background.
:colorscheme papercolor. Available themes: papercolor, carbonized.Ensure you have a ~/.vim/bundle/ directory
Install using: $ git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree
NERDTree will automatically open when VIM opens because of this command in the .vimrc:
autocmd vimenter * NERDTree
~/.vim/pluginplugin folder
curl "https://raw.githubusercontent.com/yegappan/mru/master/plugin/mru.vim" -o "mru.vim"mru.vim, type :MRU in VIM and you will open up an interactive mini window listing your most recently used files that you can then browse and open files fromLaunch VIM: $ vim <file/directory (optional)>
Close current pane: :q
Quitting all panes: :qa
Save: :w
Save & Quit: :wq
Run a terminal command: :! <your-command>
Highlight mode: v
Insert mode (keyboard and arrow keys will work like normal): i
Insert new line below and enter insert mode: o
Delete and enter insert mode: s
Viewing mode (if in highlight or insert mode): Esc
Go to line: <Line-Number> + Shift + G
Back one word: b
Forward one word: w
Move to first non-blank character of line (Like Command + Left): _
Move to last non-blank character of line (Like Command + Right): g_
Beginning of line: 0
End of line: Shift + 4 ($)
Page up: Control + B
Page Down: Control + F
Delete row: D + D
Delete key in front of cursor: X
Arrow key mappings:
Up: k
Down: j
Left: h
Right: l
Move x positions: <number> + <key>
Example: Move down 10 lines: 10 + j
Note: Syntax supported by all navigation commands
Fuzzy Search - Searches file names — ,t
To cancel: Command + C or :q
Reload buffer (file) due to external changes: :e
Refresh NerdTree file tree: While in NerdTree pane, Shift + R
Can also open multiple files into the buffer (especially helpful when you don't have plugins): vim file1 file2
Listing open buffers: :args
Switching between files. Next: :n. Previous: :N.
Global find (searches content of file in project) — ,a
The "current project" is whatever directory you $ vim into
Search the selected word: Shift + 8
Search within file: \<keyword on page>
* n goes to next
* Shift + N goes back
* :noh clears the highlighted words on the page from the search
Search and replace: :%s/foo/bar/g (Replace foo with bar no confirmation. For confirmation, use gc instead of g at end)
Highlight all occurances of current word under cursor and go to first instance (while not in highlight mode): #
To refresh NerdTree's listing of files, navigate to the NerdTree pane and press Shift + R
Highlighting an entire line: Shift + V
Highlighting mode: v
Copy/yank: y
Copy file contents to clipboard: :% !pbcopy
Paste: p
Paste above: Shift + P
Pasting in insert mode (having copied using the operating system): Command + v
Paste from clipboard with auto-indenting: :set paste + Command + V
Yank/copy the inner word (word under cursor): yiw
Highlight the inner word (word under cursor): viw
Opening new panes, start with: control + w:
* v is vertical split (focus on the new pane)
* o is horizontal split
* = makes all panes equal size
Switch panes:
Move up: control + j
Move down: control + k
Move left: control + h
Move right: control + l
Close all except current: control + o
Undo: u
Redo: control + r
Highlight cursor in vim: Command + /
Repeating the last command: .
Delete: d --> Format: d + number (optional, default 1) + action
d + 4 + w would delete the four next words
Creating new file from CL in VIM:
$ vim <file-name>
Make sure you save before you quit using :w
Quitting: :q
Quitting without saving (and not being prompted): :q!
Using easymotion
Easymotion is a plugin that allows you to quickly move through the current file by highlighting all possible next positions for your cursor and adding a hotkey to them. Instead of typing /div and pressing n to cycle through to the instance of div that you're looking for in your code, you can simply type s to initiate the easymotion grepping, di as your two character query (configurable in .vimrc file), and type the key that is placed on the instance of what you're looking for.
Commenting
To comment a line you've highlighted, type gc
To comment a line without highlighting, type gcc
Opening up explorer:
:Explore or :Exp + Tab to complete it for you
Navigation using hjkl.
When opening a file:
o splits horizontally (opens below)v splits vertically (opens to the side)- Up one level to parent
gg - top of file/pane
Shift + G - bottom
Go to line number: <Line #> + Shift + G
You can use or view the command line without leaving the VIM environment.
:! takes you to a read-only fullscreen command prompt to see your history
:! <usual-terminal-command> will allow you to enter commands
.vimrc file:
Plug 'Valloric/YouCompleteMe'
:source %:PlugInstallPlug line from the .vimrc file.vimrc file:PlugClean and pressing y to confirm deletion of plugincaps lock to escape in System Preferecnes -> Keyboard -> Modifiers$ vimtutor<C> map to? It maps to Control