[LINUX] vi (vim) command memo

1. Start

Start by specifying a file

vi <file name> 

Start by specifying multiple files

vi <File name 1> <File name 2> … <File name n>

Specify multiple files and start by splitting with tabs

vi -p <File name 1> <File name 2> … <File name n>

Start read-only

vi -R <file name> 
view <file name> 

Start with the initial cursor position on the last line

vi + <file name> 

Start with the initial cursor position as the specified line

vi +Number of lines<file name> 

Start with the initial cursor position as the line where the character string is first found

vi +/String<file name>

Start in recovery mode (start swap file)

vi -r <file name>

2. Move files when opening multiple files

command 短縮command Contents
:next :n Move to the next file
:next! :n! Forced to move to the next file
:wnext :wn Save the current file and move to the next file
:previous :prev Move to previous file
:previous! :prev! Forcibly move to the previous file
:wprevious :wp Save the current file and move to the previous file
:first :fir Move to the first file
:first! :fir! Forcibly move to the first file
:last :la Move to the last file
:last! :la! Forcibly move to the last file
:rew - Move to the first file
:rew! - Forcibly move to the first file
:rewind - Save the current file and move to the first file
:rewind! - Move to the first file without saving the current file
:e# - Return to the last edited file
:arg - Check the file being edited and the list of files

3. Move cursor

Command 1 Command 2 Command 3 Contents
h [BackSpace] Move to the left[n valid]
j [Ctrl]+ n Move down[n valid]
k [Ctrl]+ p Move up
l [Space] Move to the right
[Numbers]h [Numbers]← [Numbers][BackSpace] Numbers分左へ移動
[Numbers]j [Numbers]↓ [Numbers][Ctrl]+ n Numbers分下へ移動
[Numbers]k [Numbers]↑ [Numbers][Ctrl]+ p Numbers分上へ移動
[Numbers]l [Numbers]→ [Numbers][Space] Numbers分右へ移動
command Contents
^ Move to the first character string with blanks at the beginning of the current line skipped
0 Move to the beginning of the current line
$ Move to the end of the current line
G Move to the last line of the file
gg Move to the first line of the file
+ Move to the first character string with blanks at the beginning of the next line skipped
- Move to the first character string with blanks at the beginning of the previous line skipped
[Enter] Move to the beginning of the next line
[Numbers]$ (Numbers-1)Move to the end of the line below the line
[Numbers]G (Numbers)Move to the beginning of the line
[Numbers]+ (Numbers)Move to the first character string with blanks below the line skipped
[Numbers]- (Numbers)Move to the first character string with blanks on the line skipped
[Numbers][Enter] (Numbers)Move to the beginning of the line below
:[Numbers] (Numbers)Go to line
command Contents
w Move to 1 word next
b Move one word forward
e Move to the end of a word (if it is already at the end of a word, move to the end of the next word)
[Numbers]w (Numbers)Move to next by word
[Numbers]b (Numbers)Move word forward
[Numbers]e (Numbers)word moves to the end of the next word
command Contents
( Move to the beginning of the sentence (block unit)
) Move to the beginning of the next sentence (block unit)
{ Move to previous paragraph
} Move to next paragraph

4. Screen movement

command Contents
H Move the cursor to the top of the screen
M Move the cursor to the center of the screen
L Move the cursor to the last line of the screen
[Ctrl] + D 1 cursor/2 Move to the bottom of the screen
[Ctrl] + U 1 cursor/2 Move to the top of the screen
[Ctrl] + F Move the cursor down one screen
[Ctrl] + B Move the cursor up one screen
[Numbers]H Move the cursor from the top of the screen(Numbers)Move to line
[Numbers]L Move the cursor from the last line of the screen(Numbers)Move to line
[Numbers][Ctrl] + D Cursor(Numbers)Move down the line
[Numbers][Ctrl] + U Cursor(Numbers)Move up the line
[Numbers][Ctrl] + F Cursor(Numbers)Move to the bottom of the screen
[Numbers][Ctrl] + B Cursor(Numbers)Move to the screen
z[Enter] Scroll so that the current cursor position is at the top of the screen
zz Scroll so that the current cursor position is in the center of the screen

5. Move to INSERT input mode

command Contents
i Start typing from the left of the cursor
I Start typing from the beginning of the line
a Start typing from the right side of the cursor
A Start typing from the end of the line
o Insert a blank line below the cursor line and start typing from the beginning of that line
O Insert a blank line above the cursor line and start typing from the beginning of that line

6. Shift to REPLACE input mode

command Contents
R Start typing by overwriting from the cursor

7. Delete characters and switch to INSERT input mode

command Contents
s Delete one character at the cursor position and start inputting
S Delete the line at the cursor position and start typing
command Contents
cl Delete one character at the cursor position and start inputting
cc Delete the line at the cursor position and start typing
c^ Start input by deleting from just before the cursor position to the first character string with blanks at the beginning of the line skipped
c0 Delete from just before the cursor position to the beginning of the line and start inputting
c$ Delete from the cursor position to the end of the line and start inputting
cG Delete from the line at the cursor position to the end of the sentence and start inputting
cw Delete from the cursor position to just before the next word and start inputting
cb Delete from the cursor position to the beginning of the word and start inputting
ce Delete from the cursor position to the end of the word and start typing
cW Skip the delimiter from the cursor position, delete it until just before the next word, and start inputting

You can use the cursor movement command for the second character of the + command. (Other than the above, you can do various combinations.) Also, by entering a number at the beginning like 2cw, you can make the operation for the number like" Delete until just before the word two words ahead from the cursor position and start inputting ".

8. Move to command mode

command Contents
[Esc] Move to command mode

9. Delete

command Contents
x Delete one character at the cursor position
dl Delete one character at the cursor position
dd Delete the line at the cursor position
d^ Delete from just before the cursor position to the first character string with blanks at the beginning of the line skipped
d0 Delete from just before the cursor position to the beginning of the line
d$ Delete from the cursor position to the end of the line
dG Delete from the line at the cursor position to the end of the sentence
dw Delete from the cursor position to just before the next word
db Delete from the cursor position to the beginning of the word
de Delete from the cursor position to the end of the word
dW Skip the delimiter from the cursor position and delete it until just before the next word

You can use the cursor movement command for the second character of the + command. (Other than the above, you can do various combinations.) Also, by entering a number at the beginning like 2dw, you can make the operation for the number like" Delete from the cursor position to just before the word two words ahead ".

10. Copy

command Contents
yl Copy one character at the cursor position
yy Copy the line at the cursor position
y^ Copy from just before the cursor position to the first character string with blanks at the beginning of the line skipped
y0 Copy from just before the cursor position to the beginning of the line
y$ Copy from cursor position to end of line
yG Copy from the line at the cursor position to the end of the sentence
yw Copy from the cursor position to just before the next word
yb Copy from the cursor position to the beginning of the word
ye Copy from cursor position to end of word
yW Skip the delimiter from the cursor position and copy to just before the next word

You can use the cursor movement command for the second character of the + command. (Other than the above, you can do various combinations.) Also, by entering a number at the beginning like 2yw, you can make the operation for the number like" Copy from the cursor position to just before the word two words ahead ".

11. Paste

command Contents
p Paste the copied content after the cursor
P Paste the copied content in front of the cursor

12. undo and redo

command Contents
u undo (cancellation of previous editing work)
[Ctrl] + r redo (cancellation of undo)

13. Search

command Contents
/String+ [Enter] Search for a string behind the cursor
?String+ [Enter] Search for a string before the cursor
n Next search Move to the corresponding character string
N Previous search Move to the corresponding character string
/ + [Enter] Next search Move to the corresponding character string
? + [Enter] Previous search Move to the corresponding character string

14. Replacement

command Contents
r[letter] カーソルの1letterを入力したletterに置換
[Numbers]r カーソルからNumbers分の文字を入力した文字で置換
command Contents
:s/String 1/String 2/[Enter] カーソル行の最初の「String 1」のみを「String 2」に置換
:s/String 1/String 2/g[Enter] カーソル行の「String 1」を全て「String 2」に置換
:s/String 1/String 2/c[Enter] カーソル行の最初の「String 1」を「String 2」に置換(確認有り)
:[Number of lines]s/String 1/String 2/[Enter] 指定行で最初の「String 1」のみを「String 2」に置換
:[Number of lines]s/String 1/String 2/g[Enter] 指定行で「String 1」を全て「String 2」に置換
:%s/String 1/String 2/g[Enter] 全ての行で「String 1」を全て「String 2」に置換
:[Start line],[End line]s/String 1/String 2/[Enter] 指定範囲行内で各行の最初の「String 1」を「String 2」に置換
:[Start line],[End line]s/String 1/String 2/g[Enter] 指定範囲行内の「String 1」を全て「String 2」に置換
:[Start line],$s/String 1/String 2/g[Enter] 指定Start lineから最終行までの「String 1」を全て「String 2」に置換
& Repeat the last s command
:&[Enter] Repeat the last s command
command Contents
~ Convert cursor characters from lowercase to uppercase and from uppercase to lowercase
[Numbers]~ Converts the specified number of characters from the cursor from lowercase to uppercase and from uppercase to lowercase
command Contents
J Replace line breaks in the current line and the line immediately below with spaces
[Numbers]J Replace line breaks in lines below the current line by the specified number of lines with spaces

15. File operations

command Contents
:w[Enter] overwrite save
:w![Enter] Forced overwrite save
:w file name[Enter] Save as file name
:w!file name[Enter] Force save with file name
:[Start line],[End line] w[Enter] Start lineからEnd lineまでを保存
:wq[Enter] Overwrite and save
:q[Enter] End
:q![Enter] Exit without saving
:e[Enter] Reload the currently open file
:e![Enter] Forces the currently open file to be reloaded
:e file name[Enter] Read the file for editing
:e!file name[Enter] Forcibly read the file for editing
command Contents
:e ++enc=euc-jp[Enter] Character code euc-Set to jp
:e ++enc=shift_jis[Enter] Shift character code_Set to jis
:e ++enc=utf-8[Enter] Character code utf-Set to 8
command Contents
:e ++ff=dos[Enter] Character code dos (CR+LF)
:e ++ff=mac[Enter] Set the character code to mac (CR)
:e ++ff=unix[Enter] Set character code to unix (LF)

16. set command

command abridgement Contents
:set number[Enter] :set nu[Enter] Show line number
:set nonumber[Enter] :set nonu[Enter] Hide line numbers
:set all[Enter] - Show all options
:set fenc?[Enter] - Check file encoding
:set ff?[Enter] - Check line feed code

17. Insert the contents of the file

command Contents
:r[Enter] Insert the contents of the current file directly under the cursor line
:r file name[Enter] Insert the contents of the file directly under the cursor line
:r!command[Enter] Insert the result of a shell command directly under the cursor line

that's all

Recommended Posts

vi (vim) command memo
vim command
command memo
Command memo
scp command memo
who command memo
Linux # Command Memo 1
memorandum of vi command
Basic Python command memo
virtualenv Basic command usage memo
Linux tar xz command memo
[Learning memo] Django command summary
Introduction to vi command (memorandum)
Vim
[For memo] Linux Part 1 vi editor
[Personal memo] Python virtual environment command memo
vim
Linux command [ldconfig] LPIC learning memo
Vim + Python development environment setting memo
find / grep command (beginner's personal memo)
Linux command (basic in basic) personal memo