www.nixre.net/blog/vim-update-lastmod/index.html
c47 f5f6231a6f
Change primary color of the light theme
Contrast was too high, so I added an additional stylesheet to override
then css variable.
2024-03-03 11:18:33 +01:00

310 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en" dir="auto">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="robots" content="index, follow">
<title>Update Lastmod Using vim | nixre.net</title>
<meta name="keywords" content="editor, text, vim">
<meta name="description" content="I think it is very important to add date information about articles creation time and modification time. There are various ways to do this. I usually prefer when this be done automatically by my favorite editor and so I wrote a short function:
function! LastMod() if expand(&#39;%:p:h&#39;) =~ &#39;path/to/my/hugo-website/www.nixre.net/content&#39; let l:cursorpos = winsaveview() let l:timestamp = strftime(&#39;%Y-%m-%dT%H:%M:%S&#43;02:00&#39;) silent! exe &#39;:%s/^\(lastmod:\).*$/\1 &#39; . expand(l:timestamp) . &#39;/&#39; call winrestview(l:cursorpos) endif endfunction The markdown sources are inside the content folder of my hugo website and the expand(&#39;%:p:h&#39;) of the file that is currently loaded into the buffer will show the absolute path to the directory that contains the file.">
<meta name="author" content="c47">
<link rel="canonical" href="https://www.nixre.net/blog/vim-update-lastmod/">
<link rel="stylesheet" type="text/css" href="/fonts/Fork-Awesome/css/fork-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/style.css">
<link crossorigin="anonymous" href="/assets/css/stylesheet.745d5d8db92185a9f0a7d25ab7baabd4cee0fee5c68f6b521b7383175df39d84.css" integrity="sha256-dF1djbkhhanwp9Jat7qr1M7g/uXGj2tSG3ODF13znYQ=" rel="preload stylesheet" as="style">
<link rel="icon" href="https://www.nixre.net/favicon.ico">
<link rel="icon" type="image/png" sizes="16x16" href="https://www.nixre.net/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="https://www.nixre.net/favicon-32x32.png">
<link rel="apple-touch-icon" href="https://www.nixre.net/apple-touch-icon.png">
<link rel="mask-icon" href="https://www.nixre.net/safari-pinned-tab.svg">
<meta name="theme-color" content="#2e2e33">
<meta name="msapplication-TileColor" content="#2e2e33">
<noscript>
<style>
#theme-toggle,
.top-link {
display: none;
}
</style>
</noscript><meta property="og:title" content="Update Lastmod Using vim" />
<meta property="og:description" content="I think it is very important to add date information about articles creation time and modification time. There are various ways to do this. I usually prefer when this be done automatically by my favorite editor and so I wrote a short function:
function! LastMod() if expand(&#39;%:p:h&#39;) =~ &#39;path/to/my/hugo-website/www.nixre.net/content&#39; let l:cursorpos = winsaveview() let l:timestamp = strftime(&#39;%Y-%m-%dT%H:%M:%S&#43;02:00&#39;) silent! exe &#39;:%s/^\(lastmod:\).*$/\1 &#39; . expand(l:timestamp) . &#39;/&#39; call winrestview(l:cursorpos) endif endfunction The markdown sources are inside the content folder of my hugo website and the expand(&#39;%:p:h&#39;) of the file that is currently loaded into the buffer will show the absolute path to the directory that contains the file." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.nixre.net/blog/vim-update-lastmod/" /><meta property="article:section" content="blog" />
<meta property="article:published_time" content="2022-05-01T10:28:05+01:00" />
<meta property="article:modified_time" content="2022-05-29T07:37:05+01:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Update Lastmod Using vim"/>
<meta name="twitter:description" content="I think it is very important to add date information about articles creation time and modification time. There are various ways to do this. I usually prefer when this be done automatically by my favorite editor and so I wrote a short function:
function! LastMod() if expand(&#39;%:p:h&#39;) =~ &#39;path/to/my/hugo-website/www.nixre.net/content&#39; let l:cursorpos = winsaveview() let l:timestamp = strftime(&#39;%Y-%m-%dT%H:%M:%S&#43;02:00&#39;) silent! exe &#39;:%s/^\(lastmod:\).*$/\1 &#39; . expand(l:timestamp) . &#39;/&#39; call winrestview(l:cursorpos) endif endfunction The markdown sources are inside the content folder of my hugo website and the expand(&#39;%:p:h&#39;) of the file that is currently loaded into the buffer will show the absolute path to the directory that contains the file."/>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1 ,
"name": "Blog",
"item": "https://www.nixre.net/blog/"
},
{
"@type": "ListItem",
"position": 2 ,
"name": "Update Lastmod Using vim",
"item": "https://www.nixre.net/blog/vim-update-lastmod/"
}
]
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"headline": "Update Lastmod Using vim",
"name": "Update Lastmod Using vim",
"description": "I think it is very important to add date information about articles creation time and modification time. There are various ways to do this. I usually prefer when this be done automatically by my favorite editor and so I wrote a short function:\nfunction! LastMod() if expand(\u0026#39;%:p:h\u0026#39;) =~ \u0026#39;path/to/my/hugo-website/www.nixre.net/content\u0026#39; let l:cursorpos = winsaveview() let l:timestamp = strftime(\u0026#39;%Y-%m-%dT%H:%M:%S+02:00\u0026#39;) silent! exe \u0026#39;:%s/^\\(lastmod:\\).*$/\\1 \u0026#39; . expand(l:timestamp) . \u0026#39;/\u0026#39; call winrestview(l:cursorpos) endif endfunction The markdown sources are inside the content folder of my hugo website and the expand('%:p:h') of the file that is currently loaded into the buffer will show the absolute path to the directory that contains the file.",
"keywords": [
"editor", "text", "vim"
],
"articleBody": "I think it is very important to add date information about articles creation time and modification time. There are various ways to do this. I usually prefer when this be done automatically by my favorite editor and so I wrote a short function:\nfunction! LastMod() if expand('%:p:h') =~ 'path/to/my/hugo-website/www.nixre.net/content' let l:cursorpos = winsaveview() let l:timestamp = strftime('%Y-%m-%dT%H:%M:%S+02:00') silent! exe ':%s/^\\(lastmod:\\).*$/\\1 ' . expand(l:timestamp) . '/' call winrestview(l:cursorpos) endif endfunction The markdown sources are inside the content folder of my hugo website and the expand('%:p:h') of the file that is currently loaded into the buffer will show the absolute path to the directory that contains the file. Vim will check if the string afterwards path/to/my/[..]/content is part of path to the currently loaded file. If so then first it saves the properties of the current window, like cursorposition and so. Then it creates a local variable l:timestamp with the current timestamp, searches the line that begins with lastmod: and updates this line with the current timestamp in l:timestamp. As last step it resets the window that was saved into the l:cursorpos variable in the first step.\nNow to make vim execute this function I added an autocommand group which calls the above function:\naugroup www.nixre.net au! au BufWrite /full/path/to/my/hug-website/www.nixre.net/content/**.md call LastMod() augroup end This tells vim to call the function if I save a file that has the .md extension in the given path. This way vim always updates the timestamp string in the frontmatter field lastmod: when I save the buffers content to the file.\n",
"wordCount" : "255",
"inLanguage": "en",
"datePublished": "2022-05-01T10:28:05+01:00",
"dateModified": "2022-05-29T07:37:05+01:00",
"author":{
"@type": "Person",
"name": "c47"
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://www.nixre.net/blog/vim-update-lastmod/"
},
"publisher": {
"@type": "Organization",
"name": "nixre.net",
"logo": {
"@type": "ImageObject",
"url": "https://www.nixre.net/favicon.ico"
}
}
}
</script>
</head>
<body class=" dark" id="top">
<script>
if (localStorage.getItem("pref-theme") === "light") {
document.body.classList.remove('dark')
}
</script>
<header class="header">
<nav class="nav">
<div class="logo">
<a href="https://www.nixre.net/" accesskey="h" title="nixre.net (Alt + H)">nixre.net</a>
<div class="logo-switches">
<button id="theme-toggle" accesskey="t" title="(Alt + T)">
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
</svg>
<svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</button>
</div>
</div>
<ul id="menu">
<li>
<a href="https://www.nixre.net/blog/" title="Blog">
<span>Blog</span>
</a>
</li>
<li>
<a href="https://www.nixre.net/categories/" title="Categories">
<span>Categories</span>
</a>
</li>
<li>
<a href="https://www.nixre.net/tags/" title="Tags">
<span>Tags</span>
</a>
</li>
<li>
<a href="https://www.nixre.net/series/" title="Series">
<span>Series</span>
</a>
</li>
<li>
<a href="https://www.nixre.net/about/" title="About">
<span>About</span>
</a>
</li>
</ul>
</nav>
</header>
<main class="main">
<article class="post-single">
<header class="post-header">
<h1 class="post-title entry-hint-parent">
Update Lastmod Using vim
</h1>
<div class="post-meta"><span title='2022-05-01 10:28:05 +0100 +0100'>[ Published: 2022-05-01 ]</span>&nbsp; &middot; &nbsp;<span title='2022-05-29 07:37:05 +0100 +0100'>[ Last modified: 2022-05-29 ]</span>&nbsp; &middot; &nbsp;2 min&nbsp; &middot; &nbsp;255 words&nbsp; &middot; &nbsp;c47
</div>
</header>
<div class="post-content"><p>I think it is very important to add date information about articles creation time and modification time.
There are various ways to do this. I usually prefer when this be done automatically by
<a target="_blank" href="https://www.vim.org"
>my favorite editor</a>
and so I wrote a short function:</p>
<pre tabindex="0"><code>function! LastMod()
if expand(&#39;%:p:h&#39;) =~ &#39;path/to/my/hugo-website/www.nixre.net/content&#39;
let l:cursorpos = winsaveview()
let l:timestamp = strftime(&#39;%Y-%m-%dT%H:%M:%S+02:00&#39;)
silent! exe &#39;:%s/^\(lastmod:\).*$/\1 &#39; . expand(l:timestamp) . &#39;/&#39;
call winrestview(l:cursorpos)
endif
endfunction
</code></pre><p>The markdown sources are inside the content folder of my hugo website and the <code>expand('%:p:h')</code> of the file that
is currently loaded into the buffer will show the absolute path to the directory that contains the file.
Vim will check if the string afterwards <code>path/to/my/[..]/content</code> is part of path to the currently loaded file.
If so then first it saves the properties of the current window, like cursorposition and so. Then it creates a
local variable <code>l:timestamp</code> with the current timestamp, searches the line that begins with <code>lastmod:</code> and
updates this line with the current timestamp in <code>l:timestamp</code>. As last step it resets the window that was saved
into the <code>l:cursorpos</code> variable in the first step.</p>
<p>Now to make vim execute this function I added an autocommand group which calls the above function:</p>
<pre tabindex="0"><code>augroup www.nixre.net
au!
au BufWrite /full/path/to/my/hug-website/www.nixre.net/content/**.md call LastMod()
augroup end
</code></pre><p>This tells vim to call the function if I save a file that has the .md extension in the given path. This way vim
always updates the timestamp string in the frontmatter field <code>lastmod:</code> when I save the buffers content to the file.</p>
</div>
<footer class="post-footer">
<ul class="post-tags">
<li><a href="https://www.nixre.net/tags/editor/">editor</a></li>
<li><a href="https://www.nixre.net/tags/text/">text</a></li>
<li><a href="https://www.nixre.net/tags/vim/">vim</a></li>
</ul>
</footer>
</article>
<nav class="paginav">
<a class="prev" href="https://www.nixre.net/blog/vim2nvim/">
<span class="title">« Prev</span>
<br>
<span>n(eo)vim</span>
</a>
<a class="next" href="https://www.nixre.net/blog/veraenderungen/">
<span class="title">Next »</span>
<br>
<span>Veraenderungen</span>
</a>
</nav>
</main>
<footer class="footer">
<span>&copy; 2024 <a href="https://www.nixre.net/">nixre.net</a></span>
<span>
Powered by
<a href="https://gohugo.io/" rel="noopener noreferrer" target="_blank">Hugo</a> &
<a href="https://github.com/adityatelange/hugo-PaperMod/" rel="noopener" target="_blank">PaperMod</a>
<a href="/impressum/">Impressum</a>
</span>
</footer>
<a href="#top" aria-label="go to top" title="Go to Top (Alt + G)" class="top-link" id="top-link" accesskey="g">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentColor">
<path d="M12 6H0l6-6z" />
</svg>
</a>
<script>
let menu = document.getElementById('menu')
if (menu) {
menu.scrollLeft = localStorage.getItem("menu-scroll-position");
menu.onscroll = function () {
localStorage.setItem("menu-scroll-position", menu.scrollLeft);
}
}
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
var id = this.getAttribute("href").substr(1);
if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({
behavior: "smooth"
});
} else {
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView();
}
if (id === "top") {
history.replaceState(null, null, " ");
} else {
history.pushState(null, null, `#${id}`);
}
});
});
</script>
<script>
var mybutton = document.getElementById("top-link");
window.onscroll = function () {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
mybutton.style.visibility = "visible";
mybutton.style.opacity = "1";
} else {
mybutton.style.visibility = "hidden";
mybutton.style.opacity = "0";
}
};
</script>
<script>
document.getElementById("theme-toggle").addEventListener("click", () => {
if (document.body.className.includes("dark")) {
document.body.classList.remove('dark');
localStorage.setItem("pref-theme", 'light');
} else {
document.body.classList.add('dark');
localStorage.setItem("pref-theme", 'dark');
}
})
</script>
</body>
</html>