We can use editors to edit file content.
There are multiple editors available:
- geditor
- vi editor
- nano editor
etc.
It is a graphical editor, similar to Windows Notepad.
$ gedit file1.txt
$ gedit first.shecho "This is my first shell script"
mkdir dir{1..6}
echo "six directories got created"
date
cal$ chmod u+x first.sh
$ ./first.sh- gedit can work only on the desktop version and does not work on server versions.
- If we are connecting to a remote server using putty, then gedit cannot be used.
- In such cases, we must use vi editor.
- gedit and nano are Linux-based editors.
- vi editor is Unix-based.
- vi editor can work in both desktop and server environments.
vi means Visual Editor. It is used to create new files or edit existing files.
vi file1.txt-
If
file1.txtdoes not exist, a new file will be created and opened for editing. -
To save and exit this empty file, use
:wq(wmeans save, andqmeans quit). -
If the file already exists,
viwill open the file ready for editing.
The vi editor operates in three primary modes:
- This is the default mode.
- Allows the execution of
vicommands. - Switch to Insert Mode by pressing:
i(commonly used)- Other commands like
a,I, etc.
- Used to modify the file contents:
- Insert new data.
- Append to existing data.
- Return to Command Mode by pressing
<Esc>.
- Used to quit the editor.
- Accessed from Command Mode by typing
:.
A: Append data at the end of the line.I: Insert data at the beginning of the line.a: Append data to the right of the cursor.i: Insert data to the left of the cursor.
x: Delete the character under the cursor.3x: Delete 3 characters.X: Delete the previous character.3X: Delete the previous 3 characters.
dw: Delete the current word.3dw: Delete 3 words.
dd: Delete the current line.3dd: Delete 3 lines.d$: Delete from the cursor to the end of the line.d^: Delete from the cursor to the beginning of the line.dgg: Delete from the start of the file to the cursor.dG: Delete from the cursor to the end of the file.
r: Replace the current character.R: Replace multiple characters.Sorcc: Replace an entire line.
O: Open a new line above the cursor.o: Open a new line below the cursor.