19
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Vimで快適なgit生活を送る

Last updated at Posted at 2022-12-04

はじめに

gitを使いソフトウェアを管理することは多いです。
今回はVimに依存することで、git生活をより快適に送るTipsを紹介します。

1. git commit実行時のテンプレートにprefixを追加する

commitメッセージを書く際にprefixを先頭に書くことは多いです。
よく使うprefixなら、commitメッセージを書くときに逐一確認できる環境を作りたいですよね。
gitコマンドを使うことで、git commitを実行した際に特定の文字列をテンプレートとして挿入できます。

コミットメッセージの書き方自体は、以下の記事を参考にしました。

https://qiita.com/konatsu_p/items/dfe199ebe3a7d2010b3e


# feat: A new feature
# fix: A bug fix
# docs: Documentation only changes
# style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
# refactor: A code change that neither fixes a bug nor adds a feature
# perf: A code change that improves performance
# test: Adding missing or correcting existing tests
# chore: Changes to the build process or auxiliary tools and libraries such as documentation generation

上記のテンプレートを以下のコマンドによって、git commit実行時に追加できます。

git config --global commit.template ~/.gitmessage

see also:

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

2. Vimでprefixを補完する環境を作成する

コミットメッセージを書くとき、先頭にprefixを書くことは少なくありません。
これらを毎回手動で入力するのは面倒ですし、ちょっとしたことでプラグインを入れるのも避けたいです。
そこで、Vimの標準機能だけで実現しましょう。

2-1. 辞書補完の環境を作成する

Vimには標準で補完機能が搭載されており、プラグインを入れずとも特定のタイミングで補完を使用できます。
今回は標準機能の一種である辞書補完を使い、commitメッセージを書くときのみprefixの補完を有効にします。

see also:

https://vim-jp.org/vimdoc-ja/options.html#'dictionary'

まず最初に以下のようなファイルを用意しましょう。ここではcommit-prefixとします。

$HOME/.vim/dict/commit-prefix
feat
fix
docs
style
perf
test
chore

次に、辞書補完のリストにcommit-prefixを追加します。
ここでは、git commit時のみに追加したいのでftplugin/gitcommit.vimに以下の設定を記述します。

NOTE: git commit時にVimが立ち上がった場合、filetypeはgitcommitになっています。

see also:
https://vim-jp.org/vimdoc-ja/usr_43.html#filetype-plugin

ftplugin/gitcommit.vim
setlocal dictionary=$HOME/.vim/dict/commit-prefix

また、筆者はgina.vimを使って、Vimからコミットメッセージを書きます。
そのため、以下のような設定も追加します。

ftplugin/gina-commit.vim
setlocal dictionary=$HOME/.vim/dict/commit-prefix

これで、prefixを辞書補完できるようになりました。

ちなみに、辞書補完の入力が大変に感じた人は、以下のブログを参考にすると良いと思います。

https://daisuzu.hatenablog.com/entry/2015/12/05/002129

3. git操作のキーストロークを激減させる

git操作をする際、

  1. git status
  2. git add
  3. git commit
  4. git push

とするのが基本の流れです。
しかし、上記の処理は繰り返すことが多いため、できるだけキーストロークは減らしたいです。
筆者は、gina.vimに依存することでキーストロークを減らしています。

3-1. 使わないキーをgit操作に割り当てる

筆者の場合、カーソル移動の際に矢印キーを使いません。そのため、←↓↑→の4つのキーが余っています。
これをそのままgit操作に割り当てます。

nnoremap <Up> :Gina status<CR>
nnoremap <Down> :Gina commit<CR>
nnoremap <Left> :Gina diff<CR>
nnoremap <Right> :Gina grep<CR>

これにより、よく繰り返す操作をたった1回のキー入力で達成できます。

see also:

https://twitter.com/get_me_power/status/1201516375103008769

最後に

興味のある方は是非試してみてください。

19
6
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
19
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?