vimにコメントの先頭にスペースを自動的に追加させる方法はありますか?

vimにコメントの先頭にスペースを自動的に追加させる方法はありますか?

Pythonを学ぶために.vimrcを設定しています。これまで私は以下を持っています:

" configure expansion of tabs for .py files
au BufRead,BufNewFile *.py set expandtab

set expandtab       " Use spaces instead of TAB
set tabstop=2       " One TAB equals 2 spaces
set softtabstop=2
set shiftwidth=2    " Spaces to use for autoindent
set autoindent      " Copy indent from current line on new line   
set ruler       " show line and column number
syntax on       " syntax highlighting
set smartindent

" keep indentation on comments (#)
" http://vim.wikia.com/wiki/Restoring_indent_after_typing_hash
:inoremap # X<BS>#

私の問題は - 行を始めるたびにコメント#と見栄えをするために常にハッシュの後にスペースを追加することです。vimそのスペースを自動的に挿入する必要がある方法はありますか?理想的ですが、必ずしも行の先頭にある必要はありません。

ベストアンサー1

次の行を追加します。~/.vim/ftplugin/python.vim:

inoremap # #<space>

または、次の行を追加してvimrcファイルにこの設定を追加することもできます。

autocmd BufRead,BufNewFile *.py inoremap # #<space>

おすすめ記事