Vim For Dummies
What is Vim and Why would you use it:
Vim is a powerful text based application that can be used to create, modify or save text based files. The following tutorial will introduce you to the basic functionality of vim as well as how to utilize some of its most powerful features.
Modifying the contents of a file within the Unix/Linux command line environment may be difficult for any Linux user, especially if they are new to the operating system. There are many different ways to modify the contents of a file within the UNIX command line, one method in particular is by using the Vim command line text editor.
Create and Open a File with Vim:
To create or open an existing file, you must input the UNIX command “vi” followed by the name of your new file – if the file does not yet exist, vim will automatically create it and open it for editing. For example, if you would like to create a file called tutorial.txt, simply issue the command:
[Dillon@localhost Desktop]$ vi tutorial.txt
If the file did not yet exist, you will be prompted with a screen similar to the following. The cursor is designated by a rectangle, and will automatically be placed on the first line. Since this is a new file, you should see numerous tilde characters ( ~ ) – one on each line. These tilde symbols will not affect your file, as vim utilizes them as a placeholder for an unused line. Lastly, vim displays information regarding your file at the bottom of the window such as the file name (in this case tutorial.txt).
A ~ ~ ~ ~ ~ ~ "tutorial.txt" [New File] 0, 0 - 1 All
Vim contains two functioning modes, each permitting you to perform different tasks within the text editor. When you open vim, you are automatically placed in “command mode”. This mode will not let you modify the data in your file, although it will accept vim commands. The second mode is called “insert mode” which allows you to modify the data within your file.
To navigate from command mode to insert mode – press the “i” or “a” button on your keyboard
To navigate from insert mode to command mode – press the “escape” button on your keyboard
Navigating around your file with Vim:
Vim allows you to navigate around your file by moving the cursor with your keyboard. The location of the cursor can be used when you issue some commands and will be further explained later in this tutorial. The following keys can change the location of the cursor. On some machines, you could also use the standard arrow keys instead.
K(up) – H(left) – L(right) – J(down)
Editing a File with Vim:
To begin editing the contents of your file, press the “i” button on your keyboard. This will place you in insert mode and let you make changes to the data at the current cursor location. You could also enter insert mode by pressing the “a” button on your keyboard as this will let you insert or append text after the cursor position.
(Notice that the bottom banner will change to “– – INSERT – –“, indicating that you are now in insert mode).
Once you have finished editing your file, press the “escape” button on your keyboard and return back to command mode.
You are now using the vim editor! Type your text here... ~ ~ ~ ~ ~ ~ -- INSERT --
Saving a File and Exiting Vim:
Vim provides numerous ways to save the contents of your file. First, navigate to command mode by pressing the “escape” button on your keyboard and input one of the following command options:
(Be sure to input the colon symbol preceding the command!)
|:w||save the contents of your file|
|:wq||save the contents of your file and exit vim editor|
|:q!||exit vim editor without saving your changes|
Utilizing Keyboard Shortcuts:
Now that you have become familiar with the basic concepts and usage of vim, the rest of this tutorial focuses on the features that I’ve found to be most useful with the command line editor.
While in command mode, you can start typing any command that you would like to issue followed by pressing the “return” or “enter” keyboard button. Be advised, commands within vim are case sensitive and some require that you enter a preceding colon symbol.
Copy and Paste Data with Vim:
Vim provides the functionality to copy and paste text within your file. This feature could help tremendously if you would like to quickly move data around your document with minimal effort. There are multiple ways that you can copy data such as single words, lines, or multiple lines at a time.
Keep in mind, these commands are all issued via command mode.
To copy a word, position the cursor on the word that you would like to copy and issue the command “y”. Next, position the cursor to the new location (where you want to copy the word to) and press the “p” keyboard button.
In the following example, we will copy the word “pizza” and paste it below.
Copy Word: Words: Computer Pizza - position cursor and press "y" keyboard button once Mouse Paste Word: Pizza - position cursor to new location and paste by pressing the "p" keyboard button
To copy a line, first position the cursor on the line that you would like to copy and issue the command “yy”. Next, position the cursor to the new line (where you want to copy the data to) and press the keyboard button “p”.
In the following example, we will copy the line that contains “pizza” and paste it below.
Copy Line: Words: Computer Pizza - position cursor and press "yy" keyboard button twice Mouse Paste Line: Pizza - position cursor to new line and paste by pressing the "p" keyboard button
As similar to the previous examples, to copy multiple lines, position the cursor on the first line that you want to copy. Issue the command [number]yy (replace number with the number of lines to copy) and position the cursor to the new line (where you want to copy the data to). Lastly, press the “p” keyboard button to paste the lines to the new location.
In the following example, we will copy the lines that contain “computer”, “pizza” and “mouse” and paste them further below.
Copy Multiple Lines: Words: Computer - position cursor and issue"3yy" command Pizza Mouse Paste Multiple Lines: Computer - position cursor to new line and paste by pressing "p" keyboard button Pizza Mouse
Undo or Redo Mistakes with Vi:
One of the most convenient features of vim is the ability to undo your mistakes. This can be issued by pressing the “u” key while in command mode.
If you would like to redo your changes, press the “ctrl” and “r” keyboard buttons
Searching for Specific Text with Vi:
Vim includes a powerful search feature that allows you to quickly locate matching words within your file. To search for a keyword, enter command mode and input the symbol “/” or “?” followed by the word that you would like to search for.
For this example, we are searching for the word “pizza”:
- /pizza – performs a forward search and matches all contents after the current cursor location
- ?pizza – performs a reverse search and matches all contents before the current cursor location
Once you issue your search, pressing the “n” key will move you directly to the next matching occurrence.
Enable Syntax Highlighting:
In some instances, vim editor will not automatically enable syntax highlighting. This could be a nuisance if you are modifying a script or program via command line. To enable syntax highlighting, enter command mode and issue the command “:syntax on”.
Vim also contains additional color schemes that can be changed by issuing the command “:colorscheme ” followed by the tab keyboard button to choose a new syntax highlighting color scheme.
||enables syntax highlighting|
||disables syntax highlighting|
No Syntax Highlighting: <?php $currentYear = “2014”; echo “Hello World!\n”; echo “The current year is “ . $currentYear; ?> Syntax Highlighting: <?php $currentYear = “2014”; echo “Hello World!\n”; echo “The current year is “ . $currentYear; ?>
Enable Line Numbers:
Line numbers can make debugging alot easier if you are editing a script or program with vim. To enable line numbers, issue the command “:set number”.
||enable line numbers|
||disable line numbers|
Line Numbers: 1 <?php 2 3 $currentYear = “2014”; 4 echo “Hello World!\n”; 5 echo “The current year is “ . $currentYear; 6 7 ?>
Note: You can also learn VIM by playing this game.
This article is free and open source. You have permission to republish it under a Creative Commons license with attribution to the author and Teknixx.com