Coming from Windows, editing files in a console environment seemed terrifying. Oh sure my first word processor was Eight-In-One office suite, but that was an awfully long time ago. Also, Eight-In-One isn’t anything like Vi -- Linux’s venerable text-editor straight out of the late 70s. The newer version is referred to as Vim -- short for Vi IMproved. Vim has literally hundreds of changes and improvements on Vi, but some of the big ones include mouse support and syntax highlighting. In most Linux distributions running the `vi` command opens Vim with newer features disabled. I suggest using Vim whenever possible.
Before I go into any details about Vim you should know that the program already has it’s own
tutorial. You can launch this tutorial by entering the
vimtutor command in your
Vim has thousands upon thousands of shortcuts and commands. I’ll only be covering the most basic usage -- enough to get you through the rest of the tutorials.
Vim has a different paradigm than you’re probably used to. Files are opened in view mode. You will navigate in view mode and then switch into edit mode to make changes. Remember that while in view mode typing just executes commands.
Navigating in View Mode:
When you open a file you’ll be in view mode. You can navigate the file using the arrow keys (as you might expect) or by using the
(right) keys. You can also just to a particular line number by pressing : then entering the line number
and pressing enter.
:100 takes you to line 100 and so on. Here are a few other helpful
gg- Jumps to the top of the document.
shift + gor
G- Jumps to the bottom of the document.
shift + hor
H- Moves the cursor to the top of the current screen.
shift + lor
L- Moves the cursor to the bottom of the current screen.
:#s- Jumps to the given line number.
For deleting characters and lines you don’t need to enter edit mode. You can just use the following commands:
d- Deletes current selection.
dd- Deletes the current line.
x- Deletes the current character.
For making additions you have to enter edit mode. There are two ways to do this, either pressing i to insert or a to append. The append command will put the cursor after the current position, while the insert command will put the cursor before it. As a matter of practice, I only use the insert command.
You can leave edit mode by pressing ESC.
Saving Changes and Exiting:
:w - Write file / Save changes.
:q - Quit. If no changes were made.
:q! - Quit reverting changes.
:wq - Save and exit.
If you really don’t like all this Vim stuff there is a popular alternative called Nano. This text editor may already be installed on your distribution. If it is not you can always install it using:
sudo apt-get install nano - for Debian variants
sudo yum install nano - for CentOS and Fedora variants.
Nano has a single mode for viewing and editing, much like a traditional editor. By default directions for use are written on the bottom of the screen. Use Ctrl followed by the character to execute the command.
vim /path/to/file - opens Vim to edit a specific file. Creates the file if it doesn’t already exist.
nano /path/to/file - opens Nano to edit a specific file. Creates the file if it doesn’t already exist.
vimtutor - opens a new tutorial for Vim use.