簡體   English   中英

vimrc代碼在文件頂部插入當前文件的路徑?

[英]vimrc code to insert path of current file at top of file?

我想在該文件的頂部添加(或更新,如果存在)我正在處理的當前文件的路徑。

例如,如果我將File:放在要在vim(neovim)中編輯的文件的頂部附近,則我想用正在編輯的文件的路徑和文件名自動更新該行; 例如

File: /mnt/Vancouver/this_file.sh

如果有幫助,我的.vimrc文件中將包含以下內容,可在我.vimrc保存該緩沖區時自動在文件頂部附近的“ Last modified:行(如果存在)之后添加日期。 (光標位置也會通過keepjumps自動恢復。)

" http://vim.wikia.com/wiki/Insert_current_date_or_time 
" If buffer modified, update any 'Last modified: ' in the first 30 lines.
" 'Last modified: ' can have up to 10 characters before (they are retained).
" Restores cursor and window position using save_cursor variable.

function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([30, line("$")])
    keepjumps exe '1,' . n . 's/^\(^Last modified: \).*/\1' .
          \ strftime('%Y-%m-%d') . '/e'
    call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun
autocmd BufWritePre * call LastModified()

" TEST:
" Last updated: 
" (indented line below: should not update)
"  Last modified: 
" Last modified: 2018-11-21

如果文件的第一行以File:開頭,則以下函數將添加完整的文件路徑( %:pFile:

autocmd! insertleave * call PutPath()                                     
function! PutPath()                                                      
    let file=expand("%:p")                                               
    silent! execute '1s@^File:$@& '.file                                 
endfunction         

離開插入模式( autocmd insertleave )時,替換將自動執行,並且File:之后必須沒有尾隨空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM