The Vim is a tool editor text. It is used as the most popular and powerful tool. In the context of Vim, it can operate in two modes: command mode and insert mode. The editor always starts in command mode. In the command mode, we can move through the text, search, replace, mark blocks and switch the editor to insert mode. The first, we will install Vim.
On ubuntu
sudo apt-get install vim
On CentOS
sudo yum install vim
We use command vi file_name to open file in the Vim editor. We will start with some basic commands.
Basic commands
- h to move the cursor to the left
- l to move it to the right
- k to move it to the up
- j to move it to the down
- G to move it to the begin of last line
- gg to move it to the begin of first line
- :n to move it to line n of the file
Basic operations
- n dd will delete n lines starting from the current cursor position.
- u will undo.
- ctr + r will redo.
- n dw will delete n words starting from the current cursor position to right.
- n db will delete n words starting from the current cursor position to left.
- x will delete character on which the cursor is positioned.
- :w save file.
- :q will exit the editor.
- :q! will force the exit when user want unsaved changes.
- :wq will save and exit
- :w new_file will save the text to new_file
- /string_for_search will search the string up-down in the file, position of cursor on the first match.
- / will perform the same search again, moving the cursor to the next match.
- ?string will search the string down-up.
- :1,$s/old_text/new_text/g will replace old_text with new_text throughout the file.
- yy will copy a block of text.
- n p will paste it n times.
- a will change to append text to file.
- i will change to insert mode.
- esc will change to command mode.
Scrolling
- ctr + f will scroll down one screen.
- ctr + b will scroll up on screen.
Split screen
- :vsplit file_name will vertical split screen and load another file
- :split file_name will horizontal split screen and load another file
- ctr + w will move cursor to next screen.
When we opened file with the Vim editor, the file is opened with not set number line. To set number line please enter :set nu. To set default open file with set number line, we can edit /etc/vim/vimrc and append set number to this file. To redefine tab as 4 spaces use :set tabstop=4 or append set tabstop=4 into /etc/vim/vimrc
Nhận xét
Đăng nhận xét