Vim
Introduction to Vim
Install vim
- Linux vi is mapped to vim, but it might not have all the features.
- Ubuntu: sudo apt-get install vim
- Red Hat: sudo yum install vim
- Windows: from vim
First editing session - create file
- Type vim hello.txt
- Press i to switch to insert mode.
- Type in "hello world".
- Press ESC to switch back to command mode.
- Type :wq this will write out the file and quit vim.
Change file
- Type vim hello.txt. The editor opens, the cursor is on the "h" of the "hello".
- Press ~ to change the case of "h" to be "H"
- Press 5l (lower case L) to move 5 characters to the "w".
- Press rW to replace "w" by "W".
- Press o to switch to insert mode on the line below.
- Type in: "and welcome to vim!"
- Press ENTER and continue typing: "This is the 3rd line.
- Press ESC to switch back to command mode.
- Type :wq this will save the file and quit vim.
Visual Insert
- Type vim hello.txt. The editor opens, the cursor is on the "H" of the "Hello".
- Press 2yy followed by p. The first 2 lines were yanked into memory (copied) and the pasted above the current line.
- Press Ctrl-v to switch to visual mode.
- Press 2j to mark 2 more lines under the current line.
- Press I (Shift i) to switch to insert mode.
- Type in "vim! "
- Press ESC
- After a second "vim! " will appear on the second and third lines as well.
- Type :wq to write file and quit vim.
Visual Append
- Type vim hello.txt.
- Press $ (Shift-4) that will jump to the end of the current line.
- Press Ctrl-v to switch to visual mode.
- Press j 3 time to mark 3 additional rows.
- Press A (Shift a) to switch to insert mode appending to the end of the row.
- Type in " end"
- Press ESC
- After a secon " end" will appear at the end of 3 other lines.
- Type :wq to write and quit.
Modes of vim
- Normal Mode (aka. Command Mode or Navigation mode)
- Insert Mode
- Command line
- Visual Mode
ESC
- ESC
Before learning anything else, it is worth remembering that pressing ESC several times will always bring you back to Normal mode.
ESC - get out of trouble and back to Command mode
Save (write) file
- :w
:w Write under current filename.
:w filename Save-as.
Quit
- :q
- :q!
- :wq
These are the ways to quit vim. (when in Normal mode press either of the following:)
:q quit - If there was no change since last write.
:q! force-quite - Abandon changes.
:wq write-and-quit
Basic moves
- h
- j
- k
- l
- 0
- ^
- $
- :20
- :goto 40
Remember, you can use these in Normal mode only.
Move around h (left) j (down) k (up) l (right) or the 4 arrows
0 - jump to beginning of the line
^ - jump to first non white-space of the line
$ - jump to end of line
:20 - go to line number 20
:goto 40 - go to character 40
Switch to insert/edit mode
- i
- I
- a
- A
- o
- O
i - (insert) right before (to the left of) the current character.
I - (Insert) right before first non-blank in the current line.
a - (appaned) right after the current character.
A - (Apppend) right after end of line.
o - add new empty row below.
O - add new empty row above.
Remember, ESC will take you from insert mode back to normal mode.
Delete row/word/character
- dd
- dw
- x
dd delete current row
dw delete word
x delete current character
Undo - Redo
- u - undo
- :redo
u - undo
:redo - redo
Copy/Paste (Yank/Paste)
- yy
- p
- P
Yank: yy
Yank 4 rows: 4yy
Paste: p
Paste: P
Switch characters
- xp
xp - switch current and next character.
Exercise: vim intro
- Take the examples in the first few slides and type them yourself.
Exercise: vim editing
- Create a text file and write a short-story.
- Involve deleting characters, words, and lines.
- Switch between Insert mode and Normal command mode.
- Save the file, quit, open it again.
More of Vim
Repeate commands
Many key combinations can be preceeded with a number that will indicate the number of repetitions we would like to have.
3dd Delete 3 rows
4l Move 4 characters to the right.
5(left-arrow) Move 5 characters to the left.
2dw Delete 2 words.
Repeat last change
-
.
-
Make some change. (e.g. switch to insert mode, type in "hello world"; click ESC)
-
Press .
-
The text "hello world" will be inserted again.
-
Press 3.
-
The text "hello world" will be inserted 3 more times.
Search
search forward: /
search backward: ?
search again: n or N (in reverse)
search current word: *
search current word backwards: #
:set ignorecase
Search and replace: substitute
- /
- s
:%s/Old/New/ Replace Old by New.
:%s/^ /\t/ Replace leading 4-spaces by a tab.
:%s/$/./ Put a . at the end of every line.
:'<,'>s/... Subtitute in range.
To select range press v and then use the navigation keys. Then hit :
Search and replace: substitute modifiers
:%s/Old/New/c Ask for confirmation before every substitute.
:%s/Old/New/g Multiple replaces on the same line.
:%s/Old/New/gc Both.
help
- :help
:help
:h
- It will split the window.
- You can jump between the help window and the other editor window by Ctrl-w Up and Ctrl-w Down.
- You can close the help part by typing :q while you are in the help window.
Editing another file
:e filename
Switching buffers
- :ls
- :bn
- :bp
- :bN
- :bd
- :b TAB
:ls list buffers
:bn next buffer
:bp previous buffer
:bN switch to buffer N
:b TAB cycle thorugh the names of the files in the buffers
:bd delete current buffer
:bd3 delete buffer number 3
Switching between last two buffers in vim
- Ctrl-6
- Ctrl-^
:ls
a - active buffer (the one we see in the window)
h - hidden buffer (not in the window and has unsaved changes )
# - alternate buffer
+ - buffer modified (unsaved)
To switch back-and-forth two buffers:
Ctrl-^ (Ctrl-Shift-6)
Ctrl-6
Visual modes
- v
- Shift-v
- Ctrl-v
v - Visual mode from here to cursor.
Shift-v - Visual mode this line till the line of the cursor.
Ctrl-v - Vertical visual mode. Block or rectangular edit.
Use the arrow key and other navigation keys to move cursor.
Ctrl-V, select rectangular, x - delete the selected characters.
Indentation
Press >> to indent current line
Press N>> to indent N lines
Press << to outdent current line
Press N<< to outdent N lines
Press == to autoindent currentline.
Press N== to autoindent N lines.
Select rows in Visual mode
Press > to indent the selected lines.
Press N> to indent N time.
Press < to unindent
Press = to autoindent.
As vim looses the visual selection we need a solution to allow us to indent more than once. Press '.' to repeate the previous action to further indent or press 'u' to undo the extra indentation. Alternatively one can map keys to keep the selection after indentation. Indentation commands
String completition in insert mode
-
Ctrl-N
-
Ctrl-P
-
Ctrl-N - by searching forward
-
Ctrl-P - by searching backwards
Shell commands
- :!ls
- :!pwd
:!ls
:!pwd
Jump to character in current line
- f
- F
- ;
- ,
f C will jump to the first occurance of C to the right.
F C will jump to the first occurance of C to the left
; will repeat f or F
, will reverse f or F
Any of those can be prefixed by number.
Windows
- m
- '
Ctrl-w s horizontal split same file
Ctrl-w v vertical split same file
:sp filename horizontal split with other file
:vsp filename vertical split with other file
Ctrl-w w Cycle through the open windows
Ctrl-w arrow Move between windows. (also Ctrl-w [hjkl] work)
Ctrl-w + Increase window
Ctrl-w - Decrease window (or use the mouse)
Ctrl-w = Reset windows (equalize them)
Ctrl-w _ Maximize current window vertically
Ctrl-w | Maximize current window horizontally
Ctrl-w r Rotate windows
Ctrl-w R Rotate windows in the other direction
Ctrl-w [HJKL] Move windows
Ctrl-w c Close window
:q Close current window
:only Close every window except the current one
:help ctrl-w
Mark
ma Will set mark a.
m{a-zA-Z} Any letter can be used as a mark.
'a Jump to mark a in current buffer.
Registers
- "ayy
- "Ayy
- "ap
"ayy will copy the current line into register 'a'
"Ayy will append the current line to the register 'a'
"ap will paste the content of register 'a'.
To list the content of all the registers, type :reg
Change encoding
- set fileencoding
- converted
- utf8
Sometimes you have files in encodings you don't want, sometime you'll see in the status line that the file is
converted
. You can change the encoding of the file to utf8 by:
:set fileencoding=utf8
:w
Current filename
Copy (yank) the current file (buffer) name to clip-board so you can paste it using p.
:let @" = expand("%")
Copy the full path:
:let @" = expand("%:p")
Show rownumbers
- :set number
:set number - show line numbers
:set nonumber - hide line numbers
:set number! - toggle line numbers
Show invisible characters
-
:set list
-
tab
-
new-line
:set list
:set nolist
:set list!
~/.vimrc
- .vimrc {% embed include file="src/examples/vimrc)
Embed vim settings in source code
- vim:
In a C file:
/*
vim:sw=4:
*/
In a Perl file
# vim:expandtab
# vim:tabstop=4
In a JavaScript file
// vim:expandtab
// vim:tabstop=4
modline turns this feature on
modlines sets the number of lines to check
- set modline}
- set modlines}
:set modline
:set modlines=10
TABs vs. Spaces
- tabstop = 4
- softtabstop = 4
- shiftwidth = 4
- expandtab
Videos
- Vimcasts - free screencasts about the text editor Vim
- Vim for Perl development (video)
- More Instantly Better Vim OSCON 2013 talk by Damian Conway
Vim Resources
- Run vimtutor on the Linux command line.
- Using the menus of gvim to find the command and then execute the key combination
- Use vimium in Chrome or vimperator for Firefox to reuse the same key-bindings.
- Vim Adventures
- vimgifs
- Vim by Patrick Schanen
- Vim Videos by Derek Wyatt
- usevim
- Best of Vim Tips
- Seven habits of effective text editing by Bram Moolenaar the developer of vim.
- vimregex
- Learn Vimscript the Hard Way
- VimAwesome Awesome Vim Plugins from accross the Universe.
- vim
- vi/vim graphical cheat sheet