Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1446  / 3 Years ago, sat, july 24, 2021, 1:13:27

In MS Notepad I can create a file like this:



.LOG

I updated function xyz(), line 32 today.


save it, and when I open it:



.LOG

I updated function xyz(), line 32 today.

25-10-14 8:04:56


How can I achive the same functionality in gedit, or a similar program for Ubuntu?


More From » gedit

 Answers
5

I am not sure of the exact behaviour of Notepad here. While I don't know how to make vim do that based on some existing text in a file, I could make vim do something similar for files of a certain extension, by adding this to ~/.vimrc:



au BufWritePre *.log :normal G"=strftime("
%b %d, %Y %X
")^Mp


Explanation:




  • au - stands for AutoCommand, the vim way of executing some action for some event.

  • BufWritePre - the vim event for just before writing out a file.

  • *.log - the files to do this for (you could pick another extension)

  • :normal - Go to normal mode (the mode you're usually in when you start vim)

  • G - go to the last line

  • "=strftime("
    %b %d, %Y %X
    ")^M
    - Into the default buffer ("), copy the output of the strftime function (the ^M is a literal newline, obtained by pressing CtrlV, then Enter)

  • p - then paste the buffer.



Each time I write the file using :w, the current date will be appended to the file:



$ vim test.log
$ vim test.log
$ cat test.log
I did some work.

Oct 26, 2014 01:15:55 IST

Didn't I?

Oct 26, 2014 01:16:02 IST


This is quite customizable. You can pick your file extension, leave out the G if you want to append the date after the current line, modify the time format, use other events (see :h autocmd-events).


[#22680] Monday, July 26, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
moloy

Total Points: 457
Total Questions: 93
Total Answers: 119

Location: Romania
Member since Wed, Dec 29, 2021
2 Years ago
;