> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/linux-commands/text-editors.md).

# Text Editors

## **`nano`**: Simple Text Editor

**Examples:**

* Open a file for editing:

  ```bash
  nano filename.txt
  ```
* Create a new file or edit an existing file:

  ```bash
  nano newfile.txt
  ```
* Save changes and exit:
  * Press `Ctrl + O` to save, then press `Enter` to confirm.
  * Press `Ctrl + X` to exit the editor.
* Exit without saving changes:
  * Press `Ctrl + X`, then press `N` when prompted to save changes.
* Cut a line of text:
  * Place the cursor on the line and press `Ctrl + K`.
* Paste the cut line:
  * Move the cursor to the desired location and press `Ctrl + U`.
* Search for text:
  * Press `Ctrl + W` and enter the search term.
* Go to a specific line number:
  * Press `Ctrl + _`, then enter the line number and press `Enter`.
* Enable line numbers:
  * Press `Ctrl + C` to display the current line number at the bottom of the editor.

***

## **`vim`**: Advanced Text Editor

#### **Opening and Creating Files in `vim`**

* Open a file for editing:

  ```bash
  vim filename.txt
  ```
* Create a new file or edit an existing file:

  ```bash
  vim newfile.txt
  ```

***

#### **Vim Modes**

Vim has different modes for editing and navigation:

1. **Normal Mode** (Default) - Used for navigation, deleting, copying, and executing commands.
   * Press `Esc` to enter Normal Mode.
2. **Insert Mode** - Used to enter and edit text.
   * Press `i` to enter Insert Mode.
   * Press `Esc` to return to Normal Mode.
3. **Replace Mode** - Used to replace characters while typing.
   * Press `R` to enter Replace Mode.
4. **Visual Mode** - Used for selecting text.
   * Press `v` to enter the character-wise selection.
   * Press `V` to select entire lines.
   * Press `Ctrl + v` for block selection.
5. **Command Mode** - Used for executing commands.
   * Press `:` to enter Command Mode.

***

#### **Saving and Exiting**

* Save changes and exit:

  ```vim
  :wq  
  ```

  or press `ZZ` (Shift + zz).
* Save without exiting:

  ```vim
  :w
  ```
* Exit without saving changes:

  ```vim
  :q!
  ```

***

#### **Basic Editing Commands**

* **Delete a line:**

  ```vim
  dd
  ```
* **Undo the last action:**

  ```vim
  u
  ```
* **Redo the last undone action:**

  ```vim
  Ctrl + r
  ```
* **Copy (Yank) a line:**

  ```vim
  yy
  ```
* **Paste copied text:**

  ```vim
  p
  ```
* **Cut (Delete) multiple lines (e.g., 3 lines):**

  ```vim
  3dd
  ```
* **Copy multiple lines (e.g., 3 lines):**

  ```vim
  3yy
  ```

***

#### **Navigation Commands**

* Move the cursor left (`h`), right (`l`), up (`k`), down (`j`).
* Move to the beginning of the line:

  ```vim
  0
  ```
* Move to the end of the line:

  ```vim
  $
  ```
* Move to a specific line number (e.g., line 20):

  ```vim
  :20
  ```
* Move to the last line:

  ```vim
  G
  ```
* Move to the first line:

  ```vim
  gg
  ```

***

#### **Searching and Replacing Text**

* **Search for text (e.g., `error`)**:

  ```vim
  /error
  ```
* **Find next occurrence:**

  ```vim
  n
  ```
* **Find previous occurrence:**

  ```vim
  N
  ```
* **Replace all occurrences of "old" with "new" in the file:**

  ```vim
  :%s/old/new/g
  ```
* **Replace "old" with "new" on specific lines (e.g., lines 5 to 10):**

  ```vim
  :5,10s/old/new/g
  ```

***

#### **Working with Multiple Files in Vim**

* **Open multiple files in tabs:**

  ```vim
  vim file1.txt file2.txt
  ```
* **Switch between tabs:**

  ```vim
  :tabnext
  ```

  ```vim
  :tabprev
  ```
* **Open a new file in a tab:**

  ```vim
  :e newfile.txt
  ```
* **Close the current tab:**

  ```vim
  :q
  ```

***

#### **Enabling Line Numbers and Syntax Highlighting**

* **Enable line numbers:**

  ```vim
  :set number
  ```
* **Disable line numbers:**

  ```vim
  :set nonumber
  ```
* **Enable syntax highlighting:**

  ```vim
  :syntax on
  ```

***

#### **Splitting Windows in Vim**

* **Split window horizontally:**

  ```vim
  :split filename.txt
  ```
* **Split window vertically:**

  ```vim
  :vsplit filename.txt
  ```
* **Switch between split windows:**

  ```vim
  Ctrl + w + arrow_key
  ```
* **Close the current split window:**

  ```vim
  :q
  ```
