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!

  • NB you use $(today) within the example snippet, instead of $(date)! – aplaice Jan 21 at 0:39
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.)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.