Vim Intro
Table of Contents
Work in Progress.
Why Vim
Vim is not a necessity by any means. But in the long run it will make you more productive and save you from having to reach for the mouse as often. Vim won't enable you to type any faster, but it will allow you to navigate and edit your documents more quickly. Once you start using vim it will become second nature and you will want to use it everywhere. Don't worry other people have had that thought too! You can find all sorts of extensions for programs that incorporate vi/vim key bindings, here are just a few:
Program | Plugin |
---|---|
Visual Studio | VsVim |
Visual Studio Code | VsCodeVim |
Emacs | evil, spacemacs |
Sublime | Vintageous |
Chrome | Vimium |
Jetbrains Editors | IdeaVim |
Installing Vim
Windows
There are many different ways to install vim. My preferred way is to use chocolately and tux vim. But you may prefer to check vim's site for other ways to download it.
Chocolatey
Chocolatey is a windows package manager. We are going to use it to install vim. Follow the instructions here on how to install it first. Or simply run the following command from cmd.exe.
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Unix
Many Unix distributions will have vim installed already. The vim site recommends installing the latest version by compiling the source yourself.
git clone https://github.com/vim/vim.git
cd vim/src
make
You can also check here for other ways of getting newer versions of vim.
Vim Docs
RTFM
:help
or just :h
. You can also specify what you want help with. For
instance, you can get help with the help command :h help
.
Modal Editing
Part of the power of vim lies in the different modes that allows you to assign different purposes to keys whilst in that mode.
- Normal mode
- This is where you will spend most of your time.
- Insert mode
- This is your normal editing where you would input the text.
- Visual mode
- This allows you you to visually select portions of text.
- Command line mode
- call functions
- issue ex commands
- invoke the shell with
!
- change settings
- Ex mode
- Similar to command line mode, but optimized for batch processing.
- practical uses
- Select mode
- I don't know what this is, I just found out about it while making this list.
TODO Normal Mode
topics to cover:
- the dot operator
- macros
- movements (motions)
Motion
these can be used with other commands like yank and delete
key | description |
---|---|
h |
left |
j |
down |
k |
up |
l |
right |
w |
forward word |
W |
forward WORD |
b |
beginning of word |
B |
beginning of WORD |
e |
end of word |
E |
end of WORD |
ge |
end of prev word |
gE |
end of prev WORD |
gg |
first line |
G |
last line |
[count]G |
go to line [count] |
/{term} |
search forward {term} |
?{term} |
search backwards {term} |
n |
next search result |
N |
prev search result |
f{char} |
find {char} to right on line and place cursor on {char} |
F{char} |
find {char} to left on line and place cursor on {char} |
t{char} |
find {char} to right on line and place cursor before {char} |
T{char} |
find {char} to left on line and place cursor after {char} |
; |
next f/F/t/T |
, |
prev f/F/t/T |
non motion command movement
key | description |
---|---|
ctrl d |
scroll window down |
ctrl u |
scroll window up |
ctrl o |
go to last cursor position in jump list. |
ctrl i |
Go to newer cursor position in jump list |
gf |
go to file, edit file name under the cursor |
Insert Mode
Most of the time you will only want to be typing text directly in this mode. There are shortcuts that you can use, but most of the time you will want to use normal mode to do any modifications to the text. Vim does offer some built in completion functionality but I find that plugin replacements work better.
There are also a number of shortcuts available for insert mode. However, I never use them, and will drop back into normal mode to perform most actions.
Visual Mode
Visual mode is where you can highlight text.
key | description |
---|---|
v |
visual mode |
shift v |
visual line mode |
ctrl v |
visual block mode |
This can be useful for when you are unsure about what you want to select or for performing a command line mode action on only a section of text.
By selecting text and then pressing :
you can enter command line mode and
perform an action such as search and replace.
Visual block mode can be useful for making edits vertically. In this example
after making my first selection with ctrl v
I use I
to add a character
before the selection and the use esc
to exit and finalize the change. For
the misspelled word, first I selected the column, then yanked (copied) it with
y
and then put (pasted) it with p
.
TODO Command Line Mode
.vimrc
Making vim your own.
The vimrc file is loaded every time vim is loaded. If you make changes to your
vimrc they will not be picked up without you first either sourcing your vimrc
or closing and reopening vim. To source your vimrc we will use command line
mode by pressing :
:source ~/.vimrc
but we only need specify the shortest unique sequence of a command
:so ~/.vimrc
additionally, if we have our vimrc open, we can shorten it further. % is replaced with the current file name.
:so %
Plugins
Vim Awesome! is a great site to find awesome plugins.
Plugin Managers
I personally use plug, but I'm sure the others are fine alternatives.
Plug
To install plug on windows run the following in PowerShell.
md ~\vimfiles\autoload $uri = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' (New-Object Net.WebClient).DownloadFile( $uri, $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath( "~\vimfiles\autoload\plug.vim" ) )
To install plug on Unix run the following:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
TODO Vim Script
TODO Bang
Using the command line from within vim. Also now we have :term
.
TODO Give examples of use cases.