Linux commands editing files

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.

Источник

Everything you Must Know About How to Edit File in Linux? [Linux Edit File]

Are you new to working with Linux OS? This post will teach you on how to edit file in Linux or how to edit file in Linux without VI.

Everything you Must Know About How to Edit File in Linux? [Linux Edit File]

List of content you will read in this article:

Linux system allows the users to make the required changes to the files. For this, many text editors are available for carrying out easy changes. Not only this, you can run various commands from the command-line interface for editing the files. In this article, we will discuss how to edit file in Linux on various text editors and other command-line commands for making the desired file changes.

How to edit files in Linux via Vi editor?

You can open the Vi editor in the command-line terminal if you want to change a file. Once the terminal is open, you can type of below command to open the file in the Vi editor.

If you provide the file name that exists already, then the Vi editor will open this file in editing mode for you. But if you provide a file name that does not exist, it will create a new file for you.

screenshot of the VI editor that is used to edit file in Linux, with the word “insert” on the bottom left.

If you are working with the Vi editor, you must understand the VI Editor commands for seamless and efficient working. It will speed up the editing process by entering direct commands. Below are some Vi commands-

i – this command will allow you to Insert wherever your cursor is (goes into insert mode)

a – it will allow you to write just after the cursor (goes into insert mode)

A – it will allow you to write at the end of the line (goes into insert mode)

Читайте также:  Virtualbox linux виртуальный адаптер

ESC – this will allow you to exit the insert mode

u – it will allow you to undo the last change

U – it will allow you to undo all changes to the entire line

o – it will allow you to open a new line (goes into insert mode)

dd – it will allow you to delete the specific line

3dd – it will allow you to delete 3 consecutive lines.

D – it will allow you to delete the contents of the line just after the cursor

C – it will allow you to delete the contents of a line just after the cursor and allow you to insert new text. You can press the ESC key if you want to end insertion mode.

dw – it will allow you to delete the specific word.

4dw – it will allow you to delete 4 consecutive words

cw – it will allow you to change the specific word

x – it will allow you to delete the specific character at the cursor

r – it will allow you to replace the specific character

R – it will allow you to overwrite the characters just from the cursor onward

s – it will allow you to substitute one character just under the cursor and then continue to insert

S – it will allow you to substitute the entire line and begin to insert just at the beginning of the line

~ – it will allow you to change the case of a specific character

To run the above commands, you need to be in “command mode.” Vi editor is case-sensitive, and each command has its meaning, so you must ensure that you enter the right command. You can also use a, A, or o to get into the insert mode.

If you want to move within the file using the Vi editor, you can use the following options.

k – it allows you to move the cursor up

j – it will allow you to move the cursor down

h – it will allow you to move the cursor left

l – it will allow you to move the cursor right

To run these commands, you must be in “command mode.” Also, you can use the arrow keys on the keyword to make these movements within the file.

After making the required changes to the file using the Vi editor, it’s time to save the changes and close the specific file. You can use the following shortcuts to do so.

  1. Shift+zz – it will allow you to save the file and quit
  2. :w – it will allow you to save the file but keep the file open
  3. :q – it will allow you to quit the file without saving it
  4. :wq – it will allow you to save the file and quit vim

Again, to make such changes, you need to be in “command mode.”

Linux Edit file with Nano editor

Apart from the Vi editor, you can use the Nano editor to make the desired changes to the file. You can do it by running a simple command-

It will open the nano UI that looks like the image below.

screenshot of Nano’s UI that is used to open and edit file in Linux.

You can also provide the filename and the nano command to launch the editor directly with a file.

nano [filename]
nano test.txt

In the above image, you can see that the screen is divided into four parts.

  1. First, the line at the top of the screen will show the version of the editor, opened file, and the editing status of the file.
  2. Second is the main editing area, where you can see the contents of the opened file.
  3. Third, the highlighted line at the bottom of the edit area where you can see the important messages.
  4. Last, the two lines at the bottom display the helpful commands for beginners performing basic nano commands.
Читайте также:  Платформа smart tv linux

Below, we have mentioned some commonly used shortcuts you should be aware of.

  1. You can use the navigation keys on the keyboard to navigate within the file text.
  2. To delete the text, you can use the backspace key.
  3. To save the changes made to the file, you can use the ctrl+o.

Once you try to save the changes, the editor will ask for confirmation, as shown below.

screenshot of Nano, that is used to edit file in Linux without Vi asking for confirmation.

You can now select any option to save the changes in various OS formats. You can press Alt+d to enable the DOS format and Alt+m for the Mac format.

screenshot of Nano, before saving the applied changes.

To save the changes, you can press enter.

screenshot of Nano in Linux

You can use the ctrl+k and ctrl+u for cutting and pasting the lines of the text. You can also use the keyboard shortcuts for cutting and pasting individual words, but you need to select the specific word for which you need to perform the action. To select the text, you can press the Alt+A and then the arrow key to select the complete word.

Apart from this, you can even perform search operations. You can use the ctrl+w for performing a simple search while the ctrl+\ for searching and replacing the text.

screenshot of Nano that is used to open and edit file in Linux, with the ctrl+w being performed.

How to edit a File with Emacs Command Line Editor?

Another useful text editor of Linux is Emacs which will help edit the files using the command-line interface. You can run the following command to get started with the file edit.

sudo apt-get install emacs

Also, you can run the following command along with the file name that will open the file directly into the Emacs editor.

In the above command, the “-nw” flag is used for launching the Emacs in the bash itself rather than opening in a separate window by default.

This command will open the test file in the Emacs UI, as shown below.

screenshot of Emacs command line editor that is used to edit file in Linux command

Emacs UI is divided into different parts.

  1. First is the top menu on the screen, which is similar to any graphical application.
  2. Second is the main edit area, where you can see the file’s content. You can see the highlighted bar below the edit area that will show file details like name, editing mode, and status.
  3. Last, there is an area where you can see the inputted command and output.

Once you make the desired changes to the file, it is time to save those changes using ctrl+x followed by ctrl+s. You can see the changes in the last area, as shown below.

screenshot of Emacs command line editor, with the command to edit file in Linux running on it.

But if you want to discard the changes made to the file and quit the editor, you can press the ctrl+x followed by ctrl+c. The editor will confirm the input command as shown below.

screenshot of the Emacs command line editor that is used to edit file in Linux without Vi.

To confirm, you need to press yes, and the editor will quit without saving any changes to the file. If you want to delete the text, you can use the delete or backspace key from the keyboard. If you want to use the shortcuts, then you can use the ctrl+k for deleting a complete line, alt+d for deleting a specific word, and alt+k for deleting a sentence.

If you want to undo the changes, you can use ctrl+x followed by ctrl+u. If you want to re-do the undo changes, you can press ctrl+g followed by ctrl+_. For forward-searching, you can use the ctrl+s; for reverse search, you can press ctrl+r.

screenshot of the Emacs command line editor while Forward Searching is being performed on it.

For performing a replacement operation, you need to use the alt_shift+%. Then the editor will ask you for the word you want to replace. Enter the word then the editor will ask for the replacement confirmation. In the below screenshot, the user is replacing the word “This.»

To launch a replace operation, use the Alt+Shift+% keyboard shortcut. You’ll be asked for the word you want to replace. Enter it. Then the editor will ask you for a replacement. For example, the following screenshot shows emacs asking the user about the replacement for the word ‘This.’

Читайте также:  Linux make one kernel module

screenshot of the Emacs command line editor being used to replace words in a file in Linux.

You must provide the replacement text for “This” and press enter. For carrying out each replacement operation, the editor will ask for your permission first:

screenshot of the Emacs command line editor asking for permission before going through with the replacement operation.

After you are asked for permission, press ‘ y’, and the specific word will be replaced.

screenshot of the Emacs command line editor before the word is replaced in the file that you edit on Linux.

Conclusion

To use these Linux editors, you need to have a better grip on their specific shortcuts. If you use shortcuts, then you can seamlessly edit the files. For this, you need to practice a lot and remember various shortcuts. You can choose any of the editors of your choice and start practicing for a smooth performance. This is all about how one can edit/modify files in Linux operating system. Also, we have listed different Linux edit file commands that will help you to accomplish your goal.

People also read:

Источник

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.

Источник

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