Change text file linux

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

    Источник

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