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:
Select the text you want to copy using the mouse
Right click and select "Copy" from the menu
Move the cursor to where you want to paste
Right click and select "Paste"
Using Keyboard Shortcuts
For copying longer sections of text or when the mouse is not available, use keyboard shortcuts:
Move the cursor to the beginning of the text you want to copy
Press
Ctrl + 6
to set the markUse the arrow keys to highlight the text you want to copy
Press
Alt + 6
to copy the selected text to the clipboardMove the cursor to where you want to paste
Press
Ctrl + U
to paste the text
To copy an entire line:
Move the cursor to the beginning of the line
Press
M-6
(Alt + 6) to copy the linePress
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:
Use
Ctrl + Shift + C
to copy from nanoUse
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:
Basic Search: Find a pattern within a file.
grep 'pattern' file
Recursive Search: Traverse directories to find the pattern.
grep -r 'pattern' directory/
Case-Insensitive Search: Overlook letter casing when matching.
grep -i 'pattern' file
Display Line Numbers: Showcase the line numbers where the pattern appears.
grep -n 'pattern' file
Count Occurrences: Quantify the number of times the pattern appears.
grep -c 'pattern' file
Whole Word Match: Ensure the pattern matches complete words.
grep -w 'pattern' file
Invert Match: Reveal lines that do not match the specified pattern.
grep -v 'pattern' file
Extended Regular Expressions: Utilize extended regex patterns for matching.
grep -E 'pattern' file
Matched Pattern Display: Show only the matched pattern segments.
grep -o 'pattern' file
Files Without Matches: Enumerate filenames devoid of the specified pattern.
grep -L 'pattern' directory/*
Files With Matches: Enumerate filenames containing the specified pattern.
grep -l 'pattern' directory/*
Specify File Patterns: Narrow down the search to specific file types.
grep 'pattern' --include="*.txt" directory/
Exclude File Patterns: Omit certain file types from the search.
grep 'pattern' --exclude="*.log" directory/
Display Context: Present lines surrounding the matched pattern. For instance, show 2 lines before and after:
grep -C 2 'pattern' file
Before Context: Display lines preceding the matched pattern. For instance, 2 lines before:
grep -B 2 'pattern' file
After Context: Display lines succeeding the matched pattern. For instance, 2 lines after:
grep -A 2 'pattern' file
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:
To start a search, while in normal mode, press
/
and then type the search term. Press Enter.For example:
/search_term
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, pressN
.To search backward, press
?
instead of/
.To highlight all occurrences of the search term, you can use the
:set hlsearch
command. To turn off highlighting, use:set nohlsearch
.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
.To repeat the last search, simply press
n
(for next) orN
(for previous).
Indenting Multiple Lines in vim
Open the File: Open the file you want to edit using the
vim
command.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.
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.
Indent or De-indent:
With the lines highlighted:
Press
Shift + >
to indent the selected lines.Press
Shift + <
to de-indent the selected lines.
Repeat the Action: If you want to further indent or de-indent the selected lines:
- Simply press
.
to repeat the last action.
- Simply press
Save and Exit:
Press
Esc
to ensure you're out of visual mode.Save your changes and exit by typing
:wq
and pressingEnter
.If you wish to save the changes without exiting, type
:w
and pressEnter
.To exit without saving any changes, type
:q!
and pressEnter
.
Advanced VIM commands:
In VIM there are mainly 4 types of modes:
Command Mode
Insert Mode
Visual Mode
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 leftj
- Move downk
- Move upl
- Move rightw
- Move to the start of the next wordW
- Move to the start of the next word (skips punctuation)e
- Move to the end of the wordE
- Move to the end of the word (skips punctuation)b
- Move to the start of the previous wordB
- 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 linef<char>
- Move to the next occurrence of<char>
on the same lineF<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:
Moving to a Specific Line:
50G
or:50
: This will move the cursor to the beginning of the 50th line in the file.
Moving Multiple Lines Up or Down:
10j
: This will move the cursor down 10 lines.5k
: This will move the cursor up 5 lines.
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.
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.
Moving to a Specific Character:
3fx
: This will move the cursor to the third occurrence of the characterx
to the right on the current line.2Fx
: This will move the cursor to the second occurrence of the characterx
to the left on the current line.
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.
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:
Inserting Text Before the Cursor:
i
: Enter Insert Mode before the current cursor position.
Appending Text After the Cursor:
a
: Enter Insert Mode after the current cursor position.
Inserting Text at the Beginning of a Line:
I
: Enter Insert Mode at the first non-blank character of the current line.
Appending Text at the End of a Line:
A
: Enter Insert Mode at the end of the current line.
Opening a New Line Below the Current Line:
o
: Open a new line below the current line and enter Insert Mode.
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.
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 withl
a few times, and you'll see characters being highlighted.
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 withj
, and entire lines will be highlighted.
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 withj
a few times, and then move right withl
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, andu
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 pressingEsc
.Similarly, you can append text to the end of each selection in the block by pressing
A
, typing your text, and then pressingEsc
.
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:
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".
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 pressEsc
. 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:
Operators | Description | Motions | Description |
d | Delete | w | Move to the start of the next word |
c | Change | e | Move to the end of the word |
y | Yank (copy) | b | Move to the beginning of the word |
p / P | Put (paste after/before) | $ | Move to the end of the line |
> | Shift right (increase indentation) | ^ or 0 | Move to the beginning of the line |
< | Shift left (decrease indentation) | G | Move to the end of the file |
~ | Toggle case of character | gg | Move to the start of the file |
gu | Convert to lowercase | % | Move to matching parenthesis/bracket/brace |
gU | Convert to uppercase | } | Move to the next paragraph or block |
J | Join 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:
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.
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.
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.
Put (paste) (
p
orP
):p
: Puts the text from the unnamed register after the cursor.P
: Puts the text from the unnamed register before the cursor.
Shift (
>
or<
):Changes the indentation of the specified lines.
Examples:
>>
: Increase indentation of the current line.<<
: Decrease indentation of the current line.
Filter (
!
):Filters the specified text through an external command.
Example:
!Gsort
: Filter all lines from the cursor to the end of the file through thesort
command.
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.
Join (
J
):Joins the current line with the next one.
Example:
3J
: Join the current line with the next 3 lines.
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.
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
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.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.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 astabstop
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.