[LINUX] Vim Command Memorandum ~ Beginners will be able to do what they want! ~

table of contents

1. Move cursor (command mode) 2. Delete / Overwrite 3. Display adjustment 4. Edit (Command Mode / Insert Mode / Macro) 5. Replace / Search 6. Save / Exit 7. Specify range 8. VISUAL mode

1. Move cursor

--Move the cursor in command mode.

Move 1 character/line

Key What you want to do
j To the bottom line
   2j moves down 2 lines(Same for khl)
k To the top line
h To the left
l To the right

Movement of a unit

Key What you want to do
w To the next word
b To the previous word
$ To the end of the line
0 To the beginning of the line
G, :$<enter> Move to the end of the sentence
gg, :1<enter> Move to the beginning of the sentence
50G, 50gg, :50<enter> Move to line 50
ctrl + f to the next page(forward)
ctrl + b To the previous page(back)

2. Delete and overwrite

If you want to delete/replace characters one by one: X command/R command

Key What you want to do
x Delete the character on the cursor
X (shift + x) Delete to the left of the cursor
You can replace the current cursor position with the character to the right with xp(p is paste)
r "c" The character of the current cursor"c" (Any single letter)Replace with
ex) make->For modification to take, move the cursor to m and rt

If you want to delete in chunks: D command

Key What you want to do
dw Delete one word
3dw Delete 3 words
D (shift + d) Delete from the current position to the end of the line
d$Same meaning as
d0 Delete from the current position to the beginning of the line
dj Delete current line and lines below it
dk Delete current line and lines above it
j,k isMove cursorSame meaning as the item of
dd Delete current line(Including line breaks)
10dd Delete 10 lines from the current line
:10,20d<enter> Deleted lines 10 to 20
d/"hoge" From the current cursor"hoge"Delete up to the front of
"/"Means search.hogeIs
dt. Next from the current cursor position.Delete up to
(.Is OK with your favorite character)
t is next to the current line"."Point to the cursor position immediately before
das Delete the sentence where the cursor is currently placed
s represents sentense
It means that a is like all. That is, delete all sentence->Delete one sentence
dap Delete paragraph 1
p represents paragraph
di](or[) [...]Delete inside(Place the cursor in parentheses)
ci](or[) [...]After deleting the inside, go to insert mode

If you want to enter Inesrt mode after deletion: C command

Key What you want to do
c Enter insert mode after deletion.
You can use the command in the same way as d
For example ci[Then[]Delete the characters in and then enter insert mode
C Insert mode by deleting from the current position to the end of the line
(Corresponds to D deleting from the current position to the end of the line)
cc Delete the current line and go to Insert mode
(Corresponds to dd deleting the current line)
cw Delete the word on the current cursor and go to Insert mode
cD, c$ Insert mode by deleting the characters after the current cursor

Overwrite mode

Key What you want to do
R(shift + r) When you type a character, the character at the current cursor position is replaced with the character you typed.
<esc>Return to command mode with

3. Display adjustment

Key What you want to do
z<enter> Move the cursor to the top of the screen
zz Move the cursor to the center of the screen

4. Edit

Basic operation [Command mode]

Key What you want to do
. Repeat the last edit
u Undo last edit(Windows Ctrl+z)
Ctrl + r ReDo (Windows Ctrl+y)
p From the current cursorRight sidePaste to
P (shift + p) From the current cursorleftPaste to

Various ways to enter Insert mode (see also C command)

Key What you want to do
i Go to Insert mode at the current character position
I Move to the beginning of the current line and go to Insert mode
a Go one character forward to Insert mode
ex) $Use this when you want to insert at the end of a sentence after moving with
A Move to the end of the current line and go to Insert mode
o Change to Insert mode with a line break below the current line
5o "hoge" <esc> From the current line down"hoge"5 lines added
O (shift + o) Insert mode with a line break on one line
J (shift + j) Remove the line break at the end of the current line and insert one space.
In other words, add one space between the current line and the next line and combine them.
gJ Remove line breaks without inserting space

Macro (Recording & playback of complicated work)

Key What you want to do
qa Start recording macro in a register
(part a is b,c,d,...And any register name is OK)
q Stop recording macros
@a Play a register macro
:10,30 normal @a <enter> a macro for register 10-Applies to line 30
":10,30"Is 10-Means line 30

5. Find and replace

Search for and move characters in a line

Key What you want to do
f + c (One character) Next to the current linecMove to
;The next character in the line, ","To the previous character
t + c (One character) Next to the current linecofTo the frontMove
* Move to the position where the word on the current cursor appears next(Search by exact word match)

Search and replace the entire sentence (search range can be narrowed down)

Key What you want to do
:%s/hoge/fuga/g The whole sentencehogeTofugaConversion to.
Multiple times in one linehogeReplace even if
(/First in line without ghogeOnly replace)
:1,5s/hoge/fuga/ 1-Up to the 5th linehogeBy replacing. In addition, it should be noted%Is 1,$Represents
:/cat/,/dog/s/hoge/fuga/g catWhendogOf the line betweenhogeTofugaReplace with
:/cat/,$s/hoge/fuga/g catAfter thathogeTofugaReplace with

--Use backslashes (\) to escape special characters (/ &!. ^ * $ ?). --For example, if you want to replace "INCL \ hoge.txt" with fuga.md, enter:% s/INCL \\ hoge/fuga \ .md/g and enter -[Supplement] \ is the same as
--For the selection range, refer to Specify Range (@Link in Page).

6. Save and exit

Key What you want to do
:wq Save and exit vim
:q! Exit without saving. Ignore changes.
:w Save(vim does not quit)

7. Range specification

-Used in combination with another command such as:% s

Key What you want to do
:10,20 10-Up to line 20
:% Whole sentence
:.,$ From the current position to the end of the sentence
.Is the line at the current cursor position,$Means the end of the sentence

8. VISUAL mode

basic operation

Key What you want to do
v Visual mode(Select by character)
Shift + v Visual Line mode(Select by line)
ctrl + v Visual Block mode(Select with rectangle)
gv Reproduce the previous selection

Add comment out to multiple lines (insert characters at the beginning of the sentence)

  1. Select the range you want to comment out with shift + v (or ctrl + v)
  2. Add $ at the beginning with I \ $ \ (I: shift + i)

Add sentences at the end of multiple lines

  1. Use ctrl + v to select the beginning of the range to which you want to add a sentence
  2. Move to the end of multiple lines selected with \ $
  3. Insert * hogehoge * at the end of the sentence with A (shift + a) * hogehoge *

Recommended Posts

Vim Command Memorandum ~ Beginners will be able to do what they want! ~
Everything for beginners to be able to do machine learning
[For IT beginners] What to do when the rev command cannot be used with Git Bash
Let's summarize what you want to do.
[For beginners] What to do after installing Anaconda
What to do if pip cannot be installed
What to do when is not in the sudoers file.This incident will be reported.
Links to do what you want with Sublime Text
The programming language you want to be able to use
I want to be able to analyze data with Python (Part 3)
[AWS] What to do when you want to pip with Lambda
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to be able to run Python in VS Code
I want you to be aware of what you will develop together from next year. (From "Readable Code")
[Learning memorandum] Introduction to vim
Linux command memorandum [for beginners]
Introduction to vi command (memorandum)
[AWS] What to do when the ping command causes a "timeout"
What to do when a video cannot be read by cv2.VideoCapture
What to do if PyAudio cannot be installed on Python 3.7, 3.8, 3.9 on Windows