Use command-line text editors in Terminal
To edit a plain text file in Terminal, you can use a command-line text editor.
Text editors are among the oldest programs available on any operating system, and come in a wide variety—from completely automatic text editors, where you essentially write a recipe for what actions should be taken on text and then let the computer do the work, to much more interactive text editors that can edit (and save) text in a wide variety of formats.
For general-purpose work, it’s easiest to deal with one of the text editors included with macOS. If you want to use a graphical text editor, use TextEdit (in Launchpad). Otherwise, use one of the many command-line editors provided. The three most full-featured command-line text editors included with macOS are:
Nano nano
is a simple command-line based editor. It’s a replacement for the Pico editor, so instructions for using the Pico editor can be used with nano
. If you invoke the Pico editor, you actually run nano
. nano
is a good introduction to using a command-line based editor as it includes easy-to-follow on-screen help.
Vim vim
is a vi
-compatible text editor. It has many powerful enhancements for moving around, searching, and editing documents. Basic editing is simple to learn and there’s much additional functionality to explore. You can access most of the functionality by using keystroke combinations that trigger certain behavior. vim
, or the editor it’s modeled after, vi
, is found in most UNIX-based operating systems. If you’ll be doing lots of editing from the command line, it’s a good editor to learn to use, but if you only use a command-line based editor occasionally, you can get by without learning it.
Emacs Like vim
, emacs
is an extremely full-featured editor found on most UNIX-based operating systems. In addition to its editing power, emacs
is extremely customizable, with additional functionality available in modules that let the emacs
interface do much more than just text editing. It’s relatively easy to use for basic editing, and has lots of functionality for you to explore. Like vim
, emacs
uses keystroke combinations to access its many different functional behaviors. These keystroke combinations require memorization, so emacs
is most useful if you use the command line often.
If you’re new to using the command line and don’t anticipate using it much for editing, nano
is probably your best choice. If you expect to spend a lot of time using the command-line environment, it’s probably worth learning either vim
or emacs
. They have very different design philosophies, so spend some time with each of them to determine which works best for you.
You invoke a command-line editor by typing the name of the editor, followed by a space and then the name of the file you want to open. If you want to create a new file, type the editor name, followed by a space and the pathname of the file. Here’s an example of using
nano
to open a new file named “myFile.conf” in your Documents folder:$ nano ~/Documents/myFile.conf
For more information about using nano
, vim
, or emacs
, see their man pages.