Open3

Vim のタブ関連マッピング

AlisueAlisue

Vim/Neovim はタブ関連のマッピングをデフォルトではほぼ提供していない(gt, gT くらいか?)ので、使いこなすためには自分でマッピングする必要がある。

以下は整合性、使い勝手、潰してしまうデフォルトマッピングとのバランスを考えた結論。

実際利用するかは微妙だが <C-w>{数字} で特定のタブに一発でジャンプできるようにもしている(silent! は対象のタブが存在しない場合にエラーにしないために必須)

<C-w><C-n><C-w><C-p> によるタブ切り替えは H/LとPageUp/PageDownを共存させる設定 (submode編) にて紹介されている技を利用してサブモード化しているため <C-w><C-n><C-n><C-n> のように連続切り替え可能。<C-w>nnn のような Ctrl 無しでの連続切り替えは認めていないが、これはタブ切り替え直後に np を即使いたいケースがあるため。

" https://blog.atusy.net/2024/05/29/vim-hl-enhanced/
nnoremap <Plug>(tabmode) <Nop>
nnoremap <silent> <Plug>(tabmode)<C-n> <Cmd>tabnext<CR><Plug>(tabmode)
nnoremap <silent> <Plug>(tabmode)<C-p> <Cmd>tabprevious<CR><Plug>(tabmode)

nnoremap <silent> <C-w>n <Cmd>tabnext<CR>
nnoremap <silent> <C-w>p <Cmd>tabprevious<CR>
nnoremap <silent> <C-w>t <Cmd>tabnew<CR>
nnoremap <silent> <C-w>d <Cmd>tabclose<CR>
nnoremap <silent> <C-w>1 <Cmd>silent! tabnext 1<CR>
nnoremap <silent> <C-w>2 <Cmd>silent! tabnext 2<CR>
nnoremap <silent> <C-w>3 <Cmd>silent! tabnext 3<CR>
nnoremap <silent> <C-w>4 <Cmd>silent! tabnext 4<CR>
nnoremap <silent> <C-w>5 <Cmd>silent! tabnext 5<CR>
nnoremap <silent> <C-w>6 <Cmd>silent! tabnext 6<CR>
nnoremap <silent> <C-w>7 <Cmd>silent! tabnext 7<CR>
nnoremap <silent> <C-w>8 <Cmd>silent! tabnext 8<CR>
nnoremap <silent> <C-w>9 <Cmd>silent! tabnext 9<CR>
nnoremap <silent> <C-w>0 <Cmd>silent! tabnext 0<CR>

" Ctrl に指を置いたままでも同じ効果を得るために <C-w><C-X> にもマッピング
nnoremap <silent> <C-w><C-n> <Cmd>tabnext<CR><Plug>(tabmode)
nnoremap <silent> <C-w><C-p> <Cmd>tabprevious<CR><Plug>(tabmode)
nnoremap <silent> <C-w><C-t> <Cmd>tabnew<CR>
nnoremap <silent> <C-w><C-d> <Cmd>tabclose<CR>
nnoremap <silent> <C-w><C-1> <Cmd>silent! tabnext 1<CR>
nnoremap <silent> <C-w><C-2> <Cmd>silent! tabnext 2<CR>
nnoremap <silent> <C-w><C-3> <Cmd>silent! tabnext 3<CR>
nnoremap <silent> <C-w><C-4> <Cmd>silent! tabnext 4<CR>
nnoremap <silent> <C-w><C-5> <Cmd>silent! tabnext 5<CR>
nnoremap <silent> <C-w><C-6> <Cmd>silent! tabnext 6<CR>
nnoremap <silent> <C-w><C-7> <Cmd>silent! tabnext 7<CR>
nnoremap <silent> <C-w><C-8> <Cmd>silent! tabnext 8<CR>
nnoremap <silent> <C-w><C-9> <Cmd>silent! tabnext 9<CR>
nnoremap <silent> <C-w><C-0> <Cmd>silent! tabnext 0<CR>
AlisueAlisue

上記で上書きしているのは、以下のマッピング

CTRL-W n

CTRL-W n						*CTRL-W_n*
CTRL-W CTRL-N						*CTRL-W_CTRL-N*
:[N]new [++opt] [+cmd]					*:new*
		Create a new window and start editing an empty file in it.
		Make new window N high (default is to use half the existing
		height).  Reduces the current window height to create room (and
		others, if the 'equalalways' option is set and 'eadirection'
		isn't "hor").
		Also see |++opt| and |+cmd|.
		If 'fileformats' is not empty, the first format given will be
		used for the new buffer.  If 'fileformats' is empty, the
		'fileformat' of the current buffer is used.  This can be
		overridden with the |++opt| argument.
		Autocommands are executed in this order:
		1. WinLeave for the current window
		2. WinEnter for the new window
		3. BufLeave for the current buffer
		4. BufEnter for the new buffer
		This behaves like a ":split" first, and then an ":enew"
		command.

新しいウィンドウを縦分割で開くマッピングだが、縦分割したい場合は <C-w>s で既存バッファを分割すれば良く、明確に「新しいバッファ」が欲しいケースは頻度的にマッピングするほどではない(:new を叩いても良い)ので、潰しても問題が少ない。

CTRL-W p

CTRL-W p					*CTRL-W_p* *CTRL-W_CTRL-P*
CTRL-W CTRL-P	Go to previous (last accessed) window.

以前のウィンドウに戻るマッピングだが 、自分は<C-w>h,j,k,l でウィンドウ移動することが多いため利用しない(体に染み付いていない)。vim-choosewin などを日常的に利用している場合は相対価値が上るかも。その場合は <C-w>P (プレビューウィンドウに移動)が代替か(近年プレビューウィンドウを利用することは稀なので)。

CTRL-W t

CTRL-W t					*CTRL-W_t* *CTRL-W_CTRL-T*
CTRL-W CTRL-T	Move cursor to top-left window.

画面分割をしている際に、必ず左上のウィンドウに移動するマッピング。他のバリアントとして <C-w>b(必ず右下のウィンドウに移動)がある。今回初めて知ったレベルで、利用することはまず無いという結論。

CTRL-W d

CTRL-W CTRL-D					*CTRL-W_CTRL-D* *CTRL-W_d*
CTRL-W d		Open a new window, with the cursor on the first
			macro definition line that contains the keyword
			under the cursor.  The search starts from the
			beginning of the file.  If a count is given, the
			count'th matching line is jumped to.

マクロ???の定義ジャンプ???おそらく LSP の gd に似たマッピングだろうが、使ったこともないし、使うことも無さそう。

AlisueAlisue

なお、以前は <C-n><C-p>gtgT を当てていたが、これらのマッピングは Fuzzy Finder のようなリスト選択インターフェースで上下移動する際によく利用するため <C-w>n<C-w>p に変更した。普通にやると連打できなかったが、疑似サブモードを利用することで連打ができるようになったので <C-n><C-p の頃と変らぬ使い勝手になり、とてもよい。