Linux command to edit file

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

Image titled Create and Edit Text File in Linux by Using Terminal Step 1

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

Image titled Create and Edit Text File in Linux by Using Terminal Step 2

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

Image titled Create and Edit Text File in Linux by Using Terminal Step 3

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

Image titled Create and Edit Text File in Linux by Using Terminal Step 4

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 5

    • 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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 6

    • Avoid the temptation to press Control + S to save, as that will just freeze your terminal window!

    Image titled Create and Edit Text File in Linux by Using Terminal Step 7

    • You can reopen the file you created in Nano by typing nano filename just as you did before.

    Using Vi or Vim

    Image titled Create and Edit Text File in Linux by Using Terminal Step 8

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 9

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 10

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 11

    • You should see — INSERT — pop up at the bottom of the window when you press the I key.

    Image titled Create and Edit Text File in Linux by Using Terminal Step 12

    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.

    Image titled Create and Edit Text File in Linux by Using Terminal Step 13

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 14

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

    Image titled Create and Edit Text File in Linux by Using Terminal Step 15

    • 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 wikiHow

    Источник

    How to Edit a File in Linux? [SOLVED]

    In Linux operating systems, everything is a file. There are daily routine tasks within the system such as turning new features on or off or editing files. An editor is needed for these tasks, regardless of GUI or CLI.

    In this article, we will explain how to edit files in Linux operating systems.

    Linux users have many alternatives for editing files. Let’s talk about a few applications for both GUI and CLI. At the end you will find the list of alternative apps for you.

    Edit Files in GUI using Gedit

    The first application we will describe is Gedit . Gedit is a text editor for the GNOME desktop. If you are using your Linux system with Gnome Desktop, your default text editor may already be Gedit. If you are using a different desktop, you may need to install it.

    To install on Debian-based systems(Ubuntu, Mint, Pardus, etc), run the following command in terminal:

    For installation on Redhat-based OS(Fedora, CentOS, AlmaLinux, Rocky Linux, etc):

    $ sudo dnf install gedit.x86_64 -y

    There are many plugins for Gedit. You can install the plugin you need from the package repository.

    $ sudo dnf search gedit-plugin- Last metadata expiration check: 0:01:40 ago on Sun 12 Feb 2023 10:04:43 +03. ========================= Name Matched: gedit-plugin- ========================== gedit-plugin-bookmarks.x86_64 : gedit bookmarks plugin gedit-plugin-bracketcompletion.x86_64 : gedit bracketcompletion plugin gedit-plugin-charmap.x86_64 : gedit charmap plugin gedit-plugin-codecomment.x86_64 : gedit codecomment plugin gedit-plugin-colorpicker.x86_64 : gedit colorpicker plugin gedit-plugin-colorschemer.x86_64 : gedit colorschemer plugin gedit-plugin-commander.x86_64 : gedit commander plugin gedit-plugin-drawspaces.x86_64 : gedit drawspaces plugin gedit-plugin-editorconfig.x86_64 : EditorConfig plugin for Gedit gedit-plugin-findinfiles.x86_64 : gedit findinfiles plugin gedit-plugin-git.x86_64 : gedit git plugin gedit-plugin-joinlines.x86_64 : gedit joinlines plugin gedit-plugin-multiedit.x86_64 : gedit multiedit plugin gedit-plugin-sessionsaver.x86_64 : gedit sessionsaver plugin gedit-plugin-smartspaces.x86_64 : gedit smartspaces plugin gedit-plugin-synctex.x86_64 : gedit synctex plugin gedit-plugin-terminal.x86_64 : gedit terminal plugin gedit-plugin-textsize.x86_64 : gedit textsize plugin gedit-plugin-translate.x86_64 : gedit translate plugin gedit-plugin-wordcompletion.x86_64 : gedit wordcompletion plugin

    Edit Files in Linux

    After installing you can invoke Gedit in the Applications menu. Or you can open the file by clicking on a file (if it is not the default editor, right click and open with «Open With Other Application» > Gedit).

    After this stage, you can start editing your files with pleasure.

    Another application you can use to edit text in Linux is Mousepad. Mousepad is a simple text editor for Xfce desktop environment.

    To install on Debian-based systems(Ubuntu, Mint, Pardus, etc), run the following command in terminal:

    foc@ubuntu22desktop:~$ sudo apt install mousepad -y

    For installation on Redhat-based OS(Fedora, CentOS, AlmaLinux, Rocky Linux, etc)

    foc@fedora:~$ sudo dnf install mousepad.x86_64 -y

    How to Edit a File in Linux? [SOLVED]

    If mousepad is already your default text editor, double-clicking the mouse opens the file with the mousepad. If Mousepad is not the default editor, right-click and select «Open with Other Application» → Mousepad.

    Mousepad is a very stable and useful text editor.

    Edit Files in CLI

    Method-1: Using VIM

    Every Linux distribution has a text editor. Built for the end user, Linux OS comes with many text editors installed, while server systems have fewer. But vim/vi is available in almost all Linux distributions.

    If for some reason it is not installed, to install it on Debian based systems:

    foc@ubuntu22desktop:~$ sudo apt install vim -y

    To install on Redhat based systems:

    foc@fedora:~$ sudo dnf install vim -y

    How to Edit a File in Linux? [SOLVED]

    Type the filename you want to edit after the vim command in the terminal. If the file does not exist before, it will create a new file and display this information on the screen with the word «New».

    You can edit the file by pressing the Insert key on the keyboard.

    How to Edit a File in Linux? [SOLVED]

    Then press ESC to cancel editing. If you want to save :wq , to exit without saving :q! write the expressions.

    How to Edit a File in Linux? [SOLVED]

    Let’s show you a few vim shortcuts:

    dd - Delete entire current line q/ - search history in command buffer i - Enter insert mode to the left of the cursor a - Enter insert mode to the right of the cursor
    • CTRL+u — Move up by half a page
    • CTRL+d — Move down by half a page
    • CTRL+b — Move up by a page
    • CTRL+f — Move down by a page

    Vi/vim is more than just a text editor. Some users use vim as an application development environment. So it has so many features. Check for more shortcut information.

    Method-2: Using nano

    Nano, on the other hand, is a simpler and plainer text editor. For installation(On Debian based OS):

    foc@ubuntu22desktop:~$ sudo apt install nano -y
    foc@fedora:~$ sudo dns install nano -y

    How to Edit a File in Linux? [SOLVED]

    The file to be edited is typed after the nano command in the terminal. If it doesn’t exist, it is created, if there is, you can start editing.

    Here are some nano shortcuts:

    Ctrl + X : Exit Ctrl + W: Search Ctrl + K : Cut line Ctrl + U: Paste line

    Alternatives

    Here are the application alternatives for both GUI and CLI:

    Alternatives editor for GUI

    • Xed
    • Kate/Kwrite
    • Sublime Text Editor
    • Geany
    • Leaf Pad
    • Bluefish
    • Atom

    Alternatives editor for CLI

    What is NEXT

    Summary

    We have shared text editors for both GUI and CLI. Habits are important when choosing a text editor. You can start using one of the text editors that we have shared and explained.

    References

    Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

    If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

    Buy GoLinuxCloud a Coffee

    For any other feedbacks or questions you can either use the comments section or contact me form.

    Thank You for your support!!

    Источник

    Читайте также:  Linux user oracle password
Оцените статью
Adblock
detector