(Linux Command Line) Part 3 - Commands Line for File Editor in Linux (CentOS, Ubuntu)
In part 3 of serial Linux Commands, we will learn how to use File Editor in Linux. There are many tools that helps us to edit files in Linux (text editor). We can even edit Linux files from our Windows via FTP client (such as CuteFTP or FileZilla) or we can edit Linux files directly via command line (such as vi, nano, vim, gedit, emacs …). Remember that Commands in CentOS and Ubuntu are very similar, but sometimes there are differences.
Serial Linux Commands:
๐ Part 1 – Linux Commands for Server Management and Monitoring
๐ Part 2 – Linux Commands for File and Folder/ Directory Management
๐ Part 3 – Linux Commands for File Editor in Linux (CentOS, Ubuntu)
๐ Part 4 – Linux Commands for Network Configuration in Linux (CentOS, Ubuntu)
๐ Part 5 – Other Useful Linux Commands (CentOS, Ubuntu) you need to know
Editing files in Linux server from Windows via FTP client is quite visual and easy, but sometimes, we have to edit files directly in Linux servers via Command Line. In this article, I will guide you how to edit TEXT file in Linux by 4 ways: 1- Command Line using VI Text Editor, 2-Command Line using NANO Text Editor, 3-Command Line using other Text Editor, 4- Edit file from Windows via FTP client
Bonus: Share “All Linux Command Line” document, download here.
Commands Line for File Editor in Linux (CentOS, Ubuntu)
1) Linux Text Editor with VI
VI is the standard text editor on Unix Operating Systems. It is a visual editor, operating under two modes: Command Mode (Command Line) and Insert Mode (Edit mode)
To edit a file, we use the below command:
VI filepath/filename
For example, to edit PHP.INI file, just type:
VI php.ini
– In the Command Mode, we can only use the keyboard to perform actions such as moving the cursor, save data, open the file … we cannot edit text in this mode
– To edit text (edit file content), we need to switch to Insert Mode. Insert Mode allows using keyboard to edit file contents.
Note: In the Insert Mode, we can not use commands. To enable commands, we need to exit Insert Mode by pressing ESC key
๐ Group Commands in the Insert Mode
COMMAND | PURPOSE / DESCRIPTION |
i | Switch to Insert Mode, character is written in front of the cursor |
l | Before the first character on the line |
a | After the cursor |
A | After the first character on the line |
0 | Below the current line |
O | Above the current line |
r | Replace the current character |
R | Replace until the press |
ESC | Switch back to Command Mode |
๐ Group Commands for File actions in VI
COMMAND | PURPOSE / DESCRIPTION |
:w | Save the file |
: x | Save the file and exit Insert Mode |
: wq | Save the file and exit Insert Mode (same above) |
:r | Open/ read a file |
:q! | Exit without saving the file content |
:q | Exit if no change made |
:w | Save into a new file |
๐ Group Commands for moving the cursor in VI
COMMAND | PURPOSE / DESCRIPTION |
h | Go left 1 space |
e | Go right 1 space |
w | Go right 1 word |
k | Go above 1 line |
j | Go down 1 line |
O | In the current line |
r | Replace the current character |
R | Replace until the press |
) | End of the sentence |
( | Start of the sentence |
} | End of the paragraph |
{ | Start of the paragraph |
๐ Group Commands to delete, copy, paste, undo in VI
COMMAND | PURPOSE / DESCRIPTION |
dw | Delete 1 word |
d^ | Delete characters from the cursor to the beginning of the line |
d$ | Delete characters from the cursor to the end of the line |
3dw | Delete 3 words |
dd | Delete the current line |
x | Delete 1 character |
y | Copy |
u | Undo the previous operation |
3yy | Copy 3 consecutive lines |
yy | Copy the entire line at the cursor position |
y$ | Copy from current cursor to the last |
p | Paste |
๐ Group Commands to find and replace in VI
COMMAND | PURPOSE / DESCRIPTION |
? | Find upward |
/ | Find down |
*/and | Find next words of “and” |
*?and | Find words ending by “and” |
*/nThe | Find from the next line begins with “The” |
n | Find downward |
N | Find upward |
cw | Replace 1 word |
3cw | Replace 3 words |
cc | Replace the current line |
5cc | Replace 5 lines |
:s/text1/text2/g | Replace “text1” by “text2” |
:g/one/s/1/g | Replace “one” by “1” |
:1.$s/file/folder | Replace file by folder from row 1 |
2) Linux Text Editor with NANO
NANO is Ubuntu built-in Text Editor. It is pretty simple and easy to use, allowing us to edit files with basic features such as opening files, editing files and saving files …
Syntax:
nano filepath/filename
Some common NANO keyboard shortcuts:
COMMAND | PURPOSE / DESCRIPTION |
Ctrl-O | Save file |
Ctrl-R | Open file |
Ctrl-X | Close file |
Ctrl-G | Help |
3) Other Text Editor in Linux
Beside usual Text Editor (VI and NANO), there are many other Linux Text Editors, such as Gedit, Emacs, Kate, Geany, Kwrite, Nedit, Scribes, SciTE, Medit, Gtkedit… Each Editor supports various features. I often use VI, if you want to learn about other Editors, you can find more from Google.
4) Guide to edit files in Linux Server via Windows FTP client
In many cases, especially those who have never worked with Linux, they don’t want to use even one simple command line; or if the content of Linux file is closely related to a file(s) in Windows (you may want to copy and paste from Windows). In such case, editing Linux file via Windows FTP client will really help.
Here is how to edit Linux file via Windows FTP client:
– Connect to Linux server via FTP client (CuteFTP or Filezilla)
– Browse to the folder that contains the file(s) you want to edit
– Right-click on the file then select View/Edit
– FTP Client program will download the file from Linux server then open it using the default text editor program in Windows (Notepad, Notepad++ …) => You can edit the file as you want very easily (in Windows Notepad or Notepad++)
– After editing completed, you close Notepad/ Notepad++ => Select “Upload” or “Save” in the confirmation dialog => Wait until uploading finished. Done!
Tutorial video: How to use VI editor in Linux CentOS, Ubuntu
Source: https://easytipz.com/computer-tips/linux-command-line-part-3-commands-line-for-file-editor-in-linux-centos-ubuntu/
No comments: