LoginSignup
0
0

VSCodeVimで末尾にセミコロンをいれるショートカットを作りたい!

Posted at

セミコロン 末尾に打つの 面倒くさい (五七五)

C++Javaを書くとき, セミコロンいれるの面倒くさいですよね.今回はVSCodeの拡張機能のVimで簡単に末尾にセミコロンをいれるショートカットをキーバインディングを使って設定してみました.

以下はMacでの設定です.その他のOSは適宜キーなどを読み替えてください.

手順

どのキーを割り当てるか決める

  • Shift + Enter
  • Ctrl + Enter
  • Ctrl + ;
    など.

setting.jsonに記述

keybindings.jsonを開き,設定を追加します.

開き方
  1. 左下の設定マークをクリックする
  2. キーボードショートカット(Keyboard Shortcuts)をクリックする.
  3. 右上の紙がクルッと回っているようなアイコンをクリックする.

Shift + Enterの場合

keybindings.json
 // (上に続く)

{
  "key": "shift+enter",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Normal'",
  "args": {
    "after": ["A", ";", "<Esc>"]
  }
},
{
  "key": "shift+enter",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Insert'",
  "args": {
    "after": ["<Esc>", "A", ";", "<Esc>"]
  }
},

// (下に続く)

Ctrl + Enterの場合

keybindings.json
 // (上に続く)

{
  "key": "ctrl+enter",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Normal'",
  "args": {
    "after": ["A", ";", "<Esc>"]
  }
},
{
  "key": "ctrl+enter",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Insert'",
  "args": {
    "after": ["<Esc>", "A", ";", "<Esc>"]
  }
},

// (下に続く)

Ctrl + ;の場合

keybindings.json
 // (上に続く)

{
  "key": "ctrl+;",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Normal'",
  "args": {
    "after": ["A", ";", "<Esc>"]
  }
},
{
  "key": "ctrl+;",
  "command": "vim.remap",
  "when": "inputFocus && vim.mode == 'Insert'",
  "args": {
    "after": ["<Esc>", "A", ";", "<Esc>"]
  }
},

// (下に続く)

その他のカスタマイズ

セミコロン挿入後に改行を加えてノーマルモードに移動したい場合

"args""after"のリストに"<Enter>"を追加.

セミコロン挿入後に改行+インサートモードに移動したい場合

"args""after"のリストに"o"を追加.

colonizeとの競合を防ぐ

以下を追加してください(Shift+Enterをcolonizeのキーとして設定している場合).

keybindings.json
{
    "key": "shift+enter",
    "when": "!vim.active",
    "command": "colonize.endline"
}

最後に

使いやすいようにカスタマイズしてみてください.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0