[英]vimrc code to insert path of current file at top of file?
I want to add (or update, if present) the path to the current file I am working on, at the top of that file. 我想在该文件的顶部添加(或更新,如果存在)我正在处理的当前文件的路径。
For example, if I place File:
near the top of the file I that I am editing in vim (neovim), I want to automatically update that line with the path and filename of the file that I am editing; 例如,如果我将File:
放在要在vim(neovim)中编辑的文件的顶部附近,则我想用正在编辑的文件的路径和文件名自动更新该行; eg 例如
File: /mnt/Vancouver/this_file.sh
If it helps, I have the following in my .vimrc
file that automatically adds the date after the Last modified:
line (if present) near the top of my file, anytime I save that buffer. 如果有帮助,我的.vimrc
文件中将包含以下内容,可在我.vimrc
保存该缓冲区时自动在文件顶部附近的“ Last modified:
行(如果存在)之后添加日期。 (The cursor position is also automatically restored, via keepjumps
.) (光标位置也会通过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
The following function adds the full file path ( %:p
) if the first line of the file starts with File:
如果文件的第一行以File:
开头,则以下函数将添加完整的文件路径( %:p
) File:
autocmd! insertleave * call PutPath()
function! PutPath()
let file=expand("%:p")
silent! execute '1s@^File:$@& '.file
endfunction
The substitution is performed automatically when leaving insert mode ( autocmd insertleave
) and there must be no trailing spaces after File:
. 离开插入模式( autocmd insertleave
)时,替换将自动执行,并且File:
之后必须没有尾随空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.