The Linux User’s Ultimate Guide to Text Editors
Basic text editors seem like they should be among the least interesting of Linux utilities when, in reality, they are some of the most critical.
Aug 10th, 2024 9:00am by
$ sudo apt install vim
$ sudo apt install nanoTo install vim or nano on a Red Hat-based distribution, type:
$ sudo dnf install vim
$ sudo dnf install nanoBoth vim and nano are written in standard documentation using all lowercase characters. Note: It is a poor security practice to log on to a Linux system as the root (administrator) user. Most systems force you to log on as a regular user and then use the sudo (short for “super user do”) command to elevate your privileges. You may be prompted for your password when using sudo. You probably mainly need sudo when editing system configuration files that are normally reserved for access by the root user. Most Linux text editors must provide an alternative system to get around the problem of not having a graphical user interface (GUI). Many Linux deployments avoid the GUI to maintain speed, simplicity and stability. Therefore, these editors don’t include a menu where you can use a mouse to select Save or Exit. There are two common approaches:
- Modes: Users switch the editor between modes. The keyboard either enters text or accepts commands, depending on the current mode.
- Meta keys: Users press one or more meta keys to enter commands. Meta keys perform special functions when combined with another key. For example, meta keys may include Ctrl or Alt.
Why Are Text Editors So Important?
Text editors are standard tools on the system. Since Linux and Linux applications receive their primary settings and options from configuration files, managing these configuration files is clearly essential. If an administrator wants to change how a service like the Apache web server functions, they must edit the Apache configuration file. Here are a few common tasks for text editors:- Edit configuration files that control system actions and services.
- Edit configuration files for applications like web servers or databases.
- Create scripts and programming files to automate tasks.
Common Linux Text Editors
Many text editors are available for Linux, so I’ll just cover two of the most common.- vim: A powerful and flexible editor with a steep learning curve.
- nano: An intuitive and simple editor that may be limited for more complex projects.
The vim Text Editor
The name “vim” stands for “vi improved.” It is a fresh version of an older Unix/Linux editor called vi (pronounced “vee-eye”). It’s been a standard Linux application for decades, and with good reason. It’s highly configurable, very customizable, fast and efficient. It is not, however, the simplest application to learn. Many resources exist for learning vim basics. Linux training courses nearly always cover it, many tutorials address it and plenty of online forums discuss tweaks and modifications to it. The official vim website includes documentation, too. Finally, the program itself has a built-in tutorial to walk you through its essential features. While looking at vim’s documentation, check out its unique licensing mechanism, too. Vim uses modes to change how users interact with the program. Pressing a key on the keyboard has a different effect depending on the mode. The primary modes to be aware of are listed below:- Command mode: Pressing keys issues commands to the program. This is how you save or exit vim.
- Execute mode: Think of this as a subset of Command mode. Issue additional commands to vim by using the : character before the command.
- Insert mode: Pressing keys inserts text in the file. This is how you add, edit or remove text.
Basic Document Management With vim
Vim offers a truly vast number of options. Many new Linux users find themselves overwhelmed by its extensibility and features. However, there are really only four essential vim skills you must learn immediately. Once you master these, you can explore additional vim capabilities. The four essential tasks are:- Create or open a file.
- Edit the file.
- Save your changes.
- Close the file.
cd
vim linux-basics.txt
Figure 1: vim open in Insert mode — note the INSERT banner in the lower left.
Linux is a powerful and flexible open-source operating system.
Figure 2: Add text to the file using Insert mode.
Figure 3: Use the :w command in Command mode to write or save changes.
Figure 4: Use the :q command in Command mode to quit vim.
cat linux-basics.txt
Figure 5: View the contents of a file by using the cat command.
There are many Linux distributions, such as Ubuntu and Fedora.
Figure 6: vim displays this error message when you try to quit the program without saving data first.
Figure 7: Use the :q! command in Command mode to quit vim without saving changes.
Additional vim Tricks
There are several ways to enter Insert mode. These depend on the position of your cursor, so use the arrow keys to place your cursor at the desired location in the file, then use one of these keys to enter Insert mode and begin entering text.- i: Insert text before the cursor.
- I: Insert text before the first non-blank character of the line.
- o: Start a new line below the cursor and insert text.
- O: Start a new line above the cursor and insert text.
- gg or [[: Jump to the top of the file.
- G or ]]: Jump to the bottom of the file.
- 22G: Jump to line 22 of the file.
- :set number: Display line numbers along the left side of the file.
Figure 8: Use the :set number command in Command mode to display line numbers, such as line 1 in this document.
- x: Delete the character where the cursor is.
- dw: Delete the word where the cursor is.
- dd: Delete the line where the cursor is.
- 3dd: Delete three lines beginning where the cursor is.
- 0: Jump the cursor to the beginning of the line.
- $: Jump the cursor to the end of the line.
- yy: Yank the current line.
- 4yy: Yank the current line and the following three lines (for a total of four).
- dd: Cut (or delete) the current line.
- 4dd: Cut the current line and the following three lines.
- p: Put or paste the yanked or cut text at the cursor’s position.
Configure Additional vim Options
Vim installs with a common set of defaults most people find useful. You can customize it to fit your needs by using a vim configuration file named .vimrc. The file does not exist by default, so you must create it. Be sure to do so in your home directory. Note that the first character of the file name is a dot. Begin by moving to your home directory with cd and then creating the.vimrc file:
cd
vim .vimrc
"Display line numbers
set number
"Set tabs equal to four spaces
set tabstop=4
"Highlight search results
set hlsearch
The nano Text Editor
Nano is simpler and less confusing than vim, though it is also less feature-rich and extensible. Still, it’s a great solution for quick configuration file edits or for authoring short documents. And the menu at the bottom of the nano interface means you don’t have to memorize a bunch of odd keystrokes. Nano functions using meta keys — mainly, the Ctrl key. It opens in a normal interface, meaning if you press a key on the keyboard, it will enter text into the file. Hold down the Ctrl key and press other keys to give instructions like “save to nano.” Nano uses the ^ character to represent the Ctrl meta key, so if you see ^X, it means Ctrl+X. Many Linux distributions include nano by default, though you can install it if it’s not already part of your favorite distro. Nano’s homepage includes documentation, FAQs and shortcuts.Basic Document Management With nano
In the discussion of vim above, I showed four basic tasks: Create/open a file, edit the file, save changes and exit. These fundamental tasks apply to nano, too (and really, they apply to any text editor). Create a new file or open an existing one in your home directory by typing these commands:
cd
nano linux-basics.txt
Figure 9: Note the menu options displayed at the bottom of nano.
Two common Linux text editors are vim and nano.
Figure 10: After adding text to the file, press Ctrl+S to save changes, then press Ctrl+X to exit the editor.
Additional nano Tricks
Like vim and other editors, nano offers many basic options. Here are several you will find particularly useful.- Ctrl+A: Jump to the start of the current line.
- Ctrl+E: Jump to the end of the current line.
- Ctrl+K: Delete the current line.
- Alt+U: Undo the most recent change.
- Ctrl+W: Search for a string of text. You will be prompted to enter the search string.
- Alt+R: Search for a string. You will be prompted to enter the text you want to replace it with.
- Alt+6 copies the text.
- Ctrl+K cuts the text.
Configure Additional nano Options
Nano offers useful customizations and additional settings. Many of these are handy for development work and managing system configuration files. As with vim, you can set permanent customizations in a configuration file in your home directory. Use nano to create and open a file named .nanorc. (Note the “dot” at the start, marking this as a hidden file.) Here are a few sample entries. Put these on separate lines. Use the # character to mark comments (explanations) for each entry, as seen below:
#Display line numbers
set linenumbers
#Set the tab size to four spaces
set tabsize 4
#Automatically continue indentions from the previous line (handy for development and configuration file entries)
set autoindent
Graphical Text Editors
You may sit at standard Linux workstations with a graphical user interface (GUI) running on it. If that’s the case, you aren’t likely to want to jump out to the Terminal to write text files. Various GUI-based editors exist. One of the most common is GNU gedit. This menu-driven text editor is similar to macOS TextEdit or Windows Notepad. Use your mouse to select options from the menus at the top of the interface. Many familiar keyboard shortcuts function in gedit, too.
Figure 11: The gedit editor is simple and uses a menu interface on Linux systems with a graphical user interface installed.
- Kate: Robust KDE-based editor good for coding.
- Leafpad: Lightweight editor that emphasizes simplicity.
- Sublime: Developer coding platform that is not open source but still popular in the Linux community.
- VS Code: Microsoft’s cross-platform, extensible coding solution.
Add Linux Editors to macOS and Windows
Multiple versions of vim exist for macOS and Windows, too. Adding vim to your daily-use computer (even if it’s not Linux) is a handy way to practice your editing skills. Nano was included with older macOS versions. You can add it to your current macOS version using a package manager like Homebrew. Various nano versions exist for Windows, too.Wrap up
I don’t find nano to be as powerful or extensible as vim. Or, to phrase that another way, nano is simpler and less confusing than vim. I really don’t believe one is better than the other, but they are both useful for different things. I find nano to be handy for very quick and basic configuration file edits, such as managing root login via SSH in the /etc/ssh/sshd_config file by using either yes or no. I prefer vim for longer configuration files, where I need to search for various settings. I also use vim periodically to write more substantial documents, like this tutorial. Because I’ve used vim for a long time, I’m more comfortable with it, so it is my go-to editor. (I even use it on my Mac!) I recommend you get comfortable with opening and editing files using both vim and nano. That skill will serve you well on nearly any Linux distribution you come across. Practice with both whenever you need to generate or edit some basic text!
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.