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; 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. (The cursor position is also automatically restored, via 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:
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:
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.