Level 2 Linux Exercises
Vim Quick Reference
Make sure to save your work frequently!
- To begin editing a file:
vim file_name.extension
For example:
vim prog1.cpp
- Command Mode
To navigate in the document to prepare to edit here are a few of the choices available:- Go to the first line
1G
- Go to the first line
H
- Go to the last line
L
- Move the cursor down one line
j
- Move the cursor up one line
k
- Move the cursor right one character
l
(lower case L) - Move the cursor left one character
h
- Go to the beginning of a line
0
(a zero) - Go to the first non-black character
^
- Go to the end of the line
$
- Go forward one full screen
control
f
- Go backwards one full screen
control
b
- Go forward half a screen
control
d
- Go backwards half a screen
control
u
- Go to the first line
- Other command mode options are:
- Undo the last change
u
- To delete an entire line
dd
- To copy the current line
yy
orY
- To paste the copied line(s)
p
- Undo the last change
- Insert mode
To enter insert mode; once insert mode has been entered, you can type normally inserting text. When done, you will need to exit insert mode with the escape key or by holding down the control key and pressing c at the same time:
- Insert before the cursor
i
- Append after the cursor
a
- Insert at the beginning of the line
I
- Append at the end of the line
A
- Insert before the cursor
- To end insert mode, press
escape key
orcontrol c
- Last line mode commands
Options for saving your work and quitting:
- To save your work
:w
- To save changes and exit (quit)
:wq
- To quit without saving
:q!
(**all changes will be lost!)
- To save your work
- Navigation options at last line mode include:
- Go to a particular line
:42
go to line # 42 - Find some text in the file
/text
- Find the next occurrence
/
- Search and replace
:%
s /to_replace/replace_with/<flags>
flags:g
// replace all
i
// case insensitive
c
// confirm before doing each replacement
- Go to a particular line
- Visual Mode
To enter Visual mode:v
- Once in Visual mode, we can cut, copy and paste:
- To delete text and copy it to the clipboard:
d
- To copy (yank) text:
yy
- To paste text that has been copied:
p
- To delete text and copy it to the clipboard:
- Or we can alter indentation:
- To add indentation to the right:
>
- To reduce indentation (move to the left):
<
- To add indentation to the right:
- To end visual mode, press
escape key
orcontrol c
- Advanced vim Commands:
Split Screen: One of the really great features is working in split screen mode; this allows you to view multiple files simultaneously- Split horizontally:
vim
–o file1.cpp file2.cpp
- Split vertically:
vim –O file1.cpp file2.cpp
Split all of your .cpp files:
vim –O *.cpp
Switch between screens:
<cntrl> ww
- Split horizontally:
- Other Commands for multiple files
- Open a file next to the current code, vertically
:vs filename.extension
- Open a file below the current code, horizontally
:sp filename
- Save and close all open files
:wqa
- Open a file in a new buffer
:e filename.extension
- Cycle through available files
tab
- Open a file next to the current code, vertically