Skip to main content

Linux basics: A beginner's guide to text editing with vim

There is a multitude of reasons to learn vim, from its simple navigation tools to its quick and dirty character correction. Here's what you need to know.
Image
lego trail guide

Image by Andrew Martin from https://pixabay.com

Vim is a text editor I can identify with: Vim and I are both 90's babies, and we prefer to work smarter, not harder. There are a multitude of reasons to learn vim, from its simple navigation tools to its quick and dirty character correction. Here is a crash course in an outstanding tool that every sysadmin should learn. 

[ Download now: Vim cheat sheet ]

What is Vim? 

The name Vim is an acronym for Vi Improved. This editor is an enhanced version of the Vi text editor that we all know and love, and is normally seen in a CLI form; however, it does have a GUI version available for standard desktop use. Vim allows you to merge files using vimdiff—which is not the same as diff, the comparison utility—as well as an autocomplete feature and a comparison mode that is similar to the diff utility. This editor's real change and utility is supporting plugins and multiple scripting languages such as Perl and Python. Also included is support for compression functions such as tar and zip, as well as network transfer protocols such as SSH, FTP, and HTTP.

Vim's common modes

The Vim editor has three modes that determine how the editor functions: Normal (or Command), Insert, and GUI.

[ Looking for a different text editor? Download the Emacs cheat sheet. ]

Normal mode

Normal mode allows you to give commands to the editor. Functions such as the following can be found here:

  • :w to write/save.
  • :q to quit.
  • :w <filename.txt> to name a new file.
  • :q! to quit without saving the changes to the file

Press the Esc key to start the Normal mode and enter :(desired command) [Enter] to perform your intended task. For example, if I was working in a new file and wanted to name it 'file.txt', I would use the following: 

:w file.txt [ENTER]

Insert mode

If you have made it this far, you probably know what the Insert mode does. However, for those who don't, if you press the I key (lowercase i) once you will see the "INSERT" prompt at the bottom of the screen, indicating that you can now edit or add text.

To exit this mode and return to Normal mode, press the Esc key once. 

GUI mode

GUI mode is only available in some environments. It offers a graphical, point-and-click interface to be utilized with a mouse and keyboard. 

Vim tips and tricks

Now, imagine Vim being that shady guy on the corner in a trenchcoat selling faux Rolex watches from inside his lapel, only instead of knockoff watches, VIM has tricks and shortcuts on offer. Seriously, there are too many to list in this article, but I will list some of my favorites here: 

  • dd removes all text from the current line (deleting the full line) and saves the removed text to the clipboard. 
  • p pastes (puts) anything from the Vim clipboard to the current cursor, and pairs nicely with the full line delete shortcut above. 
  • r replaces a character and is great for a quick correction.

Using r is a bit more complicated than the others:

  1. Press Esc to enter Normal mode.
  2. Move the cursor onto the character you wish to correct.
  3. Type r followed by the character that you wish to use. 

For example, "Goodbee" can be edited to "Goodbye" by highlighting the first "e" and then entering ry

[ Get more out of your text editor: 5 Vim features for power users. ]

Where to start

I encourage you to give these tricks a try. There is no better way to learn than creating a text file and then making random edits.

Actually, that may not be true, which brings me to the absolute best way that I have found to learn Vim. I learn best with a bit of direction, so it's no surprise that a tutor is just what I needed. Vim has a built-in tutor that can be accessed by entering vimtutor in the terminal. This tutor will give you lessons on specific topics, starting from beginner to advanced. I highly recommend this tool for first-timers.

If you're someone who enjoys having fun with technology, check out the free Vim-based adventure game Vim-Adventures. This game is a great way to learn a new skill while also enjoying some old-school gaming eye candy. 

This article is only a small tease of what Vim actually has on offer. Once you are more comfortable with the things we have talked about here, I encourage you to look into "operators and motions" as well as dotfiles for customizing your Vim experience. If you are looking for further info on text editors, check out our other text editor articles.

Topics:   Text editors   Linux  
Author’s photo

Tyler Carrigan

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.