First-time users of vi might find the learning curve to be somewhat steep. Don't worry! The commands that seem completely foreign right now will soon seem like second nature once you first learn the basics and keep practicing. Before long, you will be editing files, creating documents and moving text within your files quickly and efficiently.
To editing with vi is very simple; open a terminal session on your machine and type in vi . In this example, we'll name our example file foo, so to start editing a new file called foo, I'll type 'vi foo' (without the quotes)
You should see something similar to this on your screen:
~
~
~
~
~
~
~
~
~
~
~
~
"foo" [New file]
The tilde (or squiggle, for you non-technical people) character is used to show any blank lines beyond the end of the file. If your file contains nothing but lines of tildes, this means your file doesn't contain any text nor characters yet (which we would expect from our brand new file named foo). Vi has two modes, insert and command mode. Insert mode lets the user enter text from the keyboard and command mode is generally used for manipulating text. Since we want to insert text into our new file, we need to be in insert mode. To enter insert mode, simply type the letter 'i' and type the sentence, "Hello World". Once we are done editing our file, we want to exit insert mode and enter command mode. By pressing the Escape key (ESC), we have now exited insert mode and are now in command mode. From here we want to save our new file foo and exit vi. We can type ':wq!' (without the quotes) to save the file and exit vi. You should see something similar to this on your screen:
"foo" [New file] 1 line, 13 characters
Congrats! You just created your first text document using the vi editor. Now that you know the very basics of how to create and save a file using vi, you can move on to different ways to enter and edit your text.
Moving around in vi is pretty easy; you can also use the letters 'h' (left), 'j' (down), 'k' (up) and 'l' (right) as navigation keys. Some systems will also allow you to use the arrow keys to navigate, but it is generally accepted within the UNIX community that using the letters to navigate is more efficient.
Let's edit our newly created file. Remember to edit our file named foo, we can type: vi foo. You should see something similar to this:
Hello World!
~
~
~
~
~
~
~
~
~
"foo" 1 line, 13 characters
Our previously saved text is shown on the screen. Now we want to add another line to our file that says "This is my greatest creation!" To open another line for editing directly under the line our cursor is currently on, we can press the 'o' key. This will automatically enter insert mode and place the cursor on the line directly below our first line. Now we can type our new text "This is my greatest creation!" Save the text and exit vi by typing ESC, then 'wq!' Hey, this vi stuff is pretty fun! Deleting characters is pretty easy too; just type 'x' over each character you want to delete.
With vi, you can really do just about any kind of text manipulation that you want quickly and efficiently. Let's add some more text at the end of one of our lines. To do this, type 'vi foo' to open then file and then type the '$' character (moves cursor to the end of the current line) then type the 'a' key. The 'a' key automatically puts us in insert mode one character after the current cursor position. It is a really handy shortcut for insert mode positioning. Now we can type whatever we would like to append to the first line. Once you finish typing, exit insert mode (ESC) and save (wq!).
Copy and pasting is really easy in vi. Let's copy and paste the second line of our file, foo. To open the file, type 'vi foo'. Once the file is open, move the cursor to any character on the second line of text using the 'j' or down arrow key. Once your cursor is on the second line of text, type 'yy' - this shortcut will copy all the text on the current line. To paste, the copied text, you can type the 'p' key. This should place a copy of the selected line of text directly under the original line of text. Pretty simple, isn't it?
Several websites have some very handy vi command reference indexes and I would recommend you bookmark one of those sites for easy access. One of my favorites is this site. And since you have already learned some of the very basic commands and concepts, you can build upon that knowledge by trying new shortcuts and commands. Good luck and happy editing!
Published by Jon Hardt
I am a regular guy who likes to play outside, mainly golf, football, and running marathons. Indoors, I am a huge computer geek and enjoy tinkering with various types of hardware. I also am a huge boxing fan... View profile
- PHP and Web Hosting with MacMany Mac users might not know that your OSX operating system comes equipped with the software required to host your own web pages. Learn how to start up your server, enable it with the easy and fun-to-use language PHP...
- How to Help a Child with IBD: a Mother's PerspectivePediatric IBD is traumatic for both the child and the parents. Read our interview with the parent of an IBD patient and learn what to do if you suspect a child you know has IBD or IBS.
- How to Tweak Azureus (Java Bit Torrent Client) to Obtain the Best Download Speeds!You can download the Azureus Bit Torrent client for free, and start downloading anything you want! This guide teaches you how to tweak Azureus to get the fastest download speeds.
How to Throw a Simpsons Treehouse of Horror Halloween Reference Hunt PartyHave a different kind of Halloween party this year. Learn how to throw a Simpsons Treehouse of Horror Halloween Reference Hunt Party.- How to Write SongsUsing basic music theory knowledge about scales and progressions, songwriters will find many new chords to write songs with. Because they come from the same key they will have notes in common and sound good together....
- GNU & Linux the Dynamic Duo
- How to Become a Successful Online Writer
- Top Ten Applications Every Linux User Should Have
- Applications and Software for Linux Ubuntu
- MaraDNS an Easy to Use Open Source Security-Aware DNS Server
- Compiling the Linux 2.6 Kernel from Scratch
- Learning Linux in the Fast Lane
- Vi is included in every modern UNIX and LINUX distribution
- Vi is very powerful, yet lightweight



