3

I'm using vim to take notes while reading academic articles. I prefer to have a new text file for each note I've taken, but organizing them becomes tedious.

What I would like to do is set an autocommand to detect if I'm in a certain directory, writing to a newfile and then appened the current date and time to whatever filename I write.

So if I have:

:pwd
/path/to/projects

When I type

:w Notes

I would like vim instead to save the file as "Notes - <CURRENT DATE - TIME >.txt"

I believe it involves declaring something like the following in my vimrc:

autocmd BufNewFile,BufWrite "/path/to/projects/*" <command involving strftime("%Y-%m-%d_%H-%M")>

But I can't figure out what. Any Ideas? I'm using Vim 7.3 on Debian Linux.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303

2 Answers2

5

You're very close. I think it's best to rename the file as it is created; messing with the file name during writes makes this more difficult (e.g. what if you re-open an existing note, or just write the buffer again?)

The :file command can be used to rename the current file; the current filename is in the % special identifier. Triggered when a new file is created, this does the job:

autocmd BufNewFile /path/to/projects/* execute 'file' fnameescape(expand('%') . strftime(" - %Y-%m-%d_%H-%M.txt"))

If you don't want to consider the original filename, this becomes even easier:

autocmd BufNewFile /path/to/projects/* execute 'file' fnameescape(strftime("Notes - %Y-%m-%d_%H-%M.txt"))
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks for the reply Ingo, this is very close to what I need. However, it requires me to launch vim from the terminal with an argument to be used as the filename. I use Openbox with Debian and have "sakura -e 'vim'" mapped to a hotkey, so I launch typically vim via the hotkey, start typing my note, and then use the `:w Note` command to save the file. Is there a something similar to what you mention above that would accommodate this? – Brian Albert Monroe Aug 06 '14 at 15:20
  • For that, replace `BufNewFile` with `BufWritePre`, and deal with subsequent writes. – Ingo Karkat Aug 06 '14 at 15:46
  • Replacing BufNewFile with BufWritePre still hasn't solved the issue, when I type `:w Note` while in `/path/to/projects` the file is saved as `Note`. However, I found a decent workaround with the code you posted by adding a new hotkey map for "sakura -e 'vim /path/to/projects/Note'". This still restricts the name of all my notes to "Note" though. – Brian Albert Monroe Aug 06 '14 at 16:16
4

You may be looking for something along the lines of:

function! SaveWithTS(filename) range
    execute "save '" . a:filename . strftime(" - %Y-%m-%d_%H-%M.txt'")
endfunction

command! -nargs=1 SWT call SaveWithTS( <q-args> )

With the above in your .vimrc, executing :SWT Note will save your file as Note - YYYY-MM-DD_HH-MM.txt. This has the disadvantage of not happening automatically, so you have to remember to use :SWT instead of :w the first time your write your file, but it does let you wait until you are ready to save to decide what your filename should be (i.e. you aren't stuck with Note every time).

Edit: The above version of SaveWithTS actually saves to the filename with single quotes around it (bad testing on my part). Below is a version that should fix that and also lets you specify an extension to your file (but will default to .txt)

function! SaveWithTS(filename) range
    let l:extension = '.' . fnamemodify( a:filename, ':e' )
    if len(l:extension) == 1
        let l:extension = '.txt'
    endif

    let l:filename = escape( fnamemodify(a:filename, ':r') . strftime(" - %Y-%m-%d_%H-%M") . l:extension, ' ' )

    execute "write " . l:filename
endfunction