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

Download ArticleDownload Article

This How.com.vn teaches you how to create and edit a text file in two popular Linux text editors. Nearly all Linux systems come preinstalled with Nano, a straight-forward, easy-to-use text editor. If you don't like (or don't have) Nano, you can also use Vi (or Vim, depending on the system) to edit text files. Vi and Vim are a bit more challenging to use, as there are a multitude of commands and two different modes.

Method 1
Method 1 of 2:

Using Nano

Download Article
  1. How.com.vn English: Step 1 Press Control+Alt+T to open a new terminal window.
    This keyboard shortcut opens a terminal window in nearly all versions of Linux.
    • You can also double-click the Terminal icon in your list of Applications, or by clicking your Dash menu (if you're using GNOME) and searching for terminal.
    • Nano is a very easy-to-use text editor that comes preinstalled on all Ubuntu-based Linux distributions.[1] If you don't have Nano, you can get it by running sudo apt install nano (Ubuntu and Debian) or sudo yum install nano (CentOS and Fedora).
    • If you've ever used the Pico text editor, you'll find that Nano is essentially identical. And unlike Vi and Vim, you don't have to switch between command and input modes while using it.
  2. How.com.vn English: Step 2 Navigate to the directory in which you want to create your file.
    You'll probably want to place the file somewhere in your home directory, which is where you'll already be upon opening a terminal window. If you want to place the file in an existing subdirectory, you can use the cd command to get there.
    • To view all folders in the current directory (your home directory), type ls and press Enter.
    • To go to a directory inside of your home directory, type cd directoryname and press Enter (replace directoryname' with the directory's name).
    • If you want to create a new directory, run makedir directoryname (replace directoryname with the name you want to give your new directory. Then, use cd directoryname to enter that directory.
    • You CAN create and edit files outside of your home directory, but you'll need root access to do so.
    Advertisement
  3. How.com.vn English: Step 3 Type nano filename and press ↵ Enter.
    Replace filename with the name you want to give your new text file. This creates and opens a new text file with that name.
    • For example, if you want to create a file called "testfile," type nano testfile and press Enter.
    • It may be helpful to add ".txt" to the end of your filename so you know it's a text file.
    • If your current directory has a file by the same name, this command will instead open that file.
  4. How.com.vn English: Step 4 Find the command list at the bottom of the window.
    The commands you can use while typing into your file appear at the bottom of Nano. To see more commands, simply enlarge the window by dragging it from one of its corners.
    • The commands either start with a carat (^) or an M. The carat represents the Control key, while the M represents the Alt key.[2]
      • For example, ^U is the command to paste. To paste something you've copied, you'd press Control + U.
      • M-U is the command to undo the last action. To undo, you'd press Alt + U.
    • To see all Nano commands, press Control + G.
  5. How.com.vn English: Step 5 Type into your file.
    If you need to move the cursor, use the arrow keys.
    • You can use the mouse to highlight text that you want to copy and/or paste. To copy highlighted text, press Alt + 6. Then, use the arrow keys to move to another location in the file and press Control + U to paste.[3]
  6. How.com.vn English: Step 6 Press Control+O to save the file.
    Since you've already given your file a name, you won't be asked to give this file a name. However, if you started a file without giving it a name (by just running nano from the prompt with no file name), you'll be asked to type the name for your new file and press Enter to save.
    • Avoid the temptation to press Control + S to save, as that will just freeze your terminal window!
  7. How.com.vn English: Step 7 Press Control+X to exit Nano.
    This returns you to the command prompt.
    • You can reopen the file you created in Nano by typing nano filename just as you did before.
  8. Advertisement
Method 2
Method 2 of 2:

Using Vi or Vim

Download Article
  1. How.com.vn English: Step 1 Press Control+Alt+T to open a new terminal window.
    This will open a new terminal in any version of Linux.
    • You can also double-click the Terminal icon in your list of Applications, or by clicking your Dash menu (if you're using GNOME) and searching for terminal.
    • Vi is one of the oldest and most standardized Unix-based text editors. Vim stands for "Vi iMproved," which means it's like Vi but with more features. On most modern versions of Linux, running vi at the prompt will actually launch Vim instead. The basic commands are the same for both editors.
    • Vi has more of a learning curve than Nano, but once you get the hang of it, it's pretty easy to use.
  2. How.com.vn English: Step 2 Go to the directory in which you want to create your file.
    You'll probably want to place the file somewhere in your home directory, which is where you'll already be upon opening a terminal window. If you want to place the file in an existing subdirectory, you can use the cd command to get there.
    • To view all folders in the current directory (your home directory), type ls and press Enter.
    • To go to a directory inside of your home directory, type cd directoryname and press Enter (replace directoryname' with the directory's name).
    • If you want to create a new directory, run makedir directoryname (replace directoryname with the name you want to give your new directory. Then, use cd directoryname to enter that directory.
    • You CAN create and edit files outside of your home directory, but you'll need root access to do so.
  3. How.com.vn English: Step 3 Type vi filename and press ↵ Enter.
    Alternatively, you can type vim filename to make sure the file opens in Vim instead of Vi. The "vi" part of this command selects the Vim text editor as the program to use. Replace filename with the name you wish to assign to your new file.
    • For a file named "sample.text", for example, you'd type vi sample.txt.
    • If your current directory has a file by the same name, this command will instead open that file.
  4. How.com.vn English: Step 4 Press the i key.
    When you open Vi or Vim, it opens in a special mode called Command mode. Pressing the I key will place you into Insert mode, which is where you'll do your typing.
    • You should see -- INSERT -- pop up at the bottom of the window when you press the I key.
  5. How.com.vn English: Step 5 Type your text.
    While you're in Insert mode, you can simply type as you typically would for any other text document. To go to the next line, just press Enter.
  6. How.com.vn English: Step 6 Press the Esc key.
    This takes you back to Command mode. Command mode is where you'll do things like save, copy, paste, and quit. You'll know you're in command mode when you no longer see "INSERT" at the bottom of the window.
    • You can use the arrow keys to move around the document while you're in Command mode in both Vi and Vim. Vim also lets you use the arrow keys to move in Insert mode.
    • Return to Insert mode at any time by pressing the i key.
  7. How.com.vn English: Step 7 Type :w and press ↵ Enter.
    All Vi/Vim commands begin with a colon, and the :w command saves the file (think of "w" as "write").
    • If you created a file without a name (or want to save the current edits to a new file), type :w filename instead, replacing filename with the name you want to give this file.
    • To get help and learn more about Vi/Vim commands, type :help in Command mode and press Enter.
  8. How.com.vn English: Step 8 Type :q and press ↵ Enter to exit.
    This closes your file and brings you back to the command prompt.
    • To reopen the file, just type vi filename or vim filename.
    • You can also save and quit at the same time by typing :wq in Command mode.
  9. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do I write Java code and compile it on an Ubuntu server?
    How.com.vn English: Pingu
    Pingu
    Top Answerer
    First, get access to the server. You can either use SSH or physically access it. Follow the steps in the article to write a file with the code, but save it as .java instead of .txt. After that, type "javac yourFileName.java" and then "java yourFileName", with "yourFileName" being the actual name of your file. (If a Java compiler isn't installed, this will not work. You can install a Java compiler from Ubuntu's APT repository, but you need administrator rights for that, so you won't necessarily be able to do it yourself.)
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • Make sure to save your file before exiting, as you won't always be warned about unsaved changes.
      • You can run man vi or man nano at the command prompt to view the manuals for either of these text editors.
      • One of the most useful features of Vim over Vi is its syntax highlighting, which is great for coders. It also comes with integrated spell-checking, and the ability to move around with the arrow keys in Insert mode.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 1,706,902 times.
      How helpful is this?
      Co-authors: 17
      Updated: May 13, 2023
      Views: 1,706,902
      Categories: Linux | Operating Systems
      Article SummaryX

      1.Type "nano filename," but replace "filename" with the name you want to give your file.
      2.Press Enter.
      3.Type your content.
      4.Press Control + O to save.
      5.Press Control + X to quit.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 1,706,902 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement