1

In XFCE4 it is possible to install templates in ~/Templates and they will appear under the Create Document entry of the Context and File Menus.

Now, let's say I create a Python template there (it can be any language) with the following snippet.

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#################################################
# Author: Me
# Date:   $(today)
#################################################

My question is how to achieve that $(date) be automatically replaced with the current date when a new file is created using the template. The solution should work for any text-based template like tex, c, cpp, h, html, js, css, etc.

Considering that Thunar is very lightweight it very likely this is not possible. Nonetheless I would like to know if there is a workaround.

Thanks!

1
  • NB you use $(today) within the example snippet, instead of $(date)!
    – aplaice
    Jan 21, 2019 at 0:39

1 Answer 1

0

AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.

Preparation

First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:

#!/bin/bash
# produce dd/mm/yyyy date, other formats possible
date=$(date +"%d/%m/%Y")
# see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
escaped_date=$(echo "$date" | sed 's/[\/&]/\\&/g')
sed -i 's/$(date)/'"$escaped_date"'/g' "$*"

and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.

Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).

Operation

Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:

  1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).

  2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).

(Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)

Room for improvement

In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)

1
  • I gave it a try and I must say it worked decently for one or two filetypes. However, custom actions get added to the main context menu and that one gets cluttered quite quickly, so I'd rather have them in a sub-menu, a feature that is not implemented yet.
    – Kyordhel
    Mar 27, 2019 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .