Chapter 05: Vim Basics: Editing Your Code (Insert Mode)
published:
Hello, Vim Editor!
You've mastered moving around in Vim's Normal Mode. That's fantastic! But to actually change or write new code, you need to switch into a different mode called Insert Mode. Think of Insert Mode as your "typing mode" – this is where your keyboard keys actually type letters onto the screen.
Switching to Insert Mode: Your Typing Spells
There are a few magical ways to enter Insert Mode from Normal Mode:
i: The most common spell! Pressito enter Insert Mode before your cursor.a: Pressa(for "append") to enter Insert Mode after your cursor.o: Presso(for "open") to create a new line below your current line and enter Insert Mode there.O(capital O): PressOto create a new line above your current line and enter Insert Mode there.
When you are in Insert Mode, you will usually see -- INSERT -- at the bottom
of your Vim window. This is a very important sign that you can start typing!
Going Back to Normal Mode: The Escape Key
When you're done typing and want to move around again or use other Vim commands, you need to go back to Normal Mode.
- Press the
Escapekey (usuallyEscin the top-left corner of your keyboard).
As soon as you press Escape, -- INSERT -- will disappear from the bottom,
and you'll be back in Normal Mode, ready to use your movement spells!
Deleting with Spells: dd, x
You can also delete things without going into Insert Mode, directly from Normal Mode!
dd: This powerful spell will delete the entire line your cursor is on. (Typedtwice quickly!)x: This spell will delete the single character under your cursor.
Copying and Pasting (Yank and Put!): yy, p
Vim has its own special words for copy and paste!
yy: (for "yank") This spell will copy the entire line your cursor is on. (Typeytwice quickly!)p: (for "put") This spell will paste the copied line below your current line.
Putting it Together: Practice Time
- Open a text file with Vim (
vim practice.txt). - Press
Escapeto ensure you're in Normal Mode. - Use your movement spells (
h,j,k,l,w,b,0,$) to move your cursor around. - Press
ito enter Insert Mode. Type some words! - Press
Escapeto go back to Normal Mode. - Move your cursor to a different spot.
- Press
ato enter Insert Mode. Type some more words! - Press
Escape. - Move your cursor to the middle of a line and press
xa few times to delete characters. - Move your cursor to a line and press
ddto delete the whole line. - Type
o, enter Insert Mode, type a new sentence, thenEscape. - With your cursor on a line, type
yyto copy it. - Move to a new line and press
pto paste it!
You're now learning to both move around and edit your code in Vim. Awesome job, Code Wizard!