Copy and Paste from Second File
- Open the second file while in command mode with
:e other-file.md
- Navigate to the start of to-be-copied text
- Type
v
to start visual mode
- Navigate to the end and type
y
copy the text
- Type
:bp
to go back to the first document
- Navigate to the position where the text is to be inserted
- Type
p
to paste the text after the cursor positon
Indent multiple Lines
- Start visual line mode with
V
- Select lines by movement
- Indent with
>
- Add more indent with
.
- Undo last indent with
u
Rename local Variables
- Go to the variable.
gd
to go to the declaration
cgn
and type the new name
- ESC to go in command mode
- Repeat renaming next usage with
.
Commands
Here are some list of vi commands.
Movement
Keys | |
---|
h l | Go left, right |
j k | Go down, up |
w W | Go to start of word (spaces separate words) |
e E | Go to end of word (no punctuation) |
b B | Go backward by word (no punctuation) |
0 $ | Go to the start, end of the current line |
^ | Go to the first non-blank character of the current line |
* | Go start of next same word |
- + | Go start of current, next line |
H M L | Go top, middle, bottom of the screen |
( ) | Go by sentence |
{ } | Go by paragraph |
[[ ]] | Go by section |
gg G | Go to top, bottom of file |
Keys | |
---|
zEnter | Scroll until cursor is at the top of screen |
z. | Scroll until cursor is in the middle of screen |
z- | Scroll until cursor is at the bottom of screen |
Ctrl+u | Page up |
Ctrl+d | Page down |
Edit
The following commands turns vi in edit mode except r.
| |
---|
i a | Insert before, after cursor |
I A | Insert at start, end of line |
O o | Insert new line above, below cursor |
r | Replace character at cursor |
J | Join current line with line below |
C | Cut from cursor to the end of line and enter edit mode |
Undo
| |
---|
u | Undo last change |
U | Undo all changes on line |
Ctrl+r | Redo last undo |
Cut
| |
---|
dd | Cut line |
dw | Cut word |
x | Cut character |
X | Cut character left from cursor |
D | Cut from cursor to the end of line |
Paste
| |
---|
p | Paste after cursor |
P | Paste at cursor |
Before pasting text with indentation and avoid auto indentation from vi, enter:
Visual Mode
In visual mode, use movement commands to extend selection.
| |
---|
v | Start visual mode |
V | Start linewise visual mode |
y | Copy marked text |
d | Delete (for cut and paste) |
< > | Indent |
. | Repeat indent |
~ | Switch case |
Multiple Files
| |
---|
:e file | Type path and filename, use TAB to autocomplete |
:bn | Go to next buffer |
:bp | Go to previous buffer |
Multiple Windows
| |
---|
Ctrl+ws | Split window horizontally |
Ctrl+wv | Split window vertically |
Ctrl+ww | Switch between windows |
Ctrl+wq | Close window |