Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 837

Answer by Kamil Maciorowski for bash alias or bash function that adds a timestamp to the beginning of a filename when creating a file with nano?

$
0
0

A function like this:

# unalias nanotime first, if needednanotime() {    nano "$(date +%Y-%m-%d_%H.%M.%S)-$1"}

will do what you want, although it's limited.

  • It simply glues the date string to the beginning of the first argument, so paths with / will not behave well.
  • It uses just one argument. With $@ we could pass many arguments to nano, but in general the name you want to glue the date to may be any of them.

To overcome these a non-trivial logic is needed. I'm not going to create any logic because personally I would prefer the following:

bind -x '"\C-x\C-t": _nanotime'_nanotime() {    local dte    dte="$(date +%Y-%m-%d_%H.%M.%S-)"    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$dte${READLINE_LINE:$READLINE_POINT:${#READLINE_LINE}}"    READLINE_POINT="$((READLINE_POINT+${#dte}))";}

From now on Ctrl+x, Ctrl+t injects the current date and time when you type a command. E.g. type
nano Ctrl+x, Ctrl+tThis_is_the_description_of_my_file.txtEnter

The advantage is the method is not limited to nano. Insert the date in any command line you wish, at any cursor position you wish.

Tested in Bash 5.2.21.


Viewing all articles
Browse latest Browse all 837

Trending Articles