Have you ever wondered which text editor is the best one to use in Linux? One of the best things about the world of Linux is the plethora of ways to get jobs done. That includes programming and editing configuration files in the command line. Depending on who you ask, Linux aficionados might tell you to use Emacs, Vi/Vim, or Nano. It is a fiercely debated topic.
Emacs and Vi/Vim are complex and powerful text editors while Nano is a lightweight, simplified editor. While it’s fun to have light-hearted debates about which command-line text editor is the best text editor to use, at the end of the day they are truly nothing more than different tools. So, let us take a look at Vi vs Nano and discuss which one is better.
For this post, we are only going to look at Vi instead of both Emacs and Vi. That’s because they both share the same philosophy of being very customizable and powerful text editors. Likewise, Vi and Vim are essentially the same application, so for simplicity, we are going to be referring to Vi.
A Brief Background Of Vi and Nano
Both Vi and Nano are wildly different applications with different philosophies. Before you can fully understand why you should use one text editor over the other, you should understand why each application was developed and where its roots lay.Become a security expert
The History of Vi
Vi has a long history. It was originally developed as an application called Ex in 1976 for the Unix operating system. Ex was originally developed as a simpler solution to the Ed command-line editor. It wasn’t until 1978 with the release of version 2.0 that Ex was renamed to Vi. At that time, many people had considered Vi to be a resource-intensive application for computer hardware, but Vi has persisted to become one of the most popular command-line text editors in Linux.
Though people commonly refer to Vi as a singular application, it is in fact many different applications. Vi has been forked numerous times. Some versions still contain some of the original source code dating back to 1976 while others are complete re-writes that include compatible layers to the original code. Likewise, Vi comes in both proprietary, licensed versions as well as open-source versions. Different Linux distributions will contain different versions of Vi though they all largely look and operate the same way.
The History of Nano
Nano was originally developed in 1999 (long after Vi) under the name TIP (Tip Isn’t Pico). Its goal was to emulate the Pico text editor (part of the Pine email client) but with additional features. TIP was renamed to Nano in 2000 to avoid naming conflicts with another existing Unix utility with the same name. In February of 2001, Nano became part of the GNU project and is now commonly known as GNU Nano. Nano was developed strictly as a more feature-rich version of Pico.
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”
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.
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.
In order to quit Vi, you have to hit the Esc key and then enter in “:” along with “q” for quit.
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”.
In short, here are the keys for saving and quiting after hitting Esc (Ex mode):
- :w – Saves
- :wq or 😡 – 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
- 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
- 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
- / – Is used to search(:/searchword)
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:
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”.
The look is a lot different.