- How to Create and Edit Text File in Linux by Using Terminal
- Using Nano
- Using Vi or Vim
- Community Q&A
- Terminal Basics #9: Editing Files in Linux Terminal
- Editing files in Linux terminal
- Using Nano editor
- How to Edit Files from a Linux Shell
- nano
- Create or Edit a File in nano
- Saving a File in nano
- vi
- Create or Edit a File in vi
- Saving a File in vi
How to Create and Edit Text File in Linux by Using Terminal
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.
The wikiHow Tech Team also followed the article’s instructions and verified that they work.
This article has been viewed 1,676,025 times.
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.
Using Nano
- 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.
- 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.
- 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.
- 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.
- 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
- Avoid the temptation to press Control + S to save, as that will just freeze your terminal window!
- You can reopen the file you created in Nano by typing nano filename just as you did before.
Using Vi or Vim
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Community Q&A
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.)
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHowTerminal Basics #9: Editing Files in Linux Terminal
Learn about editing text files in the Linux terminal using the beginner friendly Nano editor in the second last chapter of this series.
You have learned a bunch of file operations so far in this Terminal Basics series. You learned to create new files, delete existing ones, and copy and move them. It is time to take it to the next level. Let’s see how to edit files in the Linux terminal. If you are writing bash shell scripts, you can use the GUI text editors like Gedit and run them in the terminal. But at times, you’ll find yourself in a situation where you have to edit existing files in the terminal itself. For example, modifying config files located in the /etc directory. As a desktop Linux user, you could still use GUI editors for editing config files even as root. I’ll show it to you later. However, knowing how to edit files in the command line is better.
Editing files in Linux terminal
You may use the cat command if you just have to add a few lines at the bottom of an existing file. But in order to properly edit a file, you’ll need a proper text editor. There is simply no shortage of terminal-based text editors in Linux. Vi, Vim, Nano, Emacs are just a few of the most popular ones out there. But here is the thing. All of them have a learning curve involved. You don’t have the comfort of the GUI. You don’t have menus to interact with the editor with your mouse. Instead, you have to use (and remember) keyboard shortcuts. I find Nano to be a good starting point for new users. It is the default text editor in Ubuntu and many other Linux distributions. Of course, there is a learning curve, but it is not as steep as that of Vim or Emacs. It keeps on displaying the most relevant keyboard shortcuts at the bottom. This helps you navigate even if you don’t remember the exact shortcut. For this reason, I’ll be covering the absolute basics of the Nano editor here. You’ll learn all the essentials you need to know to start using Nano for editing files in the Linux terminal.
Using Nano editor
Nano can be used to edit text files, script files, program files etc. Please remember that it is not a word processor and cannot be used to edit docs or PDF files. For simple text editing of conf files, scripts, or text files, Nano is a great choice.
I’ll be using a text file named agatha_complete.txt. It consists of the names of all Agatha Christie’s books under her name. You can download it from this link if you plan to follow the steps on your system.
How to Edit Files from a Linux Shell
Editing text-based files in Linux can be confusing if you are unfamiliar with using the command-line as there is no graphical user interface to help guide you. But fear not, we will cover two of the most popular console-based text editors, nano and vi, in this article.
You’ll need to be logged into your server via SSH before beginning. If you’re not sure how to do this, you may refer to the following guide, which covers Connecting to Your Server via SSH.
nano
This text editor is a growing favorite among Linux users due to its overall ease of use and default on most modern Linux distributions. nano‘s most commonly used shortcut keys are shown at the bottom of the page with the ^ symbol representing the CTRL key followed by a lowercase letter.
Create or Edit a File in nano
- On your server’s console, type the nano command followed by the name of the text file, then press ENTER
- You’ll see an interface similar to the following screenshot, and you can begin editing the file now.
Saving a File in nano
- Press the CTRL key and the O key
- You’ll be prompted for the name of the file to Save As
- When you’re satisfied with the filename, press the ENTER key
- Then press the CTRL key and the X key to exit
Quick commands
Save: CTRL-O
This will save the contents to the file you opened. Just press ENTER if this is still the file name you wish to edit or CTRL-C to cancel or CTRL-X to exit.Close: CTRL-X
This will close the nano program. If you’ve made any unsaved changes, it will ask you if you’d like to continue without saving.vi
vi is an older text editor and the most widely installed. It is a bit less intuitive than nano. However, once you learn a few common commands, you won’t find them too difficult to use.
Create or Edit a File in vi
Editing a file in vi is a fairly straightforward process.
- On your server’s console, type the vi command followed by the name of the text file, then press ENTER
- You should be greeted with an interface similar to the following.
- Before you can begin editing the text, you’ll need to press the i key to switch to INSERT mode, displayed at the bottom of the screen, as shown below.
- You can now edit the text in the file.
Saving a File in vi
Once you’re done editing a file, you’ll need to save the changes you have made.
- You will need to switch out of INSERT mode by pressing the ESC key before you can close or save the file; it should now no longer show — INSERT —
- Now issue the write and quit commands.
- Press the colon (: ) key on your keyboard (you may need to hold the shift key)
- Type wq and press ENTER on your keyboard so that the command looks like :wq
- Alternatively, you can press SHIFT and hit ZZ to quickly save the file and exit.
- You will need to enter wq! and press ENTER on your keyboard to forcefully overwrite the file for read-only files.
Quick commands
Edit: i
This will place the editor into INSERT mode so you can make changes to the text.Save: :wq
This will save the contents to the file you opened. (write and quit)Close: :q or :q!
This will close the vi program. If you would like to quit without saving any changes you’ve made, then use :q! instead of :qSave and Close: Hold the shift key and press ZZ
You should now be able to successfully create, edit, and save a file in two of the primarily used text-based editors in Linux.
Written by Michael Brower / March 28, 2017