Nano & VIM editor & Grep utility commands

Nano

1. Starting and Exiting:

  • Open a file: nano filename.txt

  • Exit nano: CTRL + X

2. Basic Editing:

  • Write (Save) changes: CTRL + O

  • Cut text/line: CTRL + K

  • Paste (Uncut) text: CTRL + U

  • Move to the beginning of the line: CTRL + A

  • Move to the end of the line: CTRL + E

3. Navigation:

  • Move forward one character: CTRL + F

  • Move backward one character: CTRL + B

  • Move to the next line: CTRL + N

  • Move to the previous line: CTRL + P

  • Move to the beginning of the file: ALT + \

  • Move to the end of the file: ALT + /

4. Searching and Replacing:

  • Search for text: CTRL + W

  • Repeat the last search: ALT + W

  • Replace text: CTRL + \

5. Display:

  • Toggle line numbers: ALT + SHIFT + D

6. Miscellaneous:

  • Display help screen: CTRL + G

  • Justify text block: CTRL + J

  • Insert another file: CTRL + R

  • Toggle insert/overwrite mode: ALT + I

Copying:

Using the Mouse

The simplest way is to use the mouse:

  1. Select the text you want to copy using the mouse

  2. Right click and select "Copy" from the menu

  3. Move the cursor to where you want to paste

  4. Right click and select "Paste"

Using Keyboard Shortcuts

For copying longer sections of text or when the mouse is not available, use keyboard shortcuts:

  1. Move the cursor to the beginning of the text you want to copy

  2. Press Ctrl + 6 to set the mark

  3. Use the arrow keys to highlight the text you want to copy

  4. Press Alt + 6 to copy the selected text to the clipboard

  5. Move the cursor to where you want to paste

  6. Press Ctrl + U to paste the text

To copy an entire line:

  1. Move the cursor to the beginning of the line

  2. Press M-6 (Alt + 6) to copy the line

  3. Press Ctrl + U to paste the line

Copying Between Nano and Other Applications

To copy text from nano and paste it into another application, or vice versa:

  1. Use Ctrl + Shift + C to copy from nano

  2. Use Ctrl + Shift + V to paste into nano

This copies the text to the system-wide clipboard, allowing you to paste outside of nano.

Hope this helps! Let me know if you have any other questions.

Grep:

The grep command stands as a powerful tool in the UNIX/Linux world, enabling users to search for specific strings or patterns within files. Its vast array of options caters to a myriad of searching needs. Here's a detailed rundown of the commonly used grep options:

  1. Basic Search: Find a pattern within a file.

     grep 'pattern' file
    
  2. Recursive Search: Traverse directories to find the pattern.

     grep -r 'pattern' directory/
    
  3. Case-Insensitive Search: Overlook letter casing when matching.

     grep -i 'pattern' file
    
  4. Display Line Numbers: Showcase the line numbers where the pattern appears.

     grep -n 'pattern' file
    
  5. Count Occurrences: Quantify the number of times the pattern appears.

     grep -c 'pattern' file
    
  6. Whole Word Match: Ensure the pattern matches complete words.

     grep -w 'pattern' file
    
  7. Invert Match: Reveal lines that do not match the specified pattern.

     grep -v 'pattern' file
    
  8. Extended Regular Expressions: Utilize extended regex patterns for matching.

     grep -E 'pattern' file
    
  9. Matched Pattern Display: Show only the matched pattern segments.

     grep -o 'pattern' file
    
  10. Files Without Matches: Enumerate filenames devoid of the specified pattern.

    grep -L 'pattern' directory/*
    
  11. Files With Matches: Enumerate filenames containing the specified pattern.

    grep -l 'pattern' directory/*
    
  12. Specify File Patterns: Narrow down the search to specific file types.

    grep 'pattern' --include="*.txt" directory/
    
  13. Exclude File Patterns: Omit certain file types from the search.

    grep 'pattern' --exclude="*.log" directory/
    
  14. Display Context: Present lines surrounding the matched pattern. For instance, show 2 lines before and after:

    grep -C 2 'pattern' file
    
  15. Before Context: Display lines preceding the matched pattern. For instance, 2 lines before:

    grep -B 2 'pattern' file
    
  16. After Context: Display lines succeeding the matched pattern. For instance, 2 lines after:

    grep -A 2 'pattern' file
    
  17. Colorized Output: Embellish matched patterns with color, enhancing readability. This is usually set by default in numerous distributions:

    grep --color='auto' 'pattern' file
    

Armed with this knowledge, users can harness the full potential of grep to refine their search capabilities in UNIX/Linux systems.

VI editor:

                           :Navigation:
0: Move to the beginning of the current line.
$: Move to the end of the current line.
gg: Move to the beginning of the file.
G: Move to the end of the file.
<line-number>G: Move to a specific line number.
                              :Editing:
commonly used ones:
-------------------------------------------------------
dd: Delete the current line.
u: Undo the last change.
i: Enter insert mode before the cursor.
p: Paste after the cursor.
-------------------------------------------------------
a: Enter insert mode after the cursor.
I: Enter insert mode at the beginning of the line.
A: Enter insert mode at the end of the line.
o: Open a new line below the current line and enter insert mode.
O: Open a new line above the current line and enter insert mode.
x: Delete the character under the cursor.
yy: Copy (yank) the current line.
P: Paste before the cursor.
.: Repeat the last change.
                        :Searching and Replacing:
/search-term: Search for the given search term.
n: Move to the next occurrence of the search term.
N: Move to the previous occurrence of the search term.
:%s/old/new/g: Replace all occurrences of 'old' with 'new' in the entire file.
                         :Saving and Exiting:
:w: Save the changes.
:q: Quit the editor.
:wq or :x: Save changes and exit.
:q!: Quit without saving changes.

Search and replace in vim:

  1. To start a search, while in normal mode, press / and then type the search term. Press Enter.

    For example: /search_term

  2. Vi will move the cursor to the first occurrence of the search term. To move to the next occurrence, press n. To move to the previous occurrence, press N.

  3. To search backward, press ? instead of /.

  4. To highlight all occurrences of the search term, you can use the :set hlsearch command. To turn off highlighting, use :set nohlsearch.

  5. To search and replace, you can use the :s command. For example, to replace the first occurrence of "old" with "new" on the current line, you can use :s/old/new/. To replace all occurrences in the entire file, use :%s/old/new/g.

  6. To repeat the last search, simply press n (for next) or N (for previous).

Indenting Multiple Lines in vim

  1. Open the File: Open the file you want to edit using the vim command.

  2. Set the Indentation Level: After the file is open in vim:

    • Enter command mode by pressing Esc (if you aren't already in it).

    • Set the shift width using the command :set shiftwidth=2. This determines the number of spaces to be used for each indentation level.

  3. Mark Multiple Lines:

    • Position the cursor at the start of the lines you intend to indent or de-indent.

    • Enter visual line mode by pressing Shift + v.

    • Use the up/down arrow keys to select multiple lines.

  4. Indent or De-indent:

    • With the lines highlighted:

      • Press Shift + > to indent the selected lines.

      • Press Shift + < to de-indent the selected lines.

  5. Repeat the Action: If you want to further indent or de-indent the selected lines:

    • Simply press . to repeat the last action.
  6. Save and Exit:

    • Press Esc to ensure you're out of visual mode.

    • Save your changes and exit by typing :wq and pressing Enter.

    • If you wish to save the changes without exiting, type :w and press Enter.

    • To exit without saving any changes, type :q! and press Enter.

Advanced VIM commands:

In VIM there are mainly 4 types of modes:

  1. Command Mode

  2. Insert Mode

  3. Visual Mode

  4. Replace Mode

1.Command Mode:

This is the default mode when we open the file. we cannot insert any text into the file here. But we can do basic navigation using h,j,k,l keys and quick navigations using the commands shown below.

Motion commands:

Execute them in command mode to move quickly in the file.

  • h - Move left

  • j - Move down

  • k - Move up

  • l - Move right

  • w - Move to the start of the next word

  • W - Move to the start of the next word (skips punctuation)

  • e - Move to the end of the word

  • E - Move to the end of the word (skips punctuation)

  • b - Move to the start of the previous word

  • B - Move to the start of the previous word (skips punctuation)

  • 0 (zero) - Move to the start of the line

  • $ - Move to the end of the line

  • f<char> - Move to the next occurrence of <char> on the same line

  • F<char> - Move to the previous occurrence of <char> on the same line

Numbers:

In Vim, you can use numbers (often called "counts") in front of navigation commands to move a specific number of times or to a specific line. Here are some examples to illustrate this concept:

  1. Moving to a Specific Line:

    • 50G or :50: This will move the cursor to the beginning of the 50th line in the file.
  2. Moving Multiple Lines Up or Down:

    • 10j: This will move the cursor down 10 lines.

    • 5k: This will move the cursor up 5 lines.

  3. Moving Across Characters:

    • 3l: This will move the cursor 3 characters to the right.

    • 4h: This will move the cursor 4 characters to the left.

  4. Moving Across Words:

    • 7w: This will move the cursor to the beginning of the 7th word to the right.

    • 6b: This will move the cursor to the beginning of the 6th word to the left.

  5. Moving to a Specific Character:

    • 3fx: This will move the cursor to the third occurrence of the character x to the right on the current line.

    • 2Fx: This will move the cursor to the second occurrence of the character x to the left on the current line.

  6. Deleting, Changing, or Yanking Based on Movement:

    • d5j: This will delete the text from the current position and the next 5 lines below.

    • c3w: This will change the text from the current position to the end of the third word to the right.

    • y4l: This will yank (copy) the next 4 characters to the right of the cursor.

  7. Repeating Commands:

    • After performing an action like deleting (d) or changing (c), you can use a count to repeat it:

      • dd: Deletes the current line.

      • 3.: Repeats the last command (in this case, deleting a line) 3 times.

2.Insert mode:

In Vim, the primary mode you start in is Normal Mode, which is used for navigating and manipulating text. To actually input or modify text, you'll need to enter Insert Mode. Here are the ways to get into Insert Mode:

  1. Inserting Text Before the Cursor:

    • i: Enter Insert Mode before the current cursor position.
  2. Appending Text After the Cursor:

    • a: Enter Insert Mode after the current cursor position.
  3. Inserting Text at the Beginning of a Line:

    • I: Enter Insert Mode at the first non-blank character of the current line.
  4. Appending Text at the End of a Line:

    • A: Enter Insert Mode at the end of the current line.
  5. Opening a New Line Below the Current Line:

    • o: Open a new line below the current line and enter Insert Mode.
  6. Opening a New Line Above the Current Line:

    • O: Open a new line above the current line and enter Insert Mode.

3.Visual Mode:

Visual Mode is used for selecting text. You can operate on these selections with various commands. There are three primary types of Visual Mode: character-wise, line-wise, and block-wise.

  1. Character-wise Visual Mode:

    • Activation: Press v in Normal Mode.

    • Usage: Move the cursor using normal movement commands to select text character by character.

    • Example: Press v, then move right with l a few times, and you'll see characters being highlighted.

  2. Line-wise Visual Mode:

    • Activation: Press V (uppercase) in Normal Mode.

    • Usage: Move the cursor up or down to select entire lines.

    • Example: Press V and then move down with j, and entire lines will be highlighted.

  3. Block-wise Visual Mode:

    • Activation: Press Ctrl + v in Normal Mode.

    • Usage: Select rectangular blocks of text. This is especially useful for columnar data or to insert/edit text in the same column across multiple lines.

    • Example: Press Ctrl + v, move down with j a few times, and then move right with l to see a block of text being highlighted.

Once you have text selected in any Visual Mode, you can:

  • Delete: Press d to delete the selected text.

  • Copy (Yank): Press y to copy the selected text.

  • Change: Press c to change the selected text (deletes it and puts you in Insert Mode).

  • Format: Press = to auto-format the selected code (useful for languages like Python).

  • Uppercase/Lowercase: Press U to change text to uppercase, and u to change it to lowercase.

  • Indent/Outdent: Press > to increase the indentation of selected lines, and < to decrease.

Tips for Block-wise Visual Mode:

  • After selecting a block of text, you can insert text at the beginning of each line in the block by pressing I, typing your text, and then pressing Esc.

  • Similarly, you can append text to the end of each selection in the block by pressing A, typing your text, and then pressing Esc.

By mastering Visual Mode, you can efficiently manipulate text in a variety of ways. Remember, once you've made an operation in Visual Mode, Vim will automatically return to Normal Mode. If you want to repeat the last Visual selection, simply press gv in Normal Mode.

4.Replace Mode:

Replace Mode is used to overwrite characters in the text without having to delete them first and then enter Insert Mode to add new characters. It's a direct character replacement mode. Here's how to use it:

  1. Replace a Single Character:

    • Place the cursor over the character you want to replace in Normal Mode.

    • Press r.

    • Followed by the character you want to replace the current character with.

Example:

  • If you have the word "bat" and your cursor is on "b", press ra to replace "b" with "a", resulting in the word "aat".
  1. Replace Multiple Characters (Enter Replace Mode):

    • Place the cursor where you want to start replacing characters in Normal Mode.

    • Press R (uppercase).

    • Now, as you type, every character you input will replace the character under the cursor, and the cursor will move to the next position. It's like typing over the text.

    • Press Esc to exit Replace Mode and return to Normal Mode.

Example:

  • If you have the text "apple" and your cursor is on "a", press R, type "grape", and then press Esc. The text will now read "grape".

Finally Editing:

In Vim, text editing efficiency is achieved through a combination of operators and motions.

Operators: Actions applied to text.

  • d: Delete text.

  • c: Change text.

  • y: Yank (copy) text.

  • p/P: Paste text after/before cursor.

  • >: Increase indentation.

  • <: Decrease indentation.

  • ~: Toggle character case.

  • gu: Make text lowercase.

  • gU: Make text uppercase.

  • J: Join lines.

Motions: Define the range or target of an operator.

  • w: Move to start of next word.

  • e: Move to end of word.

  • b: Move to beginning of word.

  • $: Move to end of line.

  • ^: Move to start of line.

  • G: Go to end of file.

  • gg: Go to start of file.

  • %: Jump to matching bracket.

  • }: Jump to next paragraph.

  • {: Jump to previous paragraph.

By combining operators with motions, Vim users can execute complex text manipulations with minimal keystrokes, enhancing productivity.

Eg:

Popular Vim Operators and Motions Table:

OperatorsDescriptionMotionsDescription
dDeletewMove to the start of the next word
cChangeeMove to the end of the word
yYank (copy)bMove to the beginning of the word
p / PPut (paste after/before)$Move to the end of the line
>Shift right (increase indentation)^ or 0Move to the beginning of the line
<Shift left (decrease indentation)GMove to the end of the file
~Toggle case of characterggMove to the start of the file
guConvert to lowercase%Move to matching parenthesis/bracket/brace
gUConvert to uppercase}Move to the next paragraph or block
JJoin lines{Move to the previous paragraph or block

In Vim, operators are commands that perform actions on text. They often require a motion to define which text they operate on. Here's a list of some common operators:

  1. Delete (d):

    • Removes the specified text.

    • Examples:

      • dw: Delete from the cursor to the start of the next word.

      • d$: Delete from the cursor to the end of the line.

  2. Change (c):

    • Removes the specified text and puts you into Insert Mode.

    • Examples:

      • cw: Change from the cursor to the start of the next word.

      • c$: Change from the cursor to the end of the line.

  3. Yank (copy) (y):

    • Copies the specified text to the unnamed register (clipboard).

    • Examples:

      • yw: Yank from the cursor to the start of the next word.

      • y$: Yank from the cursor to the end of the line.

  4. Put (paste) (p or P):

    • p: Puts the text from the unnamed register after the cursor.

    • P: Puts the text from the unnamed register before the cursor.

  5. Shift (> or <):

    • Changes the indentation of the specified lines.

    • Examples:

      • >>: Increase indentation of the current line.

      • <<: Decrease indentation of the current line.

  6. Filter (!):

    • Filters the specified text through an external command.

    • Example:

      • !Gsort: Filter all lines from the cursor to the end of the file through the sort command.
  7. Case Conversion (~, gu, gU):

    • ~: Switch case of the character under the cursor.

    • gu: Convert specified text to lowercase.

    • gU: Convert specified text to uppercase.

    • Examples:

      • guw: Convert the word under the cursor to lowercase.

      • gUw: Convert the word under the cursor to uppercase.

  8. Join (J):

    • Joins the current line with the next one.

    • Example:

      • 3J: Join the current line with the next 3 lines.
  9. Rotate (g?):

    • Rotates the characters of the specified text 13 positions in the alphabet, essentially performing the ROT13 encoding.

    • Example:

      • g?w: Rotate the characters of the word under the cursor.
  10. Repeat (.):

    • Repeats the last change made in Normal Mode.

These operators can often be combined with motions (like w, $, 0, etc.) or text objects (like aw, ip, etc.) to define the region of text they should act upon. This combination of operators and motions/text objects is one of the features that makes Vim powerful and efficient for text editing.

Reference video: must watch

CKAD/CKA vim editos have this configuration:

set expandtab

set tabstop=2

set shiftwidth=2

  1. set expandtab: This tells Vim to convert tabs into spaces. This can be helpful if you want to make sure that your indentation looks the same on any system, regardless of the tab settings.

  2. set tabstop=2: This sets the number of spaces that a tab character represents. In this case, every time you press the Tab key, Vim will insert two spaces.

  3. set shiftwidth=2: This sets the number of spaces to use for each step of (auto)indent. This is usually set to the same value as tabstop to keep indentations consistent.

You would place these commands in your .vimrc file, which is the configuration file for Vim that's read every time Vim starts up. The .vimrc file is located in the user's home directory (e.g., ~/.vimrc on Unix-based systems or $HOME\_vimrc on Windows).

If you want to apply these settings immediately within an open Vim session, you can enter them by first pressing the Esc key (to make sure you're in normal mode) and then typing : to enter command mode, followed by the command, such as :set expandtab, and pressing Enter.