Unix/Linux - The vi Editor Tutorial
Welcome, aspiring programmers! Today, we're diving into the world of the vi editor, a powerful tool that's been around since the dawn of Unix. Don't worry if you've never touched a command line before - we'll start from the very beginning and work our way up. By the end of this tutorial, you'll be editing files like a pro!
Starting the vi Editor
Let's begin our journey with the basics. To open the vi editor, you simply need to type vi
followed by the filename in your terminal. For example:
vi myfile.txt
If the file doesn't exist, vi will create it for you. Isn't that nice? It's like vi is saying, "Don't worry, I've got your back!"
Operation Modes
Now, here's where things get interesting. Vi has two main modes:
- Command Mode
- Insert Mode
Think of Command Mode as your "home base." It's where you start, and where you'll return to perform operations like copying, pasting, and navigating. Insert Mode, on the other hand, is where the magic of writing happens.
To switch from Command Mode to Insert Mode, press i
. To get back to Command Mode, hit the Esc
key. Remember this - it's like the secret handshake of vi users!
Getting Out of vi
Alright, you've opened vi, but how do you exit? Don't panic! Here's a table of commands to help you out:
Command | Action |
---|---|
:q! | Quit without saving |
:wq | Save and quit |
:x | Save and quit (same as :wq) |
Just type these commands in Command Mode, and you're good to go!
Moving within a File
Navigation in vi might seem tricky at first, but once you get the hang of it, you'll be zipping around your files faster than you can say "cursor keys." Here are some basic movement commands:
h - move left
j - move down
k - move up
l - move right
I like to remember these as "h" for left because it's on the left, and "l" for right because... well, it just makes sense that way!
Control Commands
Vi has a plethora of control commands. Here are a few to get you started:
Ctrl + f : Page forward
Ctrl + b : Page backward
G : Go to the last line
1G : Go to the first line
Editing Files
Now we're getting to the good stuff! To start editing, remember to switch to Insert Mode by pressing i
. Then, type away to your heart's content. When you're done, hit Esc
to return to Command Mode.
Deleting Characters
In Command Mode, you can delete characters with these commands:
x : Delete the character under the cursor
dw : Delete the word
dd : Delete the entire line
I once accidentally deleted an entire paragraph with dd
. Let's just say I learned the importance of the undo command (u
) pretty quickly after that!
Change Commands
Want to change text? Vi's got you covered:
cw : Change word
cc : Change entire line
C : Change from cursor to end of line
Copy and Paste Commands
Copying and pasting in vi is a breeze once you know the commands:
yy : Yank (copy) a line
p : Paste after cursor
P : Paste before cursor
Think of "yy" as "yoink yoink" - you're yoinking that line right out of there!
Advanced Commands
Ready for some advanced moves? Try these on for size:
. : Repeat last command
~ : Change case of character
Word and Character Searching
Need to find something? Use these commands:
/pattern : Search forward for pattern
?pattern : Search backward for pattern
n : Repeat search in same direction
N : Repeat search in opposite direction
Set Commands
Vi allows you to set various options. Here are a few useful ones:
:set nu : Show line numbers
:set nonu : Hide line numbers
:set ic : Ignore case in searches
Running Commands
You can run shell commands without leaving vi. How cool is that?
:!command : Run a shell command
For example, :!ls
will list the files in your current directory.
Replacing Text
Need to make a lot of changes quickly? Try these:
:s/old/new : Replace first occurrence of 'old' with 'new' on the current line
:s/old/new/g : Replace all occurrences of 'old' with 'new' on the current line
:%s/old/new/g : Replace all occurrences of 'old' with 'new' in the entire file
Important Points to Note
- Vi is case-sensitive. 'A' and 'a' are different commands.
- Always know which mode you're in. If things aren't working as expected, you might be in the wrong mode.
- Practice, practice, practice! Vi has a learning curve, but once you master it, you'll be editing at the speed of thought.
Remember, becoming proficient with vi is like learning to ride a bicycle. It might seem wobbly at first, but with practice, you'll be zooming around your text files with ease. Don't get discouraged if it feels awkward initially - we've all been there!
So, fire up your terminal, open vi, and start exploring. Before you know it, you'll be editing like a pro, impressing your friends with your command-line prowess. Happy coding!
Credits: Image by storyset