In a room there stood two camps of programmers, one more dedicated to their cause than the other. There were two indeed and they held fast to their beliefs. Each beleived their editor was… the best editor.
“Emacs is the best editor,” one side said, calmly but sternly.
“Vi is the best editor.” the other group said back, with even more furor.
“Emacs” the first side said, their voices bubbling.
“Vi.” the other group shouted back.
“Emacs!” “V!” “Emacs!”
And so they went on like this, shouting at eachother, each believing their editor was the best editor. And as neither could change the other’s mind, they went to war— a holy war— a war of ideologly and of utmost importance. And this war raged on.
When unix documentation says “the editor of your choice,” we mean a text editor you use in your Terminal.
Much of your work as a programmer or devops will be to edit config files.
Although editing files in an IDE like VSCode or even a simple notes app like OneNote is possible, learning how to use Unix effectively necessitates getting comfortable with a terminal-based editor.
As you can see, the preference for an editor depends on your personal feelings about how the different terminal programs introduced here feel to you. Specifically, pay attention to how your fingers and hands feel while attempting to learn the different editors today. Repeat the keystrokes several times to feel natural. Maybe work with one of these one afternoon, the next the following afternoon, and the third on a third day still. Notice how it feels to work with the editor and then pick the most natural editor.
Today, we’ll have a brief less on the 3 most popular: Nano, Emacs, and Vi.
Nano is the quickest and easiest way to get started, but it is not as full-featured as the other two.
Vi is a smaller and faster program, but emacs has more capacity for customization. Emacs can show your console in different fonts, languages, and many plug-ins.
As far as the intro to this lesson, that is a parable written by yours truly, the author, and is a reference to the Editor War, a piece of folklore about how entrenched developers become about our attachment to the editor we choose.
Whatever your choice, practice using your tool well and understand its feature set. Take the time to learn it well. Practice keystroking the important keys over and over. If you’re confused about which symbols refer to which modifier on your keyboard, see the Modifier Keys Lesson.
Nano is designed for simplicity and ease of use. Its interface is straightforward and more similar to typical graphical text editors. Nano’s keybindings are more intuitive and user-friendly, making it a good choice for beginners. Nano is lightweight and uses minimal system resources. While Nano lacks the extensive features of Emacs and Vim, it covers the basic text editing needs. Nano is great for casual users, beginners, or those who need to perform simple text editing tasks without the complexity of Emacs or Vim.
NANO TIP: After making edits, always Write Out (^-O) before trying to quit. In the demo above, the keystrokes are ^-O (Write Out), then press Return key when Nano gives you the prompt, then press ^-X to exit Nano.
Nano Conveniently gives you a guide for moving around the program printed at the bottom fo the screen
|^-O||Write the file to disk|
Emacs is known for its extensive features and powerful extensibility. It’s more than just a text editor; it’s an integrated development environment. Users can customize every Emacs aspect using its built-in Lisp scripting language. Emacs has a steep learning curve due to its complex keybindings. However, users can perform complex tasks efficiently using keyboard shortcuts once mastered. Emacs has various modes for different tasks, such as text editing, coding, email, web browsing, and more. Each mode optimizes the editor for a specific task. Emacs tends to use more memory compared to simpler editors like Nano. Emacs is suitable for programmers, writers, and power users who require advanced editing capabilities and are willing to invest time learning and customizing the editor.
In Emacs documentation, the Ctrl key is referred to as the
C key, but it is the same key as the
^ key. (Remember, refer to the Modifier Keys Lesson if you are confused about which symbols refer to which keys.)
Emacs Tip: Always save the file before existing. To save and exit, you will want to keystroke:
Emacs has a quirk where, for many actions, you press keystrokes (both modified) to do one thing.
In Emacs screens, the C key means Control (in other parts of this doc shown as ^)
A keystroke requiring two keys one after other, Emacs designates that as the modifier key + a dash + the first key + another dash + the second key.
|C-x||The setup key you press before other keys|
|C-x-s||Ctrl-X (wait until you see ||Save file to disk|
|C-x-c||Ctrl-X (wait until you see ||Quit Emacs|
Vim is the improved version of the Vi editor. It follows a modal editing model, where users switch between different modes (insert, normal, visual, etc.) for different tasks.
This can be efficient once users are familiar with the modes. Vim is known for its efficiency once users become proficient with its modal approach and keyboard shortcuts. Vim allows customization through its configuration file, allowing users to tailor the editor to their preferences.
Vim is lightweight and fast, making it a great choice for remote access and quick edits. Vim has a learning curve due to its unique modal approach and numerous keyboard shortcuts. Vim is suitable for users who want a powerful and efficient text editor, especially programmers who work in a terminal environment.
Vi is the most esoteric of the three editors and has the most confusing first steps. To get started, you must understand the difference between display and insert: In display mode, which is the default when you open a new Text file, what you type is not added to the document. Instead, you must move into Insert mode using the I key. Notice that
-- INSERT -- that shows up in the lower left of the window when you insert:
Only when in Insert mode can you edit the document. While not in Insert mode, you’re in Normal mode, which is like a “moving around” mode: Every keystroke helps you search and navigate. Vi is heavy on providing lots of keystrokes for searching and navigating.
Many, if not most, command keystrokes in Vi are double keystrokes, like other editors. Vi uses the
: to move from Normal mode to the command mode, where it will wait for the next modifier key you enter. So most involve the colon (:) plus a sequence of other keys.
Remember, unlike modifier keys, sequence keystrokes are one after the other (not pressed simultaneously). (Although some keystrokes for Vim/Vi are modifier keys.)
Vi also uses regular characters (while in display mode), Control-key modifiers, and words to help you get around.
w is for writing to disk, so for that, you’d type this:
q is to quit vi, so you can type:
In VI, you can type the
: and then more than one command together, so to do both at once:
To save and quit. (Like other text editors, always remember to save before exiting).
While in display mode, Vi offers many options to move around using your keyboard. Move one space to the left using
h, or one space to the right using
Want to move five spaces to the left? Type 5h or for five spaces to the right type
5l. Fancy, right?
Several advanced features are well-received by text-editor lovers who dislike using IDEs or GUIs. Indeed, many esteemed professional programmers use Vi as a full IDE— meaning, it can aid them in all the ways an IDE can (language completion, syntax highlighting, go to definition, and find callers— four essential tools every programmer should have when programming) and more. To do these things with Vi, you’ll need to customize it heavily, which takes an investment of time. But many Vi users will swear that that payoff is worth it.
In summary, the choice between Emacs, Vim, and Nano depends on your needs and preferences. Emacs offers unparalleled extensibility and a wide range of features but has a steep learning curve. Vim excels in efficiency and is popular among programmers, while Nano is simple, user-friendly, and suitable for quick edits and beginners.
Beginners are encouraged to start with Nano and then experiment with Vi and Emacs to see what is most comfortable.
Setting the default EDITOR for your Shell
Sometimes, you are on the shell prompt, and your shell prompt needs you to edit a document, save it, and “give it back” to the shell for processing. This can happen, for example, when you are making a commit for a git merge conflict. The shell will look to see if you have a preferred
EDITOR set. An
EDITOR is any program you use to edit text files.
Before moving forward, set up the default editor for your shell (
~/.zprofile (using either Nano, Emacs, or Vi as above
For example, to the Bash profile open with nano use:
Remember, if your OS has ZShell instead, edit the
~/.zprofile file instead.
Add this line at the top of the file:
Or, for Emacs: