Download Article
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.
Steps
-
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] X Research source 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.
-
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 -
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.
-
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]
X
Research source
- 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 .
- The commands either start with a carat (^) or an M. The carat represents the Control
key, while the M represents the Alt
key. [2]
X
Research source
-
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] X Research source
-
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!
-
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.
Advertisement
-
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.
-
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.
-
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.
-
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.
-
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 .
-
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.
-
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 .
-
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.
Advertisement
Community Q&A
Search
-
QuestionHow do I write Java code and compile it on an Ubuntu server?PinguTop AnswererFirst, 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.Thanks
- You can run man vi or man nano at the command prompt to view the manuals for either of these text editors.Thanks
- 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.Thanks
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
References
About This Article
Article Summary
X
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,519 times.
Advertisement