A Beginner’s Guide to Text Editing: Navigating the Terminal


The terminal, also known as the command line, is a powerful tool that enables you to interact directly with your computer’s operating system. In Unix-based systems such as macOS or Linux, you can even use the terminal to create and edit text files using built-in text editors like nano, vi, and emacs. This article will provide an introduction to these text editors and show you how to navigate text files using them.

Understanding Terminal Text Editors

A text editor is a program that allows you to create and modify text files. In the terminal, text editors typically have a graphical user interface. However, these interface don’t require a mouse to move the cursor. The cursor is moved around by the keyboard.

Here’s a brief overview of three commonly used terminal text editors:

  1. Nano: A simple, user-friendly text editor that’s great for beginners. It displays all of its commands at the bottom of the screen, which makes it easy to use.
  2. Vi/Vim: A powerful, versatile text editor that’s widely used by programmers. Vim is an improved version of Vi, and both have a steeper learning curve than Nano.
  3. Emacs: Another powerful, highly customizable text editor. Emacs can be more complex to use than Nano, but it offers a wide range of features.

Creating and Editing Text Files in Nano

Nano is a simple, user-friendly text editor for Unix-like operating systems. Unlike Vim or Emacs, Nano’s commands and shortcuts are displayed at the bottom of the window, which makes it more approachable for beginners.

Creating a New File in Nano

To create a new file using Nano, type the following command in your terminal:

nano filename.txt

Replace filename.txt with the name you want for your file. This command will open a new file in Nano.

Opening an Existing File in Nano

To open an existing file, use the same command you’d use to create a new file:

nano existingfile.txt

Replace existingfile.txt with the name of the file you want to open.

Inserting Text in Nano

In Nano, you are immediately able to insert text when you open or create a file. Just start typing!

Saving Changes in Nano

  • Press Ctrl + O to save your changes (write them to the file). Nano will prompt you to confirm or change the file name. Press Enter to confirm.

Closing the File in Nano

  • Press Ctrl + X to close the file. If you’ve made changes and haven’t saved them, Nano will ask if you want to save them. Press Y for yes or N for no.

Searching Text in Nano

  • Press Ctrl + W and then type the text you want to search for and press Enter.

Cutting and Pasting Text in Nano

  • Press Ctrl + K to cut a line of text.
  • Press Ctrl + U to paste the text you cut.

There is no limit to the amount of times you can paste the text, however, once you close the session, the buffer is cleared and you can’t paste the text anymore.

Although Nano lacks the extensive features and power of editors like Vim and Emacs, its simplicity and ease of use make it a great choice for basic text editing and note-taking.

Creating and Editing Text Files in Vi/Vim

Vi (pronounced as “vee-eye”) is a screen-oriented text editor originally created for the Unix operating system. Vim (“vi improved”) is an extended version of the vi editor, with many additional features designed to be helpful for text editing.

Creating a New File in Vi/Vim

To create a new file using Vim, type the following command in your terminal:

vi filename.txtvim filename.txt

Replace filename.txt with the name you want for your file. This command will open a new file in Vim.

Opening an Existing File in Vi/Vim

To open an existing file, use the same command you’d use to create a new file:

vi existingfile.txtvim existingfile.txt

Replace existingfile.txt with the name of the file you want to open.

Inserting Text in Vi/Vim

When you first open a file in Vim, you’re in “Command mode”, where you can type commands to perform actions. To insert text into your file, you need to switch to “Insert mode”.

  • Press i to enter Insert mode. Now you can start typing text into your file.

Saving Changes in Vi/Vim

  • Press Esc to go back to Command mode.
  • Type :w to save your changes (write them to the file).

Closing the File in Vi/Vim

  • In Command mode, type :q to close the file. If you’ve made changes and haven’t saved them, Vim won’t let you close the file.
  • If you want to close the file without saving changes, type :q!.

Saving Changes and Closing the File in Vi/Vim

  • To save your changes and close the file at the same time, type :wq in Command mode.

This is a very basic introduction to Vim. Vim has many more features and commands that can make editing text faster and more efficient. It’s worth investing the time to learn more about what Vim can do.

Cutting and Pasting Text in Vi/Vim

Yes, it is possible to cut and paste text in Vi/Vim. Here are the steps:

Cutting Text

In Vi/Vim, cutting is often referred to as “deleting”. Here’s how to do it:

  1. Move the cursor to the start of the text you want to cut.
  2. Press v to start visually selecting text. If you want to select whole lines, you can press V (uppercase) instead. If you want to select blocks, you can press Ctrl + v.
  3. Move the cursor to the end of the text you want to cut. The text should now be highlighted.
  4. Press d to cut the text. The text is now in the Vim buffer and can be pasted.

Pasting Text

After cutting or yanking text, you can paste it with the following steps:

  1. Move the cursor to the location where you want to paste the text.
  2. Press p to paste the text after the cursor, or P to paste before the cursor.

The cut and paste commands in Vi/Vim work within the same session, so you can cut text from one location and paste it to another, even if they’re in different files. However, once you close the session, the buffer is cleared and you can’t paste the text anymore. If you want to copy text between sessions, you might need to use the system clipboard instead of the Vim buffer, which may require additional configuration.

Creating and Editing Text Files in Emacs

Emacs is a highly extensible and customizable text editor. It’s more than just a simple text editor, and it can be customized to do almost anything through Lisp programming language.

Creating a New File in Emacs

To create a new file using Emacs, type the following command in your terminal:

emacs filename.txt

Replace filename.txt with the name you want for your file. This command will open a new file in Emacs.

Opening an Existing File in Emacs

To open an existing file, use the same command you’d use to create a new file:

emacs existingfile.txt

Replace existingfile.txt with the name of the file you want to open.

Inserting Text in Emacs

Once you’ve created or opened a file, you can start typing to insert text. Emacs is in insert mode by default.

Saving Changes in Emacs

To save your changes, press Ctrl + X, then Ctrl + S.

Closing the File in Emacs

To close the file, press Ctrl + X, then Ctrl + C. If you’ve made changes and haven’t saved them, Emacs will ask if you want to save them.

Searching Text in Emacs

To search for text in Emacs:

  • Press Ctrl + S and then start typing the text you want to search for. Press Enter to finish the search.

Cutting and Pasting Text in Emacs

To cut and paste text in Emacs:

  • Move the cursor to the start of the text you want to cut.
  • Press Ctrl + Space to set a mark.
  • Move the cursor to the end of the text you want to cut. The text should now be highlighted.
  • Press Ctrl + W to cut the text.
  • Move the cursor to the location where you want to paste the text.
  • Press Ctrl + Y to paste the text.

Emacs has many more features and commands. It’s a powerful text editor that can also function as an integrated development environment (IDE) with the right configurations and plugins. However, it has a steep learning curve and may not be the best choice for beginners or those who only need to do simple text editing.

Conclusion

Terminal text editors offer a powerful way to create and edit text files directly within the command line. While they may seem daunting at first, especially for those accustomed to graphical text editors, they offer numerous advantages including speed, versatility, and direct control over your text editing. As you become more comfortable navigating the terminal and using these text editors, you’ll find that they can significantly enhance your productivity and efficiency.

Happy text editing in the terminal!

Daniel

Whilst building web applications, Daniel also sets up web servers from scratch because he has yet to find the perfect hosting solution. His philosophy is “Why settle, when you can build it better yourself?”

Recent Posts