SC100: Text Editing with Vim を受けるために Vim の勉強をしてみました。結果は 83/100 で合格。結構いろいろとコマンドを覚えましたがかなり基礎的な vim のコマンドに加え次のような項目のごく簡単な使い方が理解できていれば力業で解ける気がしました。
- Windows
- Visual Mode / Visual Block Mode
- vimdiff (vim -d or :diffsplit)
- vimrc (必要に応じて help ファイルを見ればよい?)
注意点としては :help コマンドが使えないこと。ディスク上 (/usr/share/vim/vim91/doc など) に help のファイルはあるのでわからない場合はそこを参照することになります。
以下は vim の勉強のために無駄に :help を写経した残骸です。SC100 対策としてはここまで覚える必要はありませんでした。。
カーソルを移動する (Normal Mode)
key | action |
---|---|
h | [count] characters to the left, e.g. 2h |
j | [count] lines downward, e.g. 10j, gj (downward on the screen) |
k | [count] lines upward , e.g. 5j, gk (upword on the screen) |
l | [count] characters to the right, e.g. 6j |
0 | To the first character of the line. |
^ | Go to first non-blank character of the line ` |
f{char} | (forward search) To [count]'th occurrence of {char} to the right, e.g. fT ; Repeat latest f, t, F or T [count] times , Repeat latest f, t, F or T in opposite direction [count] times. |
F{char} | To [count]'th occurrence of {char} to the left, e.g. fT ; Repeat latest f, t, F or T [count] times , Repeat latest f, t, F or T in opposite direction [count] times. |
t{char} | (till search) Till before [count]'th occurrence of {char} to the right., e.g. tF ; Repeat latest f, t, F or T [count] times , Repeat latest f, t, F or T in opposite direction [count] times. |
T{char} | Till before [count]'th occurrence of {char} to the left., e.g. Tb ; Repeat latest f, t, F or T [count] times , Repeat latest f, t, F or T in opposite direction [count] times. |
; | Repeat latest f, t, F or T [count] times. |
, | Repeat latest f, t, F or T in opposite direction [count] times. |
:{number} | Go to an absolute line number, e.g. :20 |
[count]gg | Goto line [count], default first line, on the first non-blank character |
G | Go to the last line |
cw | Change Word, e.g c2w |
Ctrl-o | Go to [count] Older cursor position in jump list |
Ctrl-i | Go to [count] newer cursor position in jump list |
Ctrl-] | Jump to the definition of the keyword under the cursor. |
:tag {name} | Jump to the definition of {name}, using the information in the tags file(s). |
:ju[mps] | Print the jump list (not a motion command). |
z | redraw, cursor line to top of window, cursor on first non-blank |
Delete/Yank/Put (Cut/Copy/Paste)
key | action |
---|---|
i | Insert text before the cursor [count] times. |
J | Join lines after a space, e.g. 3J |
gJ | Join lines without a space |
>> | Shift [count] lines one 'shiftwidth' rightwards. |
<< | Shift [count] lines one 'shiftwidth' leftwards. |
["x]x | Delete [count] characters under and after the cursor. = dl |
["x]X | Delete [count] characters before the cursor [into register x] (not |
["x]d{motion} | Delete text that {motion} moves over [into registerx]. |
["x]dd | Delete [count] lines [into register x] |linewise|. |
["x]D | Delete the characters under the cursor until the end of the line and [count]-1 more lines [into register x]; synonym for "d$". |
["x]y{motion} | Delete text that {motion} moves over [into registerx]. |
["x]yy or Y | Yank [count] lines [into register x] |linewise|. |
["x]c{motion} | Delete {motion} text [into register x] and start insert. |
["x]cc | Delete [count] lines [into register x] and start insert |
["x]C | Delete from the cursor position to the end of the line and [count]-1 more lines [into register x], and start insert. Synonym for c$ (not |linewise|). |
~ | Switch case of the character under the cursor and move the cursor to the right. |
g~{motion} | Switch case of {motion} text. |
[x][x] | Apply the command linewise. e.g. cc, dd, yy, g~~, guu, gUU |
:[range]s/{pattern}/{string}/[flags] | For each line in [range] replace a match of {pattern} with {string}. e.g :s/net/org/g, s#/var/spool#var/local#g, :/Global/,/Local/s/net/org/g, :%s/net/org/g |
gu{motion} | Make {motion} text lowercase. |
gU{motion} | Make {motion} text uppercase. |
{operator}{a}{object} | e.g. daw = delete a word, da{, dap, ya", das |
{operator}{i}{object} | e.g. yiw = yank inner word, yit = delete inner tab '<>' '<>' block, dit = , diB = delete inner '{' '}' block |
Registers
key | action |
---|---|
:reg | Display the type and contents of all numbered and named registers. |
:reg {arg} | Display the contents of the numbered and named registers that are mentioned in {arg}. |
"" | Unnamed register |
"0 to "9 | Numbered registers. Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with ["x]. Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). |
"a to "z or "A to "Z | Named registers. Specify them as lowercase letters to replace their previous contents or as uppercase letters to append to their previous contents. |
"_ | Blackhole register. When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned. |
:help change | Show change.txt |
文字を検索する
key | action |
---|---|
/{pattern} | Search forward for the [count]'th occurrence of {pattern} n - Repeat the latest "/" or "?" [count] times N - Repeat the latest "/" or "?" [count] times in opposite direction. e.g. /find, 2/word |
?{pattern} | Reverse search n - Repeat the latest "/" or "?" [count] times N - Repeat the latest "/" or "?" [count] times in opposite direction. |
* | Search forward for the [count]'th occurrence of the word nearest to the cursor. |
# | Reverse serach for word |
Visual Mode / Line Visual Mode / Visual Block mode
key | action |
---|---|
v | start characterwise visual mode |
V | start linewise visual mode |
Ctrl-Q or Ctrl-V | Start Visual mode blockwise. |
gv | Start Visual mode with the same area as the previous area and the same mode. |
Key binds in visual mode
key | action |
---|---|
ap | select paragraph |
ip | select while paragarph |
o O | move highlighted area |
~ U u | switch case |
w W | select word |
h j k l | select characters |
w W | select characters |
J gJ | Join lines after or without a space |
c cw | Change character or word |
> | Shift [count] lines one 'shiftwidth' rightwards. |
< | Shift [count] lines one 'shiftwidth' leftwards. |
: | Run commnad e.g. :'<,'>s/stringA/stringB/ , :'<,'>[center |
Visual Block Mode
key | action |
---|---|
Ctrl-q or Ctrl-c | Start Visual mode blockwise |
gv | Start Visual mode with the same area as the previous area and the same mode. |
Key binds in visual block mode
key | action |
---|---|
Shift-I | Add words at the beggning of lines |
Shift-A | Add words at the end of lines |
vap | select paragraph |
o O | move highlighted area |
~ U u | switch case |
Buffers/Windows/Tabs
Buffers
key | action |
---|---|
vim file1 file2 file3 | open file1, file2, and file3 with buffers |
:buffers or :ls | Show all buffers. |
:help ls | Show buffer indicator |
:buffer or :b [N] | Edit buffer [N] from the buffer list. Ctrl-d list buffers Tab Tab Completion :b text1.txt, :bnext, :bn, :bprevious, :blast, :bl |
:[N]bn :[N]bp | Go to [N]th netxt/previous buffer in buffer list |
Ctrl-^ | Edit previous buffer |
:bfirst or :bf | Go to first buffer in buffer list. |
:blast or :bl | Go to last buffer in buffer list. |
:badd {fname} | Add file name {fname} to the buffer list, without loading it, if it wasn't listed yet. |
:e {fname} :E | open {fname} or a file using explore in a new buffer |
:bdelete[!][N] or :bd[!][N] | Unload buffer [N] (default: current buffer) and delete it from the buffer list. If the buffer was changed, this fails, unless when [!] is specified, in which case changes are lost. |
:bufdo {cmd} | Execute {cmd} in each buffer in the buffer list or if [range] is given only for buffers for which their buffer number is in the [range]. |
:set hidden | When on a buffer becomes hidden when it is |
:h buffers | buffers help |
Windows
key | action |
---|---|
:sp [file] or Ctrl-W s | Split current window in two. |
:vp [file] or Ctrl-W v | Like |
CTRL-W h | Move cursor to Nth window right current one. |
CTRL-W j | Move cursor to Nth window below current one. |
CTRL-W k | Move cursor to Nth window above current one. |
CTRL-W l | Move cursor to Nth window left current one. |
Ctrl-W q | Close current window |
Ctrl-W Ctrl-W | Move cursor to window below/right of the current one. |
[N]Ctrl-W + or :reseze [N] | Increase current window height by N (default 1). |
[N}Ctrl-W - or :reseze [-N] | Decrease current window height by N (default 1). |
[N]Ctrl-W < or :vertical reseze [N] | Increase current window width by N (default 1). |
[N]Ctrl-W > or :vertical resize [-N] | Increase current window width by N (default 1). |
[N]Ctrl-W o or :only | Make the current window the only one on the screen. |
:ba or :ball | Rearrange the screen to open one window for each buffer in the buffer list. |
:windo {cmd} | Execute {cmd} in each window. |
:h windows | windows help |
all buffer cmd | Same with buffer commands |
Tabs
key | action |
---|---|
vim -p file1 file2 file3 | open file1, file2, and file3 with tabs |
:tabdo {cmd} | Execute {cmd} in each tab page or if [range] is given only in tab pages which tab page number is in the [range]. |
:h tabs | tabs help |
Commands
key | action |
---|---|
:help or :h {subject} | Open a window and display the help file in read-only mode. |
:help CTRL-V-alternative | Find an alternative way to do Ctrl-v |
:5/{string1}/{string2}/ | Replace (substract) "string1" to "string2" in in line 5 |
:1,5/{string1}/{string2}/ | Replace string1 by string2 to between line 1 and line 5 |
%s/{string1}/{string2}/ | Replace string1 by string2 to entire lines |
/{begin}/, /{end}/s/{string1}/{string2}/ | |
Ctrl-G or :file | Prints the current file name (as typed, unless ":cd" was used), the cursor position (unless the 'ruler' option is set), and the file status (readonly, modified, read errors, new file). |
:set {cmd}! | Disable the command |
:set {cmd}? | Show the command status |
:set ruler | |
:set hlsearch or :set hls | |
:nohls | Stop the highlighting for the 'hlsearch' option. It is automatically turned back on when using a search command, or setting the 'hlsearch' option. |
:incserach or :is | While typing a search command, show where the pattern, as it was typed so far, matches. |
. | Repeat last change, with count replaced with [count]. Does not repeat a command-line command. |