How to create and edit a text file using the terminal in Linux

Author: Marcus Baldwin
Date Of Creation: 19 June 2021
Update Date: 1 July 2024
Anonim
Creating and editing of text files in linux
Video: Creating and editing of text files in linux

Content

This article will show you how to create a text file on Linux using the terminal. You can then use one of the built-in text editors to edit this file.

Steps

Part 1 of 4: How to open a terminal

  1. 1 Open a terminal. Click on "Menu" and select "Terminal", whose icon looks like a black square with white "> _" symbols. This icon is in the left menu bar.
    • Alternatively, you can click the search bar at the top of the menu and then type terminal.
  2. 2 In terminal enter ls and press ↵ Enter. Terminal will open in your home directory, but with the command ls you can open a list of folders in the current directory. To create a text file in one of these folders, you need to navigate to it from the current directory.
  3. 3 Select the folder where the text file will be created. Enter the folder name after the command lsto go to that folder.
  4. 4 Enter cd directory. Replace "directory" with the name of the folder you want. With this command, you will navigate from the current directory to the specified folder.
    • For example, enter cd Desktopto go to your desktop directory.
    • To create a text file in one of the subfolders of the selected folder, type “/” (without quotes) after the folder name, and then enter the name of the subfolder. For example, if the folder "Documents" contains the subfolder "Misc" you want, enter cd Documents / Misc.
  5. 5 Click on ↵ Enter. The command will be executed, that is, you will change from the current directory to the selected folder (or subfolder).
  6. 6 Choose a text editor. You can quickly create a simple text file; you can also use the text editor Vim or Emacs to create and edit a more complex text file. Now that you've navigated to the correct folder, create a text file.

Part 2 of 4: How to Create a Text File Quickly

  1. 1 In a terminal, enter cat> filename.txt. Replace "filename" with the name of the text file to be created.
    • For example, to create a kitty.txt file, enter cat> kitty.txt.
  2. 2 Click on ↵ Enter. A new text file will be created in the current directory. In the terminal, the cursor will move to an empty line.
  3. 3 Enter the text of the document. Do it the same way as in any text document. To save the entered text and move to a new line, press ↵ Enter.
    • If a folder with a text file is open, just double-click on it (when it is created) to perform this step.
  4. 4 Click on Ctrl+Z. The entered text will be saved and you will be returned to the terminal window.
  5. 5 In a terminal, enter ls -l filename.txt. Replace "filename" with the name of the generated text file. With this command, you will find the generated file to ensure that it is indeed created in the correct folder.
    • For example, to find the text file textfile.txt, enter the command ls -l textfile.txt.
    • This command uses the lowercase "L" rather than the uppercase "i".
  6. 6 Click on ↵ Enter. The next line of the terminal will display the time and date of the file creation, as well as its name. This means that the file was created in the specified folder.

Part 3 of 4: How to Use the Vim Text Editor

  1. 1 In a terminal, enter vi filename.txt. The "vi" command tells the system to create and edit the file using the Vim text editor. Replace "filename" with the name of the text file to be created.
    • For example, to create and edit the tamins.txt file, enter vi tamins.txt.
    • If there is already a file with the same name in the current directory, that file will open.
  2. 2 Click on ↵ Enter. The text file will be created and opened in the Vim text editor. Each line of the terminal will display a "~" symbol, and the name of the created text file will appear at the bottom of the terminal.
  3. 3 Press the key i on keyboard. The text editor will switch to Insert mode; in this mode, you can enter text into the document.
    • When you press the specified key, “INSERT” will be displayed at the bottom of the window.
  4. 4 Enter the text of the document. Do it the same way as in any text document. To save the entered text and move to a new line, press ↵ Enter.
  5. 5 Press the key Esc. Typically, it is located in the upper left corner of the keyboard. The text editor switches to Command mode.
    • A cursor appears at the bottom of the window.
  6. 6 In terminal enter: w and press ↵ Enter. The entered text will be saved.
  7. 7 In terminal enter: q and press ↵ Enter. The Vim text editor will close and you will be returned to the terminal window. The text file can now be found in the current directory.
    • To verify that the file has been created, in a terminal, enter ls and find the name of the created file in the list of files.
    • You can also enter the command : wqto save the entered text and close the text editor immediately.
  8. 8 Open the file from terminal. To do this, enter vi filename.txt... When the document opens, you will see the text you entered earlier.

Part 4 of 4: How to Use the Emacs Text Editor

  1. 1 In a terminal, enter emacs filename.txt. Replace "filename" with the name of the text file to be created.
  2. 2 Click on ↵ Enter. If there is no file with the same name in the current directory, a new (empty) document opens in the Emacs text editor.
    • For example, to create and open newfile.txt, enter emacs newfile.txt.
    • If there is already a file with the same name in the current directory, that file will open.
  3. 3 Become familiar with the commands of the Emacs text editor. This editor includes many useful commands with which you can navigate the document, search for information in it, edit text, and so on. To execute a command, Emacs uses keyboard shortcuts, including the Control and Meta keys.
    • Control commands denoted as follows: C-letter>. To execute such a command, hold down the Ctrl and press the indicated letter key (for example, Ctrl and A).
    • Meta Teams denoted as follows: M-letter>. To execute such a command, hold down the Alt or Esc (if there is no key on the keyboard Alt) and press the indicated letter key.
    • To execute the command C-a b (or M-a b), hold Ctrl (or Alt, or Esc), and then press the first letter key (in our example a); now release both keys and press the second letter key (in our example b).
  4. 4 Enter the text of the document. Do it the same way as in any text document. To save the entered text and move to a new line, press ↵ Enter.
  5. 5 Click on Ctrl+Xand then press S. The entered text will be saved.
  6. 6 Click on Ctrl+Xand then press Ctrl+C. The Emacs text editor is closed, and you are returned to the terminal window. The created text file will be saved in the current directory.
  7. 7 Open the created text file again. To do this, in the terminal enter emacs filename.txt... If you are in the directory where the file you want is stored, it will open in the Emacs text editor. The document can now be edited.

Tips

  • Vim is supported by almost any Linux distribution, and Emacs is a more powerful editor that may be easier for inexperienced users to work with.
  • To open the help window in Emacs, press Ctrl+Hand then release both keys and press T... This window displays additional commands and functions for the Emacs editor.

Warnings

  • If you forget to save your changes and try to close the document, there may be no warning (especially in the Vim editor). Therefore, always save the document before closing it.