How to Create and Edit Text File in Linux by Using Terminal

This wikiHow teaches you how to use the Terminal app in Linux to create a text file. After doing so, you can use one of Linux's built-in text editors to make changes to the file. Open Terminal. To do so, click Menu, then find the Terminal...
Part 1 of 4:

Opening Terminal

  1. How to Create and Edit Text File in Linux by Using Terminal Picture 1How to Create and Edit Text File in Linux by Using Terminal Picture 1
    Open Terminal. To do so, click Menu, then find the Terminal app--which resembles a black box with a white ">_" in it--and click on it. You'll typically find Terminal in a bar on the left side of the Menu window.
    1. You can also click the search bar at the top of the Menu window and then type in terminal to search for it.
    2. Most Linux Distros open the Terminal as well, when pressing Ctrl+Alt+T.
  2. How to Create and Edit Text File in Linux by Using Terminal Picture 2How to Create and Edit Text File in Linux by Using Terminal Picture 2
    Type ls into Terminal, then press Enter. Terminal opens to your home directory, but the ls command displays every folder in your current directory. To create a text file in one of these directories, you'll need to change the current directory.
  3. How to Create and Edit Text File in Linux by Using Terminal Picture 3How to Create and Edit Text File in Linux by Using Terminal Picture 3
    Find a directory in which you wish to create a text file. Any directory name listed beneath the ls command (e.g., "Desktop") is a place to which you can navigate.
    1. You CAN create and edit files outside of this directory, but be aware that this may cause fatal problems. If you're sure about doing so, use the command ls / to see and navigate through the root directory, and prefix your commands with sudo when opening the text editor. More on that later.
  4. How to Create and Edit Text File in Linux by Using Terminal Picture 4How to Create and Edit Text File in Linux by Using Terminal Picture 4
    Type cd directory. You'll replace "directory" with your preferred directory's name. This command changes Terminal's focus from the current directory to the one you name.
    1. For example, you would type cd Desktop to shift Terminal's command location to the Desktop directory.
    2. If you want to create a text file in a specific folder within your selected directory, you'll place a "/" after the directory and then type in the folder's name. For example: for a folder named "Misc" in the Documents directory, you'd type cd Documents/Misc.
  5. How to Create and Edit Text File in Linux by Using Terminal Picture 5How to Create and Edit Text File in Linux by Using Terminal Picture 5
    Press Enter. Doing so will run your command, thereby shifting Terminal's target directory from your home directory to the one you specified.
  6. How to Create and Edit Text File in Linux by Using Terminal Picture 6How to Create and Edit Text File in Linux by Using Terminal Picture 6
    Decide on a text editing program. You can create a simple text file quickly, or you can use either Vim or Emacs to create and edit an advanced file. Now that you're in the directory in which you wish to place your text file, it's time to proceed with actually creating it.
Part 2 of 4:

Creating a Quick Text File

  1. How to Create and Edit Text File in Linux by Using Terminal Picture 7How to Create and Edit Text File in Linux by Using Terminal Picture 7
    Type cat > filename.txt into Terminal. You'll replace "filename" with your preferred text file name (e.g., "sample").
    1. For example: when creating a file named "kitty", you'd type cat > kitty.txt.
  2. How to Create and Edit Text File in Linux by Using Terminal Picture 8How to Create and Edit Text File in Linux by Using Terminal Picture 8
    Press Enter. Doing so will create a new text file with the specified name in your current directory. Your cursor will appear on a blank line in Terminal.
  3. How to Create and Edit Text File in Linux by Using Terminal Picture 9How to Create and Edit Text File in Linux by Using Terminal Picture 9
    Enter your document's text. You can simply type as you typically would for any other text document. To save the current line of text and jump to the next line, you'll press Enter.
    1. If you have the text file's directory open, you can also simply double-click the text file when it appears to perform this step.
  4. How to Create and Edit Text File in Linux by Using Terminal Picture 10How to Create and Edit Text File in Linux by Using Terminal Picture 10
    Press Ctrl+Z. This key command will save your work and return you to the Terminal command line, allowing you to continue entering commands.[1]
  5. How to Create and Edit Text File in Linux by Using Terminal Picture 11How to Create and Edit Text File in Linux by Using Terminal Picture 11
    Type ls -l filename.txt into Terminal. You'll replace "filename" with the text file's name. This command will locate your file, thus ensuring that it was safely created in your directory.[2]
    1. For example: to open a file named "textfile", you would type ls -l textfile.txt.
    2. The letters in this code are lowercase "L", not uppercase "i".
  6. How to Create and Edit Text File in Linux by Using Terminal Picture 12How to Create and Edit Text File in Linux by Using Terminal Picture 12
    Press Enter. Doing so should display the time, date, and name of your file on the following line, thus indicating that it was created and saved in your selected directory.
Part 3 of 4:

Using Vim

  1. How to Create and Edit Text File in Linux by Using Terminal Picture 13How to Create and Edit Text File in Linux by Using Terminal Picture 13
    Type vi filename.txt into Terminal. The "vi" part of this command selects the Vim text editor as the program to use. You'll replace "filename" with the name you wish to assign to your new file.
    1. For a file named "tamins", for example, you'd type vi tamins.txt.
    2. If your current directory has a file by the same name, this command will instead open that file.
  2. How to Create and Edit Text File in Linux by Using Terminal Picture 14How to Create and Edit Text File in Linux by Using Terminal Picture 14
    Press Enter. This will create your new file and open it in the Vim editor. You should see a blank Terminal window with a tilde (~) on each line, as well as your text file's name at the bottom of the window.
  3. How to Create and Edit Text File in Linux by Using Terminal Picture 15How to Create and Edit Text File in Linux by Using Terminal Picture 15
    Press your computer's i key. Doing so will place your document in "Insert" mode, wherein you can enter text as needed.
    1. You should see -- INSERT -- pop up at the bottom of the window when you press the I key.
  4. How to Create and Edit Text File in Linux by Using Terminal Picture 16How to Create and Edit Text File in Linux by Using Terminal Picture 16
    Enter your document's text. You can simply type as you typically would for any other text document. To save the current line of text and jump to the next line, you'll press Enter.
  5. How to Create and Edit Text File in Linux by Using Terminal Picture 17How to Create and Edit Text File in Linux by Using Terminal Picture 17
    Press the Esc key. It's typically in the upper-left corner of your computer's keyboard. This key will place Vim in "Command" mode.
    1. You should see a cursor appear at the bottom of the window.
  6. How to Create and Edit Text File in Linux by Using Terminal Picture 18How to Create and Edit Text File in Linux by Using Terminal Picture 18
    Type :w into Terminal and press Enter. This command will save your document as it currently is.
  7. How to Create and Edit Text File in Linux by Using Terminal Picture 19How to Create and Edit Text File in Linux by Using Terminal Picture 19
    Type :q into Terminal and press Enter. Doing so will quit out of Vim and return you to Terminal's main interface. Your text file is now in your specified directory.
    1. You can check for the text file by typing ls into Terminal and pressing Enter and then looking for the file's name.
    2. You can also type :wq to save and quit in one command.
  8. How to Create and Edit Text File in Linux by Using Terminal Picture 20How to Create and Edit Text File in Linux by Using Terminal Picture 20
    Reopen the file from the Terminal window. Just as you did to create the file, you'll type vi filename.txt to open the file. This time when you open the file, you should see any saved changes that you made.
Part 4 of 4:

Using Emacs

  1. How to Create and Edit Text File in Linux by Using Terminal Picture 21How to Create and Edit Text File in Linux by Using Terminal Picture 21
    Type emacs filename.txt into Terminal. You'll replace "filename" with your preferred name for the text file.
  2. How to Create and Edit Text File in Linux by Using Terminal Picture 22How to Create and Edit Text File in Linux by Using Terminal Picture 22
    Press Enter. As long as your text file's name isn't a duplicate of an existing text file in your current directory, this command will open a new text file in the Emacs editor.
    1. For example: a file named "newfile" would require you to type emacs newfile.txt.
    2. If you entered an existing text file name, this command will instead open the file in question.
  3. How to Create and Edit Text File in Linux by Using Terminal Picture 23How to Create and Edit Text File in Linux by Using Terminal Picture 23
    Get to know 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 and Meta commands.
    1. Control commands are written like this: C-. To execute a Control command, you'll hold down the Ctrl key and press the specified letter key at the same time (e.g., Ctrl and A).
    2. Meta (or Escape) commands are written like so: M- or ESC . The "M" refers to a computer's Alt key or Esc key, since not all computers have the Alt key.
    3. A command written like C-a b (or M-a b) will require you to hold Ctrl (or Alt or Esc) while pressing the first key (e.g., a, then release both keys and immediately press the second key (e.g., b).
  4. How to Create and Edit Text File in Linux by Using Terminal Picture 24How to Create and Edit Text File in Linux by Using Terminal Picture 24
    Enter your document's text. You can simply type as you typically would for any other text document. To save the current line of text and jump to the next line, you'll press Enter.
  5. How to Create and Edit Text File in Linux by Using Terminal Picture 25How to Create and Edit Text File in Linux by Using Terminal Picture 25
    Press Ctrl+X, then press S. Doing so will save your file.
  6. How to Create and Edit Text File in Linux by Using Terminal Picture 26How to Create and Edit Text File in Linux by Using Terminal Picture 26
    Press Ctrl+X, then press Ctrl+C. This will exit the Emacs text editor and return you to your directory in Terminal. Your text file should now be saved under your selected name in this directory.
  7. How to Create and Edit Text File in Linux by Using Terminal Picture 27How to Create and Edit Text File in Linux by Using Terminal Picture 27
    Re-open your text file. To do so, simply type emacs filename.txt into Terminal. As long as you're in the same directory as is the file, doing this will open your text file in Emacs, at which point you may resume editing it if you please.
3.3 ★ | 4 Vote