2

~/.vimrc のみが含まれています

so ~/config/vim/vimrc

~/config/vim/vimrc には、通常のオプション、いくつかのマッピング、およびさまざまなファイルタイプのソースファイルが含まれています。

autocmd FileType cpp so ~/config/vim/filetype/cpp.vimrc

そして、そのファイルで、次の関数を定義しました。これは、二重の包含を避けるために、新しい cpp ヘッダーを開くたびに呼び出す必要があります。

python import vim

function! s:insert_gates()
python << endPython
hpp = vim.current.buffer.name
hpp = hpp[hpp.rfind('/') + 1:]
hpp = hpp.upper()
hpp = hpp.replace('.', '_')
vim.current.buffer.append("#ifndef " + hpp)
vim.current.buffer.append("# define " + hpp)
vim.current.buffer.append("")
vim.current.buffer.append("#endif")
endPython
endfunction

autocmd BufNewFile *.hpp call <SID>insert_gates()

そして、シェルに次のことを要求すると:

vim -O3 t1.hpp t2.hpp t3.hpp

私が得た:

|                     |#ifndef T2_HPP       |#ifndef T3_HPP       |
|                     |# define T2_HPP      |# define T3_HPP      |
|                     |                     |                     |
|                     |#endif               |#endif               |
|                     |                     |#ifndef T3_HPP       |
|                     |                     |# define T3_HPP      |
|                     |                     |                     |
|                     |                     |#endif               |
|                     |                     |                     |
|_____________________|_____________________|_____________________|
|t1.h                 |t2.h                 |t3.h                 |

それはまさに私が望んでいるものではありません.私の間違いがわかりますか?ありがとう。

4

1 に答える 1

2

hereで参照されているように、Vim は新しいファイルを開くたびに新しいファイルを作成します。 autocmdこれを防ぐには、そのセクションを次のものに置き換えます.vimrc

python import vim

function! s:insert_gates()
python << endPython
hpp = vim.current.buffer.name
hpp = hpp[hpp.rfind('/') + 1:]
hpp = hpp.upper()
hpp = hpp.replace('.', '_')
vim.current.buffer.append("#ifndef " + hpp)
vim.current.buffer.append("# define " + hpp)
vim.current.buffer.append("")
vim.current.buffer.append("#endif")
endPython
endfunction

augroup insertgates
    autocmd!
    autocmd BufNewFile *.hpp call <SID>insert_gates()
augroup END
于 2015-11-11T04:44:20.507 に答える