Vim でカーソル位置はそのままにスクリーンをスクロールする
Published Feb 5, 2025
⋅
Updated Mar 22, 2025
Vim には、カーソル位置はそのままにスクリーン(the edit window)をスクロールするコマンドがある。
文章を書いているときなどに、スクリーンの下端にカーソルが来たときに、入力しやすいように、カーソルがある行をスクリーンの中央に持ってくることができる。
10z<CR>
のように [count]
を渡して10行目をウィンドウのトップに持ってくることも可能 。
z<CR>
:[count]
で指定したライン(デフォルトでは cursor line)をウィンドウのトップにして再描画する。カーソルは、その行のはじめの non-blank に置かれる。zt
:z<CR>
と同様だが、カーソルは same column に置かれる。z.
:[count]
で指定したライン(デフォルトでは cursor line)をウィンドウのセンターにして再描画する。カーソルは non-blank に置かれる。。zz
:z.
と同様だが、カーソルは same column に置かれる。。z-
:[count]
で指定したライン(デフォルトでは cursor line)をウィンドウのボトムにして再描画する。カーソルは non-blank に置かれる。。zb
:z-
と同様だが、カーソルは same column に置かれる。。
以下は、vim :help
の scroll.txt
の Scrolling relative to cursor のページ。
==============================================================================
3. Scrolling relative to cursor *scroll-cursor*
The following commands reposition the edit window (the part of the buffer that
you see) while keeping the cursor on the same line. Note that the 'scrolloff'
option may cause context lines to show above and below the cursor.
*z<CR>*
z<CR> Redraw, line [count] at top of window (default
cursor line). Put cursor at first non-blank in the
line.
*zt*
zt Like "z<CR>", but leave the cursor in the same
column.
*zN<CR>*
z{height}<CR> Redraw, make window {height} lines tall. This is
useful to make the number of lines small when screen
updating is very slow. Cannot make the height more
than the physical screen height.
*z.*
z. Redraw, line [count] at center of window (default
cursor line). Put cursor at first non-blank in the
line.
*zz*
zz Like "z.", but leave the cursor in the same column.
Careful: If caps-lock is on, this command becomes
"ZZ": write buffer and exit!
*z-*
z- Redraw, line [count] at bottom of window (default
cursor line). Put cursor at first non-blank in the
line.
*zb*
zb Like "z-", but leave the cursor in the same column.
参考