Inserting Date & Time into any text file

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
User avatar
ccoyle
Level 1
Level 1
Posts: 48
Joined: Sun Mar 20, 2016 11:23 am

Inserting Date & Time into any text file

Post by ccoyle » Sun Jan 22, 2017 12:46 pm

I want to be able to hit a key combo and have the current date and time popped into any text editor, word processor, Evernote file, email, etc. This is easy to do with Auto Hotkey, which unfortunately is not an option on Linux. I don't want to use Wine.

There should be an easy way to do this in Linux

If it matters: Mint 17.3, Cinnamon, Kernel 4.9.0-040900

User avatar
xenopeek
Level 24
Level 24
Posts: 24135
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Inserting Date & Time into any text file

Post by xenopeek » Sun Jan 22, 2017 2:09 pm

I don't know for older version but for the current version of Cinnamon you can add a keyboard shortcut for this. First, install xdotool from Software Manager (or with command apt install xdotool). You need xdotool for this. With that installed open Keyboard from System Settings and go to the Shortcuts tab. In the left sidebar there select Custom Shortcuts and add a custom shortcut. Give it a name you want and use as command:
bash -c "sleep .5 && xdotool type \"$(date)\""

Then assign a keyboard shortcut to it and it should work immediately. You can customize the date command to output the date and time in the format you want. See the FORMAT section here: https://linux.die.net/man/1/date. For example to use your locale's YY/MM/DD date and HH:MM:SS time format you would replace the above command with:
bash -c "sleep .5 && xdotool type \"$(date +'%x %X')\""

It is best to experiment with this on the terminal. So run the command date +'%x %X' on the terminal and see if you like the format it uses or else change the format to your liking with the link I shared.

For more complex actions we'll need xvkbd but that requires a configuration file.

Edit: xdotool type mimics human typing I see :) You can shorten the delay between typed letters if you want by adding the --delay option after xdotool type. As in for example shortening it to zero:
bash -c "sleep .5 && xdotool type --delay 0 \"$(date +'%x %X')\""
Image

User avatar
chrisuk
Level 5
Level 5
Posts: 593
Joined: Thu Jun 12, 2008 6:16 am

Re: Inserting Date & Time into any text file

Post by chrisuk » Sun Jan 22, 2017 2:28 pm

You could also use xclip... it's in the repos. Then just pipe the date to the clipborad. date | xclip. Then either just paste or add xclip -o. Add the command to a shortcut as described above.

(I'm rushing a bit, but I'm sure any errors will be spotted and corrected by someone else ;) )
Chris

Manjaro MATE - MX Linux - LMDE MATE

BEeK
Level 1
Level 1
Posts: 40
Joined: Sun Feb 28, 2016 10:17 am

Re: Inserting Date & Time into any text file

Post by BEeK » Sun Jan 22, 2017 3:24 pm

xenopeek wrote:I don't know for older version but for the current version of Cinnamon you can add a keyboard shortcut for this. First, install xdotool from Software Manager (or with command apt install xdotool). You need xdotool for this. With that installed open Keyboard from System Settings and go to the Shortcuts tab. In the left sidebar there select Custom Shortcuts and add a custom shortcut. Give it a name you want and use as command:
bash -c "sleep .5 && xdotool type \"$(date)\""

Then assign a keyboard shortcut to it and it should work immediately. You can customize the date command to output the date and time in the format you want. See the FORMAT section here: https://linux.die.net/man/1/date. For example to use your locale's YY/MM/DD date and HH:MM:SS time format you would replace the above command with:
bash -c "sleep .5 && xdotool type \"$(date +'%x %X')\""

It is best to experiment with this on the terminal. So run the command date +'%x %X' on the terminal and see if you like the format it uses or else change the format to your liking with the link I shared.

For more complex actions we'll need xvkbd but that requires a configuration file.

Edit: xdotool type mimics human typing I see :) You can shorten the delay between typed letters if you want by adding the --delay option after xdotool type. As in for example shortening it to zero:
bash -c "sleep .5 && xdotool type --delay 0 \"$(date +'%x %X')\""
Thank you, xenopeek!

I setup my keyboard shortcut for inserting time in XFCE (go to Settings > Keyboard > Application Shortcuts; a shortcut will be read from your keyboard, just type what you want).

But there's a problem with "xdotool" as it apparently base the keys on English/US keyboards... Then I had to this for it to type my date format appropriately:

Code: Select all

# for my keyboard layout, it will type: "2017-01-22 - 17:18:56 BRST"
bash -c "sleep .5 && xdotool type \"$(date +'%Y-%m-%d - %HÇ%MÇ%S %Z')\""

User avatar
xenopeek
Level 24
Level 24
Posts: 24135
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Inserting Date & Time into any text file

Post by xenopeek » Sun Jan 22, 2017 3:43 pm

chrisuk wrote:You could also use xclip... it's in the repos. Then just pipe the date to the clipborad. date | xclip. Then either just paste or add xclip -o. Add the command to a shortcut as described above.

xclip -o just outputs to stdout, it doesn't type it into the active window. So this would be a two step: press a shortcut to have xclip put the date in the clipboard and then use another shortcut to paste the contents of the clipboard in the active window. You could fake the Ctrl+V keypress but not all programs use that shortcut for pasting. You could rig it to use the secondary clipboard, the one accessed through highlighting text and then pasting by middle click, but then you'd have to fake a middle click in the active window somehow. I'm sticking with xdotool type :wink:
Image

User avatar
chrisuk
Level 5
Level 5
Posts: 593
Joined: Thu Jun 12, 2008 6:16 am

Re: Inserting Date & Time into any text file

Post by chrisuk » Sun Jan 22, 2017 3:49 pm

xenopeek wrote:
chrisuk wrote:You could also use xclip... it's in the repos. Then just pipe the date to the clipborad. date | xclip. Then either just paste or add xclip -o. Add the command to a shortcut as described above.

xclip -o just outputs to stdout, it doesn't type it into the active window. So this would be a two step: press a shortcut to have xclip put the date in the clipboard and then use another shortcut to paste the contents of the clipboard in the active window. You could fake the Ctrl+V keypress but not all programs use that shortcut for pasting. You could rig it to use the secondary clipboard, the one accessed through highlighting text and then pasting by middle click, but then you'd have to fake a middle click in the active window somehow. I'm sticking with xdotool type :wink:


Oh, OK then... I knew I shouldn't have rushed it ;)
Chris

Manjaro MATE - MX Linux - LMDE MATE

User avatar
phd21
Level 19
Level 19
Posts: 9442
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Inserting Date & Time into any text file

Post by phd21 » Sun Jan 22, 2017 4:21 pm

Hi "ccoyle",

I just read your post and the good replies to it. Here are my thoughts on this as well.

A lot of the text editor and applications already have keyboard shortcuts built-in for adding the date and or time, like the superb "CherryTree" which uses "Ctl-Alt-M" for adding Date and Time. Look in the application's settings, configuration, or options for shortcuts.

On my Linux Mint KDE 17.3 system, I can also right click the system tray panel clock, and select copy to clipboard various dates and or time formats, then I can just right click and paste that into any application, or use "Shift-Insert" or "CtL-v" to paste it into any application. I do not know if there is a keyboard shortcut for copying this to the memory clipboard, but there might be.

There is also another extremely useful application called "Autokey" where you can add a "script" to copy the date and time and paste it into any application with one keyboard shortcut.

AutoHotKey (Autokey): Paste the current date into any program
https://gist.github.com/davejamesmiller/1965837


Hope this helps ...
DateTime_in_SystemTrayPanel1sm.jpg
DateTime_in_SystemTrayPanel2sm.jpg
Phd21: Mint KDE 18.3 & 19, 64-bit Awesome OS, Ancient Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram,256gb SDD, Video: Intel 4 Graphics, DVD Lightscribe. Why I use KDE?:https://opensource.com/life/15/4/9-reasons-to-use-kde

User avatar
ccoyle
Level 1
Level 1
Posts: 48
Joined: Sun Mar 20, 2016 11:23 am

Re: Inserting Date & Time into any text file

Post by ccoyle » Mon Jan 23, 2017 9:01 am

Thanks for the helpful replies. I'm going to give them a try.

User avatar
phd21
Level 19
Level 19
Posts: 9442
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Inserting Date & Time into any text file

Post by phd21 » Mon Jan 23, 2017 12:27 pm

Hi "ccoyle",

You are welcome.

I was experimenting with "Autokey" and there are 2 versions in the Software Manager or Synaptic Package Manager (SPM), "autokey-gtk" and "autokey-qt". I use "autokey-qt". There is also a "fork" of the "autokey" application called "autokey-py3" where I have a link below on that. I would not try to install and use both "autokey" and "autokey-py3" applications at the same time, as they might conflict. I added "autokey" to my startup applications because I use it everyday.

Besides being an incredibly useful application for automatically inserting (pasting) repetitive data, like names, addresses, phone numbers, even programming code, etc... It could also insert (copy and or paste) the system date and time. I have been using it for years to help out in this forum, and for other tasks, to paste various information into posts and replies. I use the superb "CherryTree" for storing notes and information, and "autokey" for quickly inserting notes and information into applications and or forums that I use often.

I tried the script from the link for "autohotkey" in my previous reply, and it did not work in my "autokey". But, I will provide links below on "autokey" which should help anyone. I just "recorded" a new "autokey" script which I named "DateTime" to record my mouse movements to right click the system tray panel date & time, copy to clipboard, & select a format, and then I could just paste ("Shift-Insert", or "CTL-V") that date and time format I chose into an application, and it worked well. I am sure with some more experimentation, I could have it copy and paste in one keystroke combination.

Update: After some experimentation and research, this new script below which I called "DateTime2" worked perfectly for inserting (pasting) the date and time in a format I chose with one keystroke combination. You can obviously change the date and time format within the parenthesis to whatever you want or need. I used the key combination "CtL+Alt+T" as my shortcut to paste (insert) the date and time, but you have to make sure that whatever keystroke combination you use does not conflict with other existing keystroke shortcuts "globally" (desktop) or "locally" within a specific application.

Date and Time variables
https://linux.die.net/man/3/strftime
Interactive Date and Time samples.
http://strftime.net/

Autokey script Example #1

#Enter script code for date and time "Mon 01/23/2017 10:43am"
output = time.strftime("%a %m/%d/%Y %H:%M%p")
keyboard.send_keys(output)

Autokey script example #2

#Enter script code for date and time "2014-08-07 21:06 Aug-Thur"
output = time.strftime("%F %R %b-%a")
keyboard.send_keys(output)

Autokey script example #3

#Enter script code for date and time "Mon Jan 23,2017 11:12am wk:04"
output = time.strftime(%a %b %d,%Y %H:%M%p wk:%W)
keyboard.send_keys(output)


Image

Using Autokey Scripts to Automate Your Linux Desktop
https://www.maketecheasier.com/using-au ... x-desktop/

Autokey: Make Your Own Keyboard Shortcuts In Linux
https://www.maketecheasier.com/autokey- ... -in-linux/

Google Groups - autokey
https://groups.google.com/forum/#!searc ... 3NxnxfOLgJ

Github - Autokey
https://github.com/autokey/autokey

Github - Autokey-py3
https://github.com/autokey-py3/autokey

Hope this helps ...
Phd21: Mint KDE 18.3 & 19, 64-bit Awesome OS, Ancient Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram,256gb SDD, Video: Intel 4 Graphics, DVD Lightscribe. Why I use KDE?:https://opensource.com/life/15/4/9-reasons-to-use-kde

User avatar
ccoyle
Level 1
Level 1
Posts: 48
Joined: Sun Mar 20, 2016 11:23 am

Re: Inserting Date & Time into any text file

Post by ccoyle » Mon Jan 23, 2017 7:58 pm

phd21 wrote:Hi "ccoyle", . . .

There is also another extremely useful application called "Autokey" where you can add a "script" to copy the date and time and paste it into any application with one keyboard shortcut. . . .


Hey! That did it. I used this script;

#Enter script code for date and time "2014-08-07 21:06 Thursday"
output = time.strftime("%F %R %A")
keyboard.send_keys(output)

Yields "2017-01-23 18:54 Monday" I have it bound to Super+T. Just what I'm looking for.

Thanks to everyone for all the ideas.

User avatar
phd21
Level 19
Level 19
Posts: 9442
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Inserting Date & Time into any text file

Post by phd21 » Mon Jan 23, 2017 8:10 pm

Hi "ccoyle",

You are welcome...
Phd21: Mint KDE 18.3 & 19, 64-bit Awesome OS, Ancient Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram,256gb SDD, Video: Intel 4 Graphics, DVD Lightscribe. Why I use KDE?:https://opensource.com/life/15/4/9-reasons-to-use-kde

Post Reply

Return to “Newbie Questions”