This is Kamijo of the Development Department.
This article is for vim beginners.
Click here for table of contents
- 1. vim input and modes
- 2. a collection of commands for vim beginners to learn
About vim input and modes
If you have a little experience with vim, you can skip this section.
There are several modes in vim.
First, let's learn about normal mode and insertion mode.
In normal mode, you can mainly move the cursor.
You can use the hjkl keys to move up, down, left, or right. (h = left, j = down, k = up, l = right)
In insert mode, you can enter text.
This state allows almost the same behavior as a common text editor.
To change from normal mode to insert mode, use i to enter insert mode from the left of the cursor or a to enter insert mode from the right of the cursor.
Conversely, to return from insert mode to normal mode, enter the esc key.
If you get lost, you can usually get back to normal mode by hitting the esc key repeatedly.
If you are not familiar with vim, you can use the directional keys in insert mode to move around.
for exampleaiueo(at sentence-end, falling tone) indicates a confident conclusionuSuppose you forgot to enter If you are currently in insert mode and aieo| Press the left directional key twice to use the same text editor as a normal text editor. ai|eo You can move the cursor as in the following example.
If you get used to vim, you can do the same with esc (to normal mode), h (to the left), i (from the left of the cursor to insert mode).
The directional keys are far away, so eventually you should be able to operate without using them.
If you feel that the Esc key is too far away, please add the following line to setting.json in case of vscode.
"vim.insertModeKeyBindings": [{"before": ["j", "j"], "after": [""]}],
In the case of vim, the configuration file
inoremap jj
Let's add this one line. By pressing j twice in insert mode, the action is the same as pressing the esc key.
There is another mode that is often used, the visual mode, but it is not presented in this article.
A collection of commands that vim beginners should learn
Now that you are able to type basic characters, we will introduce commands that you should learn and use frequently.
How to enter insertion mode
★★★★ From the left of the "i" cursor
★★★★ From the right of the "a" cursor
★★From the leftmost of the "I" row
★★From the far right of row "A
★★★ Insert one line under the "o" cursor to insert mode.
★★★★ Insert one line above the "O" cursor to insert mode.
cancel
★★Cancel the operation just before "u".
★★★ "Ctrl-r" Undo Undo
This function is often assigned to Ctrl+z, Ctrl+y in other applications and editors.
Movement system (←/→)
★★★★ "w" to the top of the next word
★★★★ "b" to the top of the previous word
★★★★★★ To the end of the "e" word
★★★★ "0" to the beginning of the line
★★★★ "$" to the end of the line
★★★★ "f+?" After pressing f, press any key to align the cursor with the nearest pressed alphabet to the right of the cursor
★★★★ "F+?" After pressing F, press any key to align the cursor with the nearest pressed alphabet to the left of the cursor
For moving to the beginning or end of a line, "I" or "A" is often used, since the insert mode is often entered after the move.
Movement system (↑, ↓)
★★★To the top of the "gg" file
★★★ Go to the last line of the "G" file
It is not necessary to learn it right away. You will learn it if you look up the command each time you want to go to the first or last line.
scroll
Ctrl-d" Scroll down half a page.
Ctrl-u" Scroll halfway up the page.
Scroll down one page.
Ctrl-f" Scroll up one page
Personally, I have no problem with only half-page scrolling, so I assign Ctrl-b and Ctrl-f to other functions.
copy and paste
Copy one line of "yy" cursor
★★★ Copy from the "y$" cursor to the end of the line
★★★★ Copy from the "yw" cursor position to the end of the word
★★★ "yiw" (yank inner word without whitespace) Copy the word at the cursor
Copy the word at the cursor with whitespace.
★★★★ Paste after "p" cursor
★★★★ Paste before "P" cursor
Since y$ is used often, it is convenient to assign it to Y. It is easy to remember because the movement is similar to D for deletion.
For vscode setting.json
"vim.normalModeKeyBindings": [
{ "before": ["Y"], "after": ["y", "$"] }
]
For vim .vimrc, init.vim etc...
nnoremap Y y$
Deletion (cut out)
★★★★ Delete "x" cursor character
★★★★ Delete from "D" cursor to end of line
★★★★ "dw" Delete from cursor position to the end of the word
★★★ Delete the word "diw" cursor
★☆☆☆ Delete "daw" cursor words with spaces
★★★ Delete one line of "dd" cursor
Deletion is the same as windows ctrl+x or other cut functions, and the deleted item can be pasted with p.
Since this cropping may not be necessary, and I don't want the copy to be overwritten every time I delete a letter, especially when I delete a letter with x or s, I use the following settings
The cut function can be eliminated.
For vscode setting.json
"vim.normalModeKeyBindingsNonRecursive": [
{ "before": ["x"], "after": ["\"", "_", "x"] },
{ "before": ["s"], "after": ["\"", "_", "s"] }
]
For vim .vimrc, init.vim etc...
nnoremap x "_x
nnoremap s "_s
Delete (cut out) and insert mode
★★★ "cw" "dw" + insert mode
★★★★ "C" "D" + insert mode
★★★ "ciw" "diw" + insert mode
★☆☆☆ "caw" "daw" + insert mode
★★★ "cc" "dd" + insert mode
★★★★ "s" "x" + Insert mode
The c series commands work in delete + insert mode.
The "d + Insert" command is also acceptable, but it requires more input, so use this command if possible.
Other
★★★★ "%" Move to corresponding brackets
★★★★ "Ctrl-a" Move the cursor from the cursor to the nearest number to the right and add one to the number value.
★★★★ "Ctrl-x" Move the cursor from the cursor to the nearest number to the right and subtract 1 from the value of the number.
★★★ Add ">>" indent
★★★★ "★<<」インデントの削除
There are many more useful commands, but that is all for this issue.
I hope this will be helpful to those interested in vim.