Blog: vim

Vim Templates

I feature of Vim I recently came across was the use of templates. Templates can be used when creating new documents to automatically insert information that will be always included. Examples would be headers for blog posts, copyright notices, zettelkasten ids. Templates are based on file extenstions. So if the new document name ends in .html it uses the html template, if it ends in .md it uses the md template.

Templates are named skeleton.yourextension. So the html template is named skeleton.html. The md template is named skeleton.md. If I need multiple templates for particular file type, a work around I use is using different valid extensions for a that file type. I use markdown for three types of documents; dairy entries, zettelkasten notes, and blog posts. So in order to have three different templates, I use three different markdown extensions; for diary entries I use .mkd, for zettelkasten notes I use .markdown, and for blog posts I use .md. What if you want multiple templates for a given extension. The answer is I don't know. I would suggest looking for a Vim plugin.

Templates can be used for static or dynamic information. A good article to read about Vim templates is Use eval to create dynamic templates The article contains some great drop in place code to use dynamic & static templates. The addition of the code below to your .vimrc will allow you to use static or dynamic templates. The only bit that needs to be changed is the path to your templates.

augroup templates
  au!
  " read in template files
  autocmd BufNewFile *.* silent! execute '0r $HOME/vimfiles/templates/skeleton.'.expand("<afile>:e")

  " parse special text in the templates after the read
  autocmd BufNewFile * %substitute#\[:VIM_EVAL:\]\(.\{-\}\)\[:END_EVAL:\]#\=eval(submatch(1))#ge
augroup END

Here is an example template I use:

 ---
 Title: 
 Published: [:VIM_EVAL:]strftime('%Y-%m-%d %H:%M')[:END_EVAL:]
 Author: Rev. Fr. Robert Bower
 Tag: 
 layout: blog
 ---

 Zettelkasten ID  **[:VIM_EVAL:]expand("%:t:r")[:END_EVAL:]**

Any static text can be typed as you would like it to appear. There are two tags [:VIM_EVAL:] and [:END_EVAL:]. Any vim script function that is between the tags will be run. So in my blog template my first set of tags calculates today's date and time. The second set uses expand to insert the file name which I use for the zettelkasten ID.

Using templates can speed up your workflow and ensure consistency in document structure.


Zettelkasten ID vimtemplates-2020-05-12-0933

Linking Zettelkasten Notes

For several years I have been keeping a journal and notes in Vimwiki. While Vimwiki is a great plugin, I wanted more control over how things were done, so I decided to develop my own system while still using Vim. The system is a work in progress. There are two things I have committed to. The first being the format will be plain text using markdown syntax. The second being the notes part of the system will be using the idea of zettelkasten for the actual taking of notes.

At this point I should tell you what zettelkasten is but I am not. That is for another post. Today I am going to talk about how to link notes together.

One of the sites I find very helpful about using Vim with zettelkasten is Edwin Wenick's Blog. His article titled Creating and Linking Zettelkasten Notes in Vim has been very helpful. This article would be not possible without it.

Edwin created some vim script that uses CtrlP to link the current note you are editing to a previous note. A reader, Fernando, created script that did the same thing except it used fzf and he posted it in the comments. I mentioned I wanted to build on that idea and add Silver Searcher to the linking script so it would be easier to find the previous note by searching on the text of all the notes rather than just file names. Well, before I could get around to it another reader, hbenevides, created a script that uses Ripgrep to search on the text of all the notes to create a link to a particular note and posted it. His script can be found also in the comments of the above article.

Rather than create something new for silver searcher, I built off hbenvides script.

Here is the modified script:

" make_note_link: List -> Str
" returned string: [Title](YYYYMMDDHH.md)
function! s:make_note_link(l)
        " fzf#vim#complete returns a list with all info in index 0
        let line = split(a:l[0], ':')
        let ztk_id = l:line[0]
    try
        let ztk_title = substitute(l:line[2], '\#\+\s\+', '', 'g')
catch

        let ztk_title = substitute(l:line[1], '\#\+\s\+', '', 'g')
endtry
        let mdlink = "[" . ztk_title ."](". ztk_id .")"
        return mdlink
endfunction

" mnemonic link zettel
inoremap <expr> <c-l>z fzf#vim#complete({
  \ 'source':  'rg --no-heading --smart-case  .',
  \ 'reducer': function('<sid>make_note_link'),
  \ 'options': '--multi --reverse --margin 15%,0',
  \ 'up':    5})
" mnemonic link ag
inoremap <expr> <c-l>a fzf#vim#complete(fzf#vim#with_preview({
  \ 'source':  'ag --smart-case  .',
  \ 'reducer': function('<sid>make_note_link'),
  \ 'options': '--multi --reverse --margin 15%,0',

What does this script do? This script has two different hot keys, one for Ripgrep and another for Silver Searcher. If you have both search tools installed you can use both and decide which one you like best. I left hbenevides original Ripgrep search alone except for one change. The script was originally designed to only search headers. I changed the search to also include all text. If you only want to search only headers you can start your search with the hashtag or modify the script back to the original search.

The script for Silver Search behaves much the same way but presents at the bottom of the page, which can be changed. The Silver Searcher script includes the fzf-vim preview window. If you use RipGrep and want the preview window that could certainly be added to the script for Ripgrep.

To use the RipGrep search in insert mode the key mapping is z and to use the Silver Searcher in insert mode the mapping is a

Hopefully someone besides me finds it helpful.

I am new to Vim Script so use at your own risk.

You may notice this article has a Zettelkasten ID. This is not a zettelkasten note but I am expanding the ID idea to many of my other text documents.

Zettelkasten ID linkingzettelkasten-2020-05-11-0735

VimWiki outside of Vim Revisted

Since me last post about VimWiki on the command line I have created a new script. I have also decided to run it as a function rather than an alias.

The new function is as follows

#!/bin/sh

function notesfind(){

wordfind=$1

/bin/grep -rwi "$wordfind" /home/frrobert/Nextcloud/vimwiki | fzf |cut -f1 -d":" | xargs cat

}

The function searchs within VimWiki documents to find a word match based on the word given. They syntax is notesfind followed by the word you are searching for. The results from grep are sent to fzf where a menu is presented with all the files that include the word along with the line of the file where the word is found. Once you select the file you are interested in, the file is displayed on the command line.

To the use the script simply copy the text of the script to an empty file and change the path to the location of your VimWiki files.. The way it is written is it can be used as a shell function or a shell script.

Remember this is not designed to replace the search VimWiki but a way to access a VimWiki document without having to switch to Vim when you are doing other work on the command line.

Bibliography

VimWiki https://github.com/vimwiki/vimwiki

cheat.sh https://github.com/chubin/cheat.sh

fzf https://github.com/junegunn/fzf

Luke Smith's fzf Video https://www.youtube.com/watch?v=vt33Hp-4RXg

VimWiki outside of Vim

I am a big fan of Vim. I use it to do my general word processing, to keep a diary, and to keep general notes. A plugin called VimWiki helps me to the last two. VimWiki allows you to keep notes and a diary. The notes in my Wiki are varied but one thing I keep in the wiki is little notes about commands I use in the shell. Often I forgot the syntax of a command while I am working on the command line but I know I have notes in my wiki on how to do what I want. So I have to go to Vim find the entry I want then go back to command line and type the command. This is assuming I haven't forgotten what the wiki said by the time I get back to the command line.

I found a neat little utility call cheat.sh. It will bring up an abbreviated cheat sheet for the command you typed in. I wanted to do something similarly but with my VimWiki entries. If I know the wiki entry file name I could just use the cat command to show it on the screen. I am too old for that. I remember coming across a YouTube Video by Luke Smith about fzf.

So between the video and fzf I came up with an alias to allow me to find wiki entries.

alias notes='du -a /home/frrobert/Nextcloud/vimwiki/* |awk "{print$2}" | fzf | xargs -r cat'

I type notes and hit enter and a list of wiki entries come up and I type what I think may be the file name and fzf finds it for me. One more strike of the enter key and the article is there on the command line.

Bibliography

VimWiki https://github.com/vimwiki/vimwiki

cheat.sh https://github.com/chubin/cheat.sh

fzf https://github.com/junegunn/fzf

Luke Smith's fzf Video https://www.youtube.com/watch?v=vt33Hp-4RXg

Vim SSH and Yellow

As in a previous blog post I mention I am using Yellow as my CMS. To create a blog post it is simple as creating a text file in the proper directory, with the proper header, and the text of the blog post. The text can be in either md or html. I found the easiest way to create a post is to create and write it right on the server with a server based editor. Vim is included with almost every Linux install and by creating your own .vimrc file you can customize the editor the way you want it. I have customized Vim to create the standard header with an abbreviation, added an html plugin, and spell check. So all I do is ssh into my hosting provider's server, start Vim, write my post in html, and save the file in the appropriate directory and there is my blog post.

The best part of this configuration is it allows my to easily write posts or edit on them on any device that has an ssh client. Since the editor is on the server and customized by me I don't have to worry about having the right tools or editor. It is always there ready to use.

To save a draft copy not ready for publication I save the file with the name I want to use but with a dot in front and then the file is hidden. When I am ready to publish it all I have to do is rename the file by removing the dot and there is my blog post.

Why not give Vim a try as your editor. Even if it is not for you it is an editor you should know how to use. It is often the one tool you have when your system leaves you with just a command prompt and a configuration file that has to be edited to get your gui back