How to edit files in linux terminal

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

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 a few of the many 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

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:

For more information, see our NEdit information page.

Geany

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:

Gedit

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:

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 of the most popular:

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 very 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:

Читайте также:  Как настроить панель инструментов windows

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, 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.

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

emacs

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:

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 contents of the file 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.

For instance, the command:

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:

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 contents of the file 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:

Читайте также:  Как сделать live cd astra linux

Источник

How to Edit Config Files in Ubuntu

Configuration files contain the information required to control the operation of a program. Linux users, especially Linux administrators, must know how to edit configuration files to maintain an Ubuntu server or desktop. Configuration files on Ubuntu are stored in subdirectories of the /etc folder as a text file and can be edited using text editors. Some of the examples of config files are host.conf, hosts. resolv.conf, networks, syslog.conf etc. in Linux-based operating systems.

To make any configuration changes, you have to open and edit the file using a text editor. In this article, I will describe multiple ways to edit config files in Ubuntu. The steps described in this guide have been tested on Ubuntu 20.04, but they will work on other Ubuntu versions too.

  • Using Gedit Editor
  • Using Nano Editor
  • Using Vim Editor
  • Using Gvim Editor
  • Using Emacs Editor

Now let’s take a look at these text editors. By using these editors, you can easily edit files in terminal .

Using Gedit Editor

Gedit is the default text editor of the Gnome desktop environment. It supports tabs so you can open different files in multiple tabs. It is the simpler and easier text editor as compared to other available text editors.

To edit a config file in text editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Then type the below command as sudo:

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

Example: editing of the /etc/resolv.conf file

It is like the notepad editor in Windows OS. As soon as the editor window opens, you can start typing or editing without entering in any mode. Once you have done with the editing, press Ctrl+S to save the file.

Using Nano Editor

Nano is the simplest and the powerful built-in editor in the many Linux distributions. You do not have to learn the Nano editor before using it and also there is no need to remember the commands. They are displayed at the bottom and can be triggered with the Ctrl key. For instance to save a file, press Ctrl+O.

To edit any config file, simply open the Terminal window by pressing the Ctrl+Alt+T key combinations. Navigate to the directory where the file is placed. Then type nano followed by the filename that you want to edit.

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password. Advertisement

Now you can edit and make changes in the config file using Nano editor.

Once you have done editing, press Ctrl+O to save and Ctrl+X to exit the Editor.

Using Vim Editor

Vim or Vi editor also comes pre-installed with most Linux distributions. It has no visible prompts or control that can be a little harder for beginners. However, once you learned it, it is the more quick and powerful tool for editing. Using Vim, you can edit and view multiple files in one session.

Читайте также:  Создать usb загрузочную флешку windows 10 uefi

To open a file in Vim editor, open the Terminal window by pressing the Ctrl+Alt+T key combinations. Then type vi as sudo followed by filename along with the file path like:

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

Then edit the file using Vim editor. Note that when the editor opens, you enter command mode. Here are the few steps to edit a file in Vim editor:

  • To edit the file, Press I from keyboard to enter into insert mode, here you can do editing just like the normal editor.
  • When done with editing, get out of this mode by pressing Esc. It will return you to normal mode.
  • To save the file, type :w in command mode
  • To quit the editor, type :q in command mode
  • To combine the above actions type :wq to save and quit simultaneously.

Using Gvim Editor

Gvim is a Vim editor with a graphical user interface. To install Gvim, launch Terminal by pressing Ctrl+Alt+T. then type the below command in Terminal to start the installation.

Once done with the installation, you can open and edit any config file in it. Type the below command with sudo in Terminal to open any config file in Gvim:

Press I to enter in insert mode where you can edit the file.

In Gvim editor, you can do various functions using the toolbar just like any normal GUI application like saving the file, undo, redo, finding a keyword, etc.

Using Emacs Editor

Emacs is also a text editor but it is not a built-in editor. You have to manually install it using the Ubuntu Terminal. It has so many features including a built-in web browser, calculator, calendar, Terminal, etc.

To install Emacs editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Type the below command in Terminal as sudo:

Once done with the installation, you can open and edit any config file in it.

To open a config file using emacs editor, type emacs followed by filename along with the file path in the below syntax:

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

Edit the file as normal and save it using the toolbar.

You can also open the config file in bash instead of a separate window.

Type the following command as sudo in Terminal:

Edit the file as a normal text file. Then press Ctrl+X and Ctrl+C to save the file. When it prompts for saving the file, press y to save and quit the editor.

So these were some simple editors you can use to easily and safely edit config files in your Ubuntu operating system.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Источник

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