# Vim: Editing

Disclaimer: Snipcademy is supported by your purchases of our recommended products through Amazon Services LLC and the ads posted on our website. All products listed are thoroughly researched, curated and reviewed so that viewers are given the best recommendations. Thank you and happy browsing!

01. How to insert and append in Vim (i, a, I, A)

Remember that Vim is known as a modal editor, meaning its state depends on what "mode" it's in.
When vim starts up, it's in command mode.
Command mode is how you navigate, like we are now!

Now let's say we want to insert some text.
We can change into insert mode using several keys, depending on where we want to insert our cursor:
i - before the cursor
a - after the cursor
I - at the beginning of the line (first non-white space)
A - at the end of the line

Now we can start inserting things!

Let's say this is some cool text block,
and you want to insert at the beginning of the line.
Simply go to the line, and press "I"!

*** Remember to hit the ESC key to return to COMMAND mode! ***

// Find the two error in this Java code and fix it
x = 0;
if (x = 0)
System.out.println("I love Vim.")

# Or python if you want
L = ["vim", "is", "so", "!"]
for n in L
rint(n)

BTW or LOLCODE...
HAI 1.2
CAN HAS STDIO?
VISIBLE "CAN I LUVS VIM!!!1!"
KTHXBYE

Solutions:
Java: Make the "=" in the parenthesis to make "==" and insert a semicolon at the end using "A;"
Python: add a ":" at the end of the for statement, and a "p" in front of "rint"
LOLCODE: nothing's wrong with this one -- just wanted to use it as a fun example!

02. How to use Vim to insert, open and join lines (o, O, J)

Let's say you want to append some code beneath a line.

To do this, we had to go to our line, then press "A" and ENTER.
Remember that even two key strokes is sometimes too much in Vim.

Thankfully, Vim makes this process much easier with the "o" key.
Think of "o" as in open.

o - open a line below
O - open a line above

def sumAndSquare(a, b):
#comment here
sum = a+b
# another comment here
return sum ** 2

We can also join lines together.
J - join current line with the one below

Turn this
block of text
back into one line!

Review Questions:
1) Insert a period at the end of this line
2) Insert a "please" in front of the following sentences:
don't text and drive.

Solutions:
1) A.
2) on each line, type "IPlease"

03. Undo and redo in Vim (u, U, ctrl+r)

Let's learn how to undo and redo our editing before we move on.

We can undo with the following commands:
u - undo the latest change
3u undoes the last 3 changes
U - undo an entire line

We can use a "redo" key to redo the undo:
ctrl+r - redo

Try making some mistakes in the text here and reverting back to original changes.

Review Questions:
1) Insert a #2 review question below.
4) Now insert a #4 review question above.

Solutions:
1) "o"
2) "O"

04. How to replacing text in Vim (r, R, ~ (tilde), c, C)

We can use text replacements to edit more efficienctly.

To replace a character, we simply place our cursor under the character we wish to replace, press "r", then the new character.

r - replace a selected character with another
ra replaces selected character with "a"
3ra replaces three characters with "a"

Try replacing the incorrectly spelled character in this sentance.
Now try replacing four characters in this sentence.

We can also use substitution:
s - replace a char and add more
S - change the entire line

If you want to change the case of a letter, simply press the squiggly ~ (tilde).
This is located on the top left corner of your keyboard, below the ESC key.

~ - change the character case
3~ changes 3 character's case

Now remember I told you that vim is a modal editor?
There's another nift mode called REPLACE. With it, you can write over existing text.

R - activate Replace mode

Press "R" and you replace as you go!

For a quick change, you can use the "c" key
c - delete and insert
cw - delete selected word and insert a new on
cc - change entire line, same as "S"
C - change line from where your cursor is

Try changing this line $from the "$" symbol!

Review Questions:
1) Insert some random text into this sentence, then undo with one command.
2) Redo what you just undid in question 1.

Solutions:
1) "U"
2) ctrl+r
05. Deleting text in Vim (x, X, d, . (period))

So far we've covered, inserting and replacing.
Now let's hop on over to the deleting!

*** Remember to hit "u" to undo a change! ***

There are many ways we can delete text in a file.
x - delete the character at the cursor
3x deletes current character + 2 characters to the right of cursor
X - delete character to the left
3x deletes 3 characters to the left of cursor

Try dexletxing axxll txxxxhe "x" chxaraxcters hxere!

We can also delete words or WORDs at a time.
d - delete something
dW deletes to beginning of next WORD
dE deletes to end of next WORD
dB deletes to beginning of previous WORD

Let's try deleting all the words here.

Another nifty tool:
. - repeat any command

I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!
I love Justin Bieber!

Review Problems:
1) What's the difference between word and WORD? How many words and WORDs does www.youtube.com have?
2) How do you activate REPLACE mode?

Solutions:
1) WORDs ignore punctuations. www.youtube.com has 5 words and 1 WORD.
2) "R"

06. How to Cut and paste in Vim (d, p, P)

Deleting doesn't just delete keys, it copies it into the buffer (or the clipboard).
Sound familiar? This is exactly how "cut" works.

To "paste" this snippet, use the following:
p - paste below cursor line
P - paste above cursor line

dd - deletes (or cuts) current line

Let's delete this line...
and make it reappear below!

Can you make "superfragilisticexpialidocious" disappear?
Now paste it here! ->

Wow! What is this sorcery? Are you a wizard? Can you make this rabbit disappear too?

(\__/)
(='.'=)
(")_(")

Don't forget to make it re-appear!
Or the kids will be very upset...

Great job! In the next tutorial we'll look at copying and pasting.

Review Questions:
1) Delete every word above that starts with a vowel.
2) How would you undo the last 5 words you just deleted?

Solutions:
1) You can go to the front of each word and type "dw"
2) "5u"

07. Copying and pasting in Vim (y, Y)

Now we can learn a very useful command that we use every day - copying and pasting.
In vimland, "copying" is refered to as "yanking."

We can use the "y" key, along with several variations of it to yank text:
y - yank (or copy)
yy current line
5yy this line, and 4 more under it
yw to the end of this word
y\$ to the end of the line
y^ to the first non-white space
ygg to the beginning of the file
yG to the end of file
y20G yanks to the 20th line of the file
Y - yank entire line

Remember that p or P is used to paste!

These are just a fraction of the various ways to yank text.
Can you think of how you would yank from HERE to the beginning of this sentence?

Review Questions:
1) If you delete something using "d", is it gone forever?
2) How would you cut and paste a character?

Solutions:
1) No, it is copied to the clipboard. Paste it somewhere using "p".
2) Use "x" to delete it, and "p" to paste it".

08. Searching for characters in Vim (f, F, t, T, ; (semicolon), , (comma))
We can quickly navigate a line by searching characters.

We can do this using the find and til keys:
f - to the first character ahead
"fa" moves cursor to "a" ahead
F - to the first character behind
"Fa" moves to the first "a" behind
t - up to the first character ahead
T - up to the first character behind

The difference between the "f" and "t" is that one moves up to the letter, and the other moves directly on top of the letter.

Try searching for any letter in any line in this file.

Since it can get cumbersome if we have multiple occurences before our desired destination, we can use the repeat commands:
; (semicolon) - repeat the command
, (comma) - reverse the command; search backwards of what you started with

Try getting to the last "e" character in this sentence. Now try going back to the first "e."

Review Questions:
1) What is the fastest way to copy a line?
2) How do you paste above your current line?

Solutions:
1) "yy"
2) "P"
09. Searching for words in Vim (/ (forward slash), ? (question mark), n, n, #, *)
We're almost done!

To search the entire file of words, use:
/ - to start the search, and to go to next
? - to go to previous

We can use this to search a long file for a variable name or any words.

To move to the next occurence of the word that your cursor is under, use:
* - move to the next occurence
# - move to the previous occurence

Put your cursor over the move word, and hop over to the previously occuring command.

Review Questions:
1) How would you find all occurring o's in this line?
2) What key repeaets a given find command?

Solution:
1) "fo" if you're finding ahead, "Fo" if you're finding behind.
2) ";"

!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!~!                         !~!
!~!    CONGRATULATIONS!!!   !~!
!~!    You have completed   !~!
!~!       this course!      !~!
!~!                         !~!
!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
Remember to practice daily to improve!