(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.

Commands Line for File Editor in Linux (CentOS, Ubuntu)
Commands Line for File Editor in Linux (CentOS, Ubuntu)

 

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

Linux Text Editor with VI
Linux Text Editor with VI (photo: howtogeek.com)

– 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

COMMANDPURPOSE / DESCRIPTION
iSwitch to Insert Mode, character is written in front of the cursor
lBefore the first character on the line
aAfter the cursor
AAfter the first character on the line
0Below the current line
OAbove the current line
rReplace the current character
RReplace until the press
ESCSwitch back to Command Mode

? Group Commands for File actions in VI

COMMANDPURPOSE / DESCRIPTION
:wSave the file
: xSave the file and exit Insert Mode
: wqSave the file and exit Insert Mode (same above)
:rOpen/ read a file
:q!Exit without saving the file content
:qExit if no change made
:wSave into a new file

? Group Commands for moving the cursor in VI

COMMANDPURPOSE / DESCRIPTION
hGo left 1 space
eGo right 1 space
wGo right 1 word
kGo above 1 line
jGo down 1 line
OIn the current line
rReplace the current character
RReplace 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

COMMANDPURPOSE / DESCRIPTION
dwDelete 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
3dwDelete 3 words
ddDelete the current line
xDelete 1 character
yCopy
uUndo the previous operation
3yyCopy 3 consecutive lines
yyCopy the entire line at the cursor position
y$Copy from current cursor to the last
pPaste

? Group Commands to find and replace in VI

COMMANDPURPOSE / DESCRIPTION
?Find upward
/Find down
*/andFind next words of “and”
*?andFind words ending by “and”
*/nTheFind from the next line begins with “The”
nFind downward
NFind upward
cwReplace 1 word
3cwReplace 3 words
ccReplace the current line
5ccReplace 5 lines
:s/text1/text2/gReplace “text1” by “text2”
:g/one/s/1/gReplace “one” by “1”
 :1.$s/file/folderReplace 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

Linux Text Editor with NANO
Linux Text Editor with NANO (photo: itsfoss.com)

Some common NANO keyboard shortcuts:

COMMANDPURPOSE / DESCRIPTION
  Ctrl-OSave file
  Ctrl-ROpen file
  Ctrl-XClose file
  Ctrl-GHelp

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.

Guide to edit files in Linux Server via Windows FTP client
Edit files in Linux Server via Windows FTP client

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 

Add Comment