Skip to content

Latest commit

 

History

History
406 lines (307 loc) · 9.8 KB

Text-Editor.md

File metadata and controls

406 lines (307 loc) · 9.8 KB

Text Editors in Linux

Text editors are programs used to view and modify the content of files. There are different types of text editors in Linux, and each has its features. Here are two commonly used ones: cat and nano.


1. cat Command

The cat command is used to view the content of a file. It's simple and easy to use.

Basic Usage:

cat filename

This will display the content of the file filename.

If the File is Too Long:

If the file is too long to fit on one screen, you can use less to scroll through the content:

cat filename | less

or simply:

less filename

Limitations of cat:

  • You can only view the file, not edit it.
  • You can add content to the end of a file, but you can't change anything in the middle.

Create or Edit a File:

You can create a new file with the cat command:

cat > file1.txt

After you press Ctrl + C, the file is saved, but the content may not include the line where you pressed Ctrl + C.

Using Here Documents to Add Content:

You can use cat to write a block of content to a file and end it with a marker (like eof):

cat << eof >> file1.txt
This is the content I'm adding to the file.
eof

This will add the content to the file and close it with eof.

Redirect Content from Multiple Files:

You can combine content from multiple files and put them into one new file:

cat hosts messages etc > file1.txt

This combines the contents of hosts, messages, and etc into file1.txt.

Display Line Numbers:

To show the line numbers along with the content:

cat -n filename

Show End-of-Line Characters:

To show a $ at the end of each line (useful for checking empty lines):

cat -e filename

NANO

2. nano Command

nano is a simple text editor, similar to Notepad, that allows you to create and edit files directly in the terminal.

Install nano (if not already installed):

yum install nano

Open a File in nano:

To open a file for editing:

nano filename

If the file does not exist, nano will create a new one.

Basic Operations in nano:

  • Save the file: Press Ctrl + O (then enter the filename to save).
  • Exit nano: Press Ctrl + X.
  • Move the cursor: Use the arrow keys to move around.
  • Find a word: Press Ctrl + W, then type the word you want to find.
  • Replace a word: Press **Ctrl + **, then enter the word to replace.
  • Cut text: Highlight text and press Ctrl + K to cut.
  • Paste text: Press Ctrl + U to paste.
  • Execute a command: Press Ctrl + T to run commands like ifconfig, ip a, etc.
  • Justify text: Press Ctrl + J to align text.
  • Check your position: Press Ctrl + C to see your current line and column number.

Undo and Redo:

  • Undo: Press Alt + U.
  • Redo: Press Alt + E.

Read a File into nano:

You can read an existing file into nano by opening it directly:

nano /path/to/file

Or, if you want to create a new file but load the content of an existing one (e.g., log/passwd):

nano newfile

After opening newfile, you can use Ctrl + R to load the content of log/passwd into newfile.


Summary of Key Commands

cat Commands:

  • View a file:
    cat filename
  • View a file with scrolling:
    cat filename | less
  • Create a new file and add content:
    cat > file1.txt
  • Add content from multiple files:
    cat hosts messages etc > file1.txt
  • Show line numbers:
    cat -n filename
  • Show end-of-line characters:
    cat -e filename

nano Commands:

  • Open a file:
    nano filename
  • Save a file: Press Ctrl + O.
  • Exit nano: Press Ctrl + X.
  • Find a word: Press Ctrl + W.
  • Replace a word: Press **Ctrl + **.
  • Cut text: Press Ctrl + K.
  • Paste text: Press Ctrl + U.
  • Execute a command: Press Ctrl + T.
  • Justify text: Press Ctrl + J.
  • Check position (line/column): Press Ctrl + C.
  • Undo changes: Press Alt + U.
  • Redo changes: Press Alt + E.

VIM


VIM Text Editor

VIM is one of the most widely used text editors in the command line. It's an improved version of the older VI editor. While VI was used in Unix-like systems, VIM is now commonly used.

VIM works in different modes, and each mode serves a specific purpose. You can use VIM to view and edit files in a more flexible way.


VIM Modes

When you open VIM, it starts in Command Mode. You can switch between different modes to perform various actions like editing text, deleting characters, or running commands.

  1. Command Mode (Default mode when you open VIM)
  2. Insert Mode (To insert/edit text)
  3. Visual Mode (To select text)
  4. Replace Mode (To replace characters)
  5. Command Line Mode (To run commands)

How to Use VIM

Open a File in VIM:

To open a file with VIM, use this command:

vim filename.txt

Command Mode:

  • When you open a file, you are in Command Mode by default.
  • In Command Mode, you can move around the file, delete text, or run commands.
  • To exit Insert Mode or any other mode and go back to Command Mode, press ESC.

Switching Between Modes in VIM

  • Insert Mode: Press i or Insert to switch to Insert Mode. This allows you to type text.
  • Visual Mode: Press v to enter Visual Mode. This allows you to select text.
  • Replace Mode: Press r to enter Replace Mode. This allows you to replace characters one by one.
  • Command Line Mode: Press : to enter Command Line Mode. This lets you run various commands like save, quit, search, etc.

Basic VIM Commands

Saving and Exiting:

  • To save the file, type:
    :w
  • To quit VIM, type:
    :q
  • To save and quit VIM at the same time, type:
    :wq
  • To force quit without saving, type:
    :q!

Running Commands from VIM:

You can run system commands while in Command Line Mode:

  • To list files with ls:
    :!ls
  • To show the current date:
    :!date

Search and Replace:

  • Search for a word:

    :s/word

    This will search for the first occurrence of the word.

  • Replace the first occurrence of a word with another word:

    :s/word/replacement
  • To replace all occurrences of a word in the file, use:

    :%s/search/replace/g

    g means replace globally (every occurrence in the file).

  • Case-insensitive replacement:

    :%s/Krish/KRISH/gi

    This will replace "Krish" and "krish" with "KRISH".

  • Replace a word in specific lines (e.g., lines 2, 4, and 8):

    :2,4,8s/krish/KRISH/gi
  • Disable search highlights:

    :nohl

Undo and Redo:

  • To undo the last action, press:
    u
  • To redo the last undone action, press:
    Ctrl + r

Editing Text in Command Mode

  • Delete a character under the cursor:

    • Press x to delete one character.
    • Press 2x to delete two characters.
  • Delete a word:

    • Press dw to delete one word.
    • Press 2dw to delete two words.
  • Delete a line:

    • Press dd to delete one line.
    • Press 3dd to delete three lines.
  • Replace a character:

    • Press r and then type the character you want to replace it with.
    • Press 2r to replace two characters, and so on.
  • Replace a word:

    • Press cw to replace a word.
    • Press 2cw to replace two words, and so on.
  • Add a new line below the cursor:

    • Press o to add one empty line below.
    • Press 10o to add 10 empty lines below.
  • Add a new line above the cursor:

    • Press O to add one empty line above.
  • Join lines:

    • Press j to join the current line with the line below.
    • Press 2j to join two lines, and so on.
    • Press J (capital) to join lines from above.
  • Copy the current line and paste it:

    • Press yy to copy the current line.
    • Press p to paste the copied line below the cursor.
    • Press 10yy to copy 10 lines, then press p to paste them.

Visual Mode (Selecting Text)

  • In Visual Mode, you can select text, and then perform actions like delete or copy.
  • Press v to start Visual Mode (like holding the Shift key in Windows).
  • Once text is selected, you can:
    • Delete the selected text: Press d.
    • Copy the selected text: Press y.
    • Replace the selected text: Press cw.

Learning More:

  • VIM Tutor: To learn more about VIM, use the following command:

    vimtutor

    This will guide you through the basics of using VIM.

  • VIM Adventure: Visit vim-adventure.com to play a fun game and learn VIM commands.


Summary of Key Commands

  • Command Mode: Default mode when you open VIM. Press ESC to return to this mode.
  • Insert Mode: Press i to type and edit text.
  • Visual Mode: Press v to select text.
  • Replace Mode: Press r to replace a single character.
  • Command Line Mode: Press : to run commands like save, quit, search, and replace.

Common VIM Commands:

  • Save file: :w
  • Quit VIM: :q
  • Save and quit: :wq
  • Force quit: :q!
  • Search for a word: :s/word
  • Replace word: :s/word/replacement
  • Replace all occurrences: :%s/search/replace/g
  • Undo: u
  • Redo: Ctrl + r