Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
3,500+ tech & creative courses authored by experts - unlimited & online Get it now →
March 23, 2012

Linux Text Editors: Vi and Nano

By linux-text-editors_featured-image

When working with text editors, you pretty much choose between Vi or Nano. Vi is old school with a blank layout and no visible controls, which can make it difficult to work with for beginners. However, it is very powerful and once you learn the keystrokes it can be used very quickly to edit config files and used for scripts, etc. Nano has a pseudo-graphical layout that makes it a little easier to jump right into. Both are viable options. Vi is a standard whereas Nano has to be available depending on the Linux OS you use. We will look at both and go over the important commands for both.

Let’s look at the difference.
To start Vi, you just have to type “vi” and then the name of the file, or type any name and the file will be created anew.

Example: “vi newfile”

Linux Text Editors 1

Vi will automatically open in Command mode. On the bottom you will see some info such as the name of the document & info on whether it is a new file or an existing one. Let’s look at an existing file.

Linux Text Editors 2

You will see additional info for existing files and more importantly whether you have to write premissions.

Quitting & Saving

There are three modes to Vi: Command mode, Text mode & Ex Mode. If you tried to start typing in command mode you wouldn’t be able to. You need to hit the “I” in order to get into text mode. You can then get back to command mode by the ESC key. Typng “:” will put you into Ex mode.

Linux Text Editors 3

In order to quit Vi, you have to hit the Esc key and then enter in “:” along with “q” for quit.

Linux Text Editors 4

If you get stuck in a file that is Read-Only and you can’t quit, you can enter “:q!” with the exlamation point to force quit. If you want to save changes, hit the Esc key along with “:w”.

Linux Text Editors 5

Linux Text Editors 6

In short, here are the keys for saving and quiting after hitting Esc (Ex mode):

  • :w – Saves
  • :wq or :x – Save & Quit
  • :q – Quit
  • :q! – Force quit, no save

Navigation in Vi

Navigating in Vi is a little more tricky, and this is what gives new users headaches. You just want to keep your fingers on your keyboard as if you are constantly typing (i.e. h, j, k, l). These keys are the substitute for the arrow keys.

  • j – down
  • k – up
  • l – right
  • h – left
  • gg – Takes you to the beginning of the file
  • G(shift + g) – Will take you to the end of the file
  • xxG – Brings you to a specific line in the file(xx is the line number)
  • ( – Move up to the beginning of the sentence
  • ) – Move to the end of the sentence

Insterting Text

  • i – Insert before cursor position
  • a – append after cursor position
  • r – Replace letter
  • o – Open line below cursor position automatically goes into insert mode
  • O – Open line above cursor in insert mode

Deleting Text

  • x – Deletes the character under cursor position
  • X – Deletes the character before cursor
  • dw – Deletes the word after cursor
  • db – Deletes word before the cursor
  • u – Will undo changes

Copy(yank) & Paste

  • yw – Yanks the word after the cursor
  • yb – Yanks the word before the cursor
  • yy – Yanks the entire line
  • p – Will paste
  • P – Will paste before the cursor position

Search

  • / – Is used to search(:/searchword)

Linux Text Editors 7

Linux Text Editors 8

Setting up Vi with numbers

It might benefit some to view the documents with numbers. What you want to do is go into Exec mode(ESC) and then type in “: set number”. Let’s look at the group file:

Linux Text Editors 9

Linux Text Editors 10

Nano

Let’s take a look at workinig with Nano. You start out the same way as you do with Vi by just typing “nano newfile”.

Linux Text Editors 11

The look is a lot different.

About the Author

is the product of Western Illinois University’s prestigious academics programs. With a BS in Instructional Technology and a BA in Communications Roman has taken his education and applied it into the vast world of Information Technology. Roman currently works for Jack Morton Worldwide and is a Technical Administrator for the IBM training facility in the Chicago Loop. Roman has been working at Jack Morton for two years and is consistently engulfing himself in different technologies that IBM has to offer. While pursuing higher education through certifications such as A+ and Net + he also enjoys life outside of the computer world with ultimate frisbee, synchronized swimming, and fantasy football.


Discussion