File editors in linux

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.

Читайте также:  Open suse enterprise linux

Источник

How to Edit File in Linux

If you’re new to the Linux operating system and want to learn some basic points, you’re in the right place. This tutorial is mentioning one of the general beginnings points you should know about.

How to edit a file while using the Linux system is the question that arrives in every beginner’s mind. When executing programs on a system, sometimes we need to modify them in a timely manner. You can perform such tasks through two of the most used editors’ i-e, nano editor, and vim editor.

How To Edit File in Linux Through Nano Editor

Nano editor is a straightforward built-in editor in Linux distributions used to edit files. We don’t need to learn much before using this, it’s simple. All the basic operations with the symbols are mentioned at the bottom of the editor. You can make use of them with the “Control (ctrl)” key. There is no pre-requisite of it or no command to operate this.

To open nano editor, first check its version using command in terminal:

(If it is not installed in your system; type the following command to get it “$ sudo apt install nano”)

Simple type “nano” with the file name that needs to be edited in the editor. For example, there is a file in my system name “testing.txt”, to open it, type:

To edit it, move the cursor where you want to add required text and start typing.

After modification press “ctrl+o” keys to save it:

File modification has been saved successfully. Now, press “ctrl+x” keys to exit the nano editor.

To verify the modification, open the terminal and run cat command with the file name:

How To Edit File in Linux Through Vim Editor

The vim or vi editor is most likely a built-in Linux editor that comes up with multiple modes. Most of the tools contain one mode to edit documents. But when you start working with vim editor, you will get several things like insert, command, line mode and many more.

Before getting started with vim editor, you should know some basic symbols to switch mode:

  • “i” for insert mode
  • “w” to save file
  • “Esc” for normal mode
  • “:q” to quit editor without saving
  • “:wq” to save updated document and quit editor

To check which version is installed in your system, type in terminal:

Now, type vim with the file name to open it, for example to open “test.txt” file, type:

If there is no “test.txt” file in the system backup, then this command will create a new file of this name.

Edit File: Now to edit the file, press “i”, it will activate insert mode and allow you to add anything in the file.

Save File: Use the following “:wq” operation to save an updated file and quit the vi editor.

Conclusion

Editing a file or document is the most common thing while using any system. Similarly, in the Linux operating system, you might need editing all the time. Linux distributions come with the multiple built-in editing tools i-e, vim, and nano. This article has briefly explained how to edit files using vim editor and nano editor.

Читайте также:  Установка linux без графической оболочки

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

How to open, create, edit, and view a file in Linux

Linux logo

Computer Hope

One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover some text editors available for Linux.

GUI text editors

This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.

If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.

NEdit

The nedit text editor for Linux

NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.

The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:

sudo apt-get install nedit

For more information, see our NEdit information page.

Geany

The Geany text editor for Linux

Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.

The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:

sudo apt-get install geany

Gedit

The Gedit text editor for Linux

Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.

The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:

sudo apt-get install gedit

Terminal-based text editors

If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some popular ones:

pico

The pico text editor, running on Linux

pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for small things.)

The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.

On Debian and Ubuntu Linux systems, you can install pico using the command:

sudo apt-get install alpine-pico

nano

The nano text editor, running on Linux

nano is the GNU version of pico and is essentially the same program under a different name.

On Debian and Ubuntu Linux systems, nano can be installed with the command:

vim

The vim text editor, running on Linux

vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.

Читайте также:  Сколько памяти на сервере linux

On Debian and Ubuntu Linux systems, vim can be installed using the command:

emacs

The emacs editor, running on Linux

emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.

On Debian and Ubuntu Linux systems, emacs can be installed using the command:

sudo apt-get install emacs

Redirecting command output into a text file

When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.

Creating an empty file with the touch command

To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the file’s contents had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.

The above command creates a new, empty file called myfile.txt if that file does not already exist.

Redirecting text into a file

Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.

For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:

The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:

echo "Example text" > myfile.txt

The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.

Be careful when redirecting output to a file using >. It will overwrite the previous file’s contents if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.

Here’s an example using another command:

The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.

Redirecting to the end of a file

The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:

The above command takes the output of ls -l and adds it to directory.txt. If directory.txt does not exist, it is created first. If it already exists, the output of ls -l is added to the end of the file, one line after the existing content.

Источник

Оцените статью
Adblock
detector