Create and Edit Text File in Linux by Using Terminal

Text files are perhaps the most common files that you use on your computer every day. Creating and editing a text file on a Windows or Mac computer is easy and simple with a text editor like TextEdit or Notepad. However, you can also manipulate text files in Linux or any other operating system from the Terminal. While this may seem confusing at first, you'll find it simple and efficient in the long run.

Steps

Using the Terminal

  1. Open the terminal. This works differently on each specific operating system, but in general you can find Terminal in your Applications folder. A quick Google search will give you plenty of resources for the specific operating system you use.
  2. Learn the basics of navigation in the Terminal. You will automatically be in your home directory, represented by "~". Type "ls" to list all files and directories in your current directory. Type "cd dir_name" to change to the directory dir_name. For example, if you want to move to your Documents folder, you would type "cd Documents".
    • Follow a path by including the path to a directory as the argument in your "cd" command. Use slashes to create the path. If you wanted to move from your home directory to a folder called Misc inside your Documents folder, you would type "cd Documents/Misc".
    • Type "pwd" to display the full path of your current directory.
  3. Learn quicker navigation tips. Use ".." (two periods) to access the directory just above your current directory. If you wanted to move from Misc back to Documents, you would type "cd ..", since Documents is one directory up from your current directory. Use "~" to access your home directory. If you want to move quickly back to your home directory, rather than walking through the path to get there, simply type "cd ~".
  4. Navigate to the directory where you want to create or edit a text file. If you want to edit an existing document, "cd" into the directory that holds the document. If you want to create a new document, navigate to the directory where you want it to go. For example, if you want to edit or create a text file in your Documents folder, you would type "cd Documents".
  5. Choose a text editor. The two main text editors that the Terminal uses are Program Using GNU Emacs and Learn vi. They both have their advantages. Emacs is a powerful and universal text editor that has commands that do nearly everything.[1] Vim is smaller and simpler and uses less memory. Programmers are often fiercely loyal to their particular text editor, so the debate between the two has fostered something called the "Editor war."[2]
  6. Open the file you want to edit. To use Vim, type "vi filename.txt", where "vi" is the command to open the Vim text editor and "filename.txt" is the name of the file you want to edit. To use Emacs, type "emacs filename.txt". If the file exists, the text editor will open with the file. Otherwise, the text editor will open a blank file with the name you chose.
    • When naming a new file, it is not necessary to include the .txt extension. However, this makes it clear that the file is a text file rather than a directory or other file. When opening an existing file with a .txt or other extension, you must include the extension in the filename in order for your text editor to correctly identify the file.
    • The text editor interprets the file as if it were a plain text file, no matter what format it really is. Thus, when opening .doc or other text file formats, or even other file types altogether like .jpg or .wav, you will see a sequence of random characters rather than anything meaningful. Editing these files can change them in unpredictable ways, so it's best to stick to .txt files when using Vim or Emacs.
  7. Edit your file as you would in any other text editor. Don't forget to save the file before you close it to make sure that your changes persist. Quit the text editor to return to the terminal.
    • A new file is only actually created when you save it. If you open a new file and quit without saving, the file will not actually be created, and so reopening it will open a new blank document.

Using Vim from the Terminal

  1. Open the file you want to edit in Vim. Simply type "vi filename.txt", where "vi" is the command to open Vim and "filename.txt" is the name of the file you want to edit. Vim will open in the Terminal window you're currently using. The contents of the file will appear. If the file is blank or is shorter than your window, you will see lines with colored squiggles ("~"). These signify blank lines that are not part of the file. The very bottom of your window should show the name of the file and any other information about it.
  2. Understand the basics of Vim. There are two modes that Vim uses, Insert mode and Command mode. You'll do your actual text editing in Insert mode, while you'll use Command mode to copy and paste, navigate, save, quit, and execute other commands.
    • The bottom line of your window will show what mode you're in. It will display "-- INSERT --" if you're in insert mode. When you switch to Command mode, it will display a blank line that will be filled in by any command you type.
  3. Learn how to navigate in Vim. Use the up and down arrow keys to move up or down one line. The left and write arrow keys will move the cursor left or right one character. In command mode, press "0" to jump to the beginning of the line and "$" to move to the end of it. Use "b" and "e" to move to the beginning and end of a word. Press "[" twice to jump to the beginning of the file, and "]" twice to jump to the end. Press "{" (Shift + "[") to move to the previous blank line and "}" to move to the next one. Type ":n" to jump to line number n (":5" will jump to line 5).
    • There are lots of tips and tricks to make navigating in Vim faster and more intuitive. Learn more commands and experiment with them to figure out what works best for you.
  4. Enter Insert mode and start editing your text file. Press "i", "I" (capital i), "a", or "A" to enter Insert mode and start typing. "i" will let you insert text just before the cursor. "I" inserts text at the beginning of the current line. "a" will let you insert text just after the cursor, and "A" will let you type at the end of the current line. From there, you can type into your file just as you normally would.
  5. While in Insert mode, press the Escape key to switch to Command mode. From there you can navigate more quickly, copy and paste text, save and quit, or execute any other command.
    • Quickly switch back and forth between Insert and Command mode to speed up navigation and perform other operations.
  6. Save and quit. When you're finished editing your text file, press Escape to enter Command mode. To save your changes, type ":w" (for "write"). To quit Vim and return to the Terminal, type ":q". To save and quit in one command, type ":wq". To quit without saving your changes, type ":q!".
  7. Reopen the file from the terminal window in the same way. Just as you did above, type "vi filename.txt" to open the file. This time when you open the file, you should see any saved changes that you made.

Using Emacs from the Terminal

  1. Open your text file in Emacs. Type "emacs filename.txt" to do so. On some operating systems, Emacs will open in a separate window, leaving you with your Terminal window still accessible. You should see your text file in the main part of the window, with two lines at the bottom that show the status of your operations in Emacs.
  2. Understand Emacs commands. Emacs comes complete with tons of powerful commands that let you navigate around a document, look up related or help information, manipulate text, and understand your code. These commands fall into two types.
    • Control commands are written like this: C-<letter>. Execute a control command by holding down the Control button while you press the given key. For example, execute the control command C-v by holding down Control while striking v.
    • Meta or Escape commands are executed with a Meta or Alt button or, if your keyboard doesn't include such a button, the Escape key. They are written M-<letter> or ESC <letter>. To execute them with the Meta or Alt key, hold down that key while pressing the appropriate letter, as you did with control commands. To execute them with the Escape key, strike ESC once and then press the appropriate letter.
  3. Read the Emacs tutorial. Access the tutorial by entering C-h t (hold down Control while you press h, then let go of both keys and strike t). Read through the tutorial to learn more about Emacs commands and about all the operations available in Emacs. This information is essential if you'll be using Emacs regularly, particularly for programming.
    • Access general help by entering the command C-h C-h and then following the instructions that appear on the screen.
  4. Learn how to navigate in Emacs. Move up, down, left, or right with the arrow keys. You can also use C-b and C-f to move back and forward one character and ESC b and ESC f to move back and forward one word. Use C-p and C-n to move up and down one line. C-a and C-e move the cursor to the beginning and end of a line, ESC a and ESC e to the beginning and end of the sentence, ESC [ and ESC ] to the beginning and end of the paragraph, and ESC < and ESC > to the beginning and end of the file.
  5. Edit your file. Type into your file the same way you would with any other text editor.
  6. Save and quit. Periodically while editing or when finished, save your file with the command C-x s. To save the file under a new name, use C-x C-w. To quit Emacs, type C-x C-c. If your file is unsaved, the system will ask you to save the file before quitting. Respond as prompted, and Emacs will proceed to quit and return you to your Terminal window.

Tips

  • Look up other commands for your text editor. Having more commands at your fingertips makes your text editing more efficient and makes using the Terminal more convenient. With enough practice, you might even end up preferring these editors to normal text editing applications.

Sources and Citations