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.
The cat command is used to view the content of a file. It's simple and easy to use.
cat filenameThis will display the content of the file filename.
If the file is too long to fit on one screen, you can use less to scroll through the content:
cat filename | lessor simply:
less filename- 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.
You can create a new file with the cat command:
cat > file1.txtAfter you press Ctrl + C, the file is saved, but the content may not include the line where you pressed Ctrl + C.
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.
eofThis will add the content to the file and close it with eof.
You can combine content from multiple files and put them into one new file:
cat hosts messages etc > file1.txtThis combines the contents of hosts, messages, and etc into file1.txt.
To show the line numbers along with the content:
cat -n filenameTo show a $ at the end of each line (useful for checking empty lines):
cat -e filenameNANO
nano is a simple text editor, similar to Notepad, that allows you to create and edit files directly in the terminal.
yum install nanoTo open a file for editing:
nano filenameIf the file does not exist, nano will create a new one.
- 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: Press Alt + U.
- Redo: Press Alt + E.
You can read an existing file into nano by opening it directly:
nano /path/to/fileOr, if you want to create a new file but load the content of an existing one (e.g., log/passwd):
nano newfileAfter opening newfile, you can use Ctrl + R to load the content of log/passwd into newfile.
- 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
- 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 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.
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.
- Command Mode (Default mode when you open VIM)
- Insert Mode (To insert/edit text)
- Visual Mode (To select text)
- Replace Mode (To replace characters)
- Command Line Mode (To run commands)
To open a file with VIM, use this command:
vim filename.txt- 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.
- Insert Mode: Press
iorInsertto switch to Insert Mode. This allows you to type text. - Visual Mode: Press
vto enter Visual Mode. This allows you to select text. - Replace Mode: Press
rto 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.
- 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!
You can run system commands while in Command Line Mode:
- To list files with
ls::!ls - To show the current date:
:!date
-
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
gmeans 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
- To undo the last action, press:
u
- To redo the last undone action, press:
Ctrl + r
-
Delete a character under the cursor:
- Press
xto delete one character. - Press
2xto delete two characters.
- Press
-
Delete a word:
- Press
dwto delete one word. - Press
2dwto delete two words.
- Press
-
Delete a line:
- Press
ddto delete one line. - Press
3ddto delete three lines.
- Press
-
Replace a character:
- Press
rand then type the character you want to replace it with. - Press
2rto replace two characters, and so on.
- Press
-
Replace a word:
- Press
cwto replace a word. - Press
2cwto replace two words, and so on.
- Press
-
Add a new line below the cursor:
- Press
oto add one empty line below. - Press
10oto add 10 empty lines below.
- Press
-
Add a new line above the cursor:
- Press
Oto add one empty line above.
- Press
-
Join lines:
- Press
jto join the current line with the line below. - Press
2jto join two lines, and so on. - Press
J(capital) to join lines from above.
- Press
-
Copy the current line and paste it:
- Press
yyto copy the current line. - Press
pto paste the copied line below the cursor. - Press
10yyto copy 10 lines, then presspto paste them.
- Press
- In Visual Mode, you can select text, and then perform actions like delete or copy.
- Press
vto 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.
- Delete the selected text: Press
-
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.
- Command Mode: Default mode when you open VIM. Press ESC to return to this mode.
- Insert Mode: Press
ito type and edit text. - Visual Mode: Press
vto select text. - Replace Mode: Press
rto replace a single character. - Command Line Mode: Press
:to run commands like save, quit, search, and replace.
- 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