LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatic date stamp in text document (https://www.linuxquestions.org/questions/linux-newbie-8/automatic-date-stamp-in-text-document-502979/)

gemini728 11-19-2006 08:55 AM

Automatic date stamp in text document
 
Hi guys,

I am a recent convert from Windows to Linux and am currently using Fedora 5.

In Windows, there was a functionality in notepad that was really cool. You create a new Notepad document and enter ".LOG" at the top of the document, then close it. Now each time you open that document it automatically enters a datetime stamp on a new line. I used it for keeping notes. All I had to do was open it and start typing. It would put a new date-and-time stamp on a new line below which I could write my notes.

My question is: is there something like that available in any of the usual Linux/KDE text editors (KWrite, KEdit, Kate)? That would be really handy.

Thanks guys.

unSpawn 11-19-2006 04:36 PM

is there something like that available in any of the usual Linux/KDE text editors
In Vi (~/.vimrc) adding these lines:
Code:

function! InsertDate()
let l = line("$")
    exe "1," . l . "g/adddate /s/adddate /" . strftime("%Y%m%d") " "
endfun

makes each line that starts with "adddate " gets replaced with the date when the file is saved, so this should work in Gvim as well (~/.gvimrc?). Dunno about the other "usual Linux/KDE text editors".

gemini728 11-21-2006 07:39 AM

Quote:

Originally Posted by unSpawn
[i]
In Vi (~/.vimrc) adding these lines:
Code:

function! InsertDate()
let l = line("$")
    exe "1," . l . "g/adddate /s/adddate /" . strftime("%Y%m%d") " "
endfun

makes each line that starts with "adddate " gets replaced with the date when the file is saved, so this should work in Gvim as well (~/.gvimrc?). Dunno about the other "usual Linux/KDE text editors".

I suppose that would provide the functionality, but it would require that I type the word "adddate". I am not familiar with VI, but I could give that a try.

Anybody else know of something that would work without having to type anything? And hopefully with a texst editor in KDE.

pmcgraw 02-25-2009 10:15 AM

_vimrc changes for emulating notepad .LOG behavior
 
I use the .LOG datestamp behavior in notepad on Windows frequently, but my editor of choice is vim or gvim, so I wanted to emulate notepad's behavoir more precisely than the solution suggested by unSpawn.

Although modifying .vimrc (or _vimrc on Windows) was fairly new to me, I used unSpawn's approach as a starting point (writing an InsertDate function) and combined it with an autocmd which looks for the .LOG at the beginning of any file:

Code:

function! InsertDate()
    normal G
    execute "normal O" . strftime("%Y-%m-%d %H:%M") . " \<Esc>"
    normal $
endfun
autocmd BufReadPost * if getline(1) =~? '^\.LOG$' | call InsertDate() | endif

As you can see, I prefer the ISO8601 date format standard, but you are welcome to use whatever date format you prefer. I hope others find this useful.


All times are GMT -5. The time now is 12:17 PM.