PDF download Download Article PDF download Download Article

This wikiHow 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

PDF download Download Article
  1. 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. 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. 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. 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. 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. 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. 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

PDF download Download Article
  1. 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. 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. 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. 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. 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. 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. 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. 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?
    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
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Install Google Chrome Using Terminal on Linux
Linux How to Mount Drive Mount Drives on Linux: Command Line & GUI
Can Linux Run ExeCan Linux Run .exe Files? How to Run Windows Software on Linux
Use Wine on Linux Install and Use Wine on Linux
Tar a Directory Tar a Directory and Subdirectories in Linux
Take a Screenshot in LinuxTake a Screenshot in Linux
Become Root in LinuxBecome Root in Linux
Execute INSTALL.sh Files in Linux Using Terminal Run an INSTALL.sh Script on Linux in 4 Easy Steps
Install DebianEasy Debian Linux Installation Tutorial
Add or Change the Default Gateway in LinuxAdd or Change the Default Gateway in Linux
Run Files in LinuxRun Files in Linux
Execute .RUN Files in LinuxExecute .RUN Files in Linux
Boot Linux from a USB on Windows 10 Run Linus from a USB: Making a Bootable Stick and Installing
Unrar Files in Linux Open RAR Files in Linux using Unrar
Advertisement

About This Article

Nicole Levine, MFA
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. 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,716,509 times.
How helpful is this?
Co-authors: 17
Updated: May 13, 2023
Views: 1,716,509
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,716,509 times.

Is this article up to date?

Advertisement