- Terminal Basics #9: Editing Files in Linux Terminal
- Editing files in Linux terminal
- Using Nano editor
- How to edit a text file in my terminal
- 6 Answers 6
- How to Create and Edit Text File in Linux by Using Terminal
- Using Nano
- Using Vi or Vim
- Community Q&A
- Editing files in terminal linux
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
Terminal 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 a text file in my terminal
I’m using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?
6 Answers 6
it, will open up a text editor to edit your file.
Here, you can edit your file in the terminal window.
Open the file again using vi. and then press » i » or press insert key ,
and write the following command
Open the file again using vi. and then press the insert button to begin editing it.
could also run ‘vimtutor’ to learn how to use vi/vim. This of course assumes you have vim (vi’s big brother) if you don’t run ‘sudo apt-get install vim’
If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then «i» to enter INSERT mode or «a» to enter APPEND mode (they are the same, just start before or after current character).
If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned «vi helloWorld.txt». Once you are in the editor, you’ll need to check the vi reference to know how to perform the editions you want (you may want to google «vi reference» or «vi cheat sheet»).
Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.
For quick reference, here you have a text-based cheat sheet.
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,297 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 wikiHowEditing files in terminal linux
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 weekLike/Subscribe us for latest updates or newsletter