Editing Files in Command Line
Usually, when we simply edit scripts or configuration files, we don't use SFTP to download, edit, and then upload and overwrite. We can use the Vim editor for editing.
Vim has a certain learning curve and operates differently from conventional text editors. Please refer to this tutorial: link.
Here are some basic operations (highlighted ones must be learned before using Vim) to avoid getting stuck in Vim without being able to exit.
1) Open a file:
2) Some operations in normal mode:
- Arrow keys: Navigation
h
: Move leftj
: Move downk
: Move upl
: Move rightw
: Move to the beginning of the next worde
: Move to the end of the next wordb
: Move to the beginning of the previous wordgg
: Move to the topG
: Move to the endyy
: Copy the current lineyw
: Copy a wordp
: Paste2p
: Paste twicedd
: Delete the current line.
: Repeat the previous operationu
: Undo the previous operationctrl+r
: Redo the previous operation:%s/old str/new str/g
: Global replace
3) Enter insert mode:
i
: Insert before the current characterI
: Insert at the beginning of the linea
: Insert after the current characterA
: Insert at the end of the lineo
: Insert a new line belowO
: Insert a new line aboveci{
: Delete the content inside the{
brackets and enter insert mode
4) Press Esc
to exit insert mode, and enter the following to exit:
:wq
: Save and exit:q!
: Force exit
Note that the colon :
is necessary.
Last update: September 16, 2023