vim
is a text editor. This is a page with some useful info on using vim
.
Open a terminal window and type vim filename
to start editing the given filename (will create a blank file if it doesn’t exist already).
The trick to using vim is knowing whether you are in COMMAND mode (moving around on top of the text) or INSERT mode (inserting text into the file). Usually it says – INSERT – at the bottom of the screen when in insert mode. If you are ever unsure, hit the ESCAPE key to get back to COMMAND mode.
h, j, k, l move left, down, right, up (or use the arrow keys)
Cntrl-f forward one screen
Cntrl-b backward one screen
G move to end of file
gg move to beginning of file
#G move to line # (eg, 207G moves to line 207)
0 move to beginning of line
$ move to end of line
H move to top of screen (High)
M move to middle of screen
L move to bottom of screen (Low)
/pattern search forward for pattern (hit n to repeat search for next)
w move forward one word at a time
b move backward one word at a time
NOTE: you should be in COMMAND mode to execute these commands. Some of them, like i
, a
, A
, and R
, put you into INSERT mode. Remember, hit Esc
to get back to command mode.
i insert text
a append text (after cursor)
A append text at end of line
x delete character
dd delete line
#dd delete # lines (eg, 5dd deletes 5 lines)
yy copy line
p paste line (below cursor)
r replace one character
R replace until you hit ESCAPE
u undo (can do multiple times if needed)
Cntrl-r redo
:wq write (save) and quit
:q! quit without saving
I make heavy use of tabs when I need to look at/edit multiple files. For example, if I’m editing two classes and a main program, I’ll typically have each in a separate tab, and use gt
to go between the tabs.
If you’re already in vim
editing a file, use :tabe filename
to open filename in a new tab. Then use gt
to “go” to the next tab (i.e., make some edits in one file, Esc
to get back to command mode, gt
to go to the other tab, edit that file, and so on…
Vundle is a package manager for vim bundles that can make your vim much nicer. For example you can use different colorschemes that are not in the default.
Plugin 'w0ng/vim-hybrid'
Adds the hybrid colorscheme for use within vim. However before we can use this bundle we have to setup vundle. Follow these steps to setup your vundle!
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Now you have vundle installed! Go to your .vimrc file by vim ~/.vimrc
and add the following to it at the top (Your .vimrc will have a lot of other inside of it just put these at the top).
set background=dark " Optional personally I like my background to be dark
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim' " This must be here at the top
Plugin 'w0ng/vim-hybrid'
call vundle#end()
syntax enable
Now you should save and quit out of your .vimrc file using :wq
. Finally there are two ways to complete the installation of your new plugins (you must do one of these two every time you add a plugin to your .vimrc)
Launch vim
and run :PluginInstall
To install from command line: vim +PluginInstall +qall
So now a few of my favorite plugins are:
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'valloric/youcompleteme' " This bundle is around 400 - 500 MB
Plugin 'tpope/vim-fugative'
Check them out or find your own! VimAwesome is a good site to find cool vim plugins!
See also: