michal-h21 / vim-zettel

VimWiki addon for managing notes according to Zettelkasten method
MIT License
557 stars 73 forks source link

use random id to name the file #63

Closed bangedorrunt closed 4 years ago

bangedorrunt commented 4 years ago

it'd be great if you could add feature allow us to create file using random id with optional length (prefer 7-8).

this could be done using mktemp XXXXXXX.md for example

note: mktemp in macOS doesn't have --suffix option

michal-h21 commented 4 years ago

Good idea. Recent versions of Vim have rand() function, so I've used that and added support. It can be required using:

  let g:zettel_format = "%random"

You can combine it with other formats, of course. The number of characters is configured using

 let g:zettel_random_chars=8 

This is the default value.

bangedorrunt commented 4 years ago

awesome ❤️❤️❤️

bangedorrunt commented 4 years ago

i got these error unfortunately

fyi, i'm using neovim 0.5 nightly

Error detected while processing function zettel#vimwiki#zettel_new[1]..zettel#vimwiki#create[2]..zettel#vimwiki#new_zettel_name:
line   31:
E121: Undefined variable: s:randomchars
E116: Invalid arguments for function substitute
E15: Invalid expression: substitute(newformat, "%random", s:randomchars, "")
new zettel: 11:35:16 pmandom
Error detected while processing function zettel#vimwiki#zettel_new[1]..zettel#vimwiki#create[7]..zettel#vimwiki#make_random_chars:
line    1:
E117: Unknown function: srand
E15: Invalid expression: srand()
line    2:
E730: using List as a String
E15: Invalid expression: range(g:zettel_random_chars)->map({-> (97+rand(seed) % 26)->nr2char()})->join('')
"~/workspace/note-to-self/11:35:16 pmandom.md" [New] 5L, 39C [w]
3
"~/workspace/note-to-self/learn.me.some/11:35:16 pmandom.md" [New] 6L, 45C [w]
"~/workspace/note-to-self/learn.me.some/11:35:16 pmandom.md" 13L, 70C [w]
michal-h21 commented 4 years ago

Ah, it seems that the srand() function isn't available in Neovim :/

bangedorrunt commented 4 years ago

i hope you could come up with general solution to support neovim when possible 👍

michal-h21 commented 4 years ago

I've added the Neovim support, hopefully.

bangedorrunt commented 4 years ago

I've added the Neovim support, hopefully.

unfortunately, it's not working

Error detected while processing function zettel#vimwiki#zettel_new[1]..zettel#vimwiki#create[2]..zettel#vimwiki#new_zettel_name:
line   31:
E121: Undefined variable: s:randomchars
E116: Invalid arguments for function substitute
E15: Invalid expression: substitute(newformat, "%random", s:randomchars, "")
michal-h21 commented 4 years ago

I've added a debugging message to test whether random char generator works. You should get message like

Test random chars: gphxamxi

when you run :messages command.

bangedorrunt commented 4 years ago

i do see the random id

Test random chars: kcvsapqg
Error detected while processing function zettel#vimwiki#zettel_new[1]..zettel#vimwiki#create[2]..zettel#vimwiki#new_zettel_name:
line   32:
E121: Undefined variable: s:randomchars
E116: Invalid arguments for function substitute
E15: Invalid expression: substitute(newformat, "%random", s:randomchars, "")
new zettel: 07:00:54 pmandom
"~/workspace/note-to-self/07:00:54 pmandom.md" [New] 5L, 39C [w]
"~/workspace/note-to-self/07:00:54 pmandom.md" 6L, 45C [w]
"~/workspace/note-to-self/07:00:54 pmandom.md" 6L, 45C [w]
michal-h21 commented 4 years ago

I see, the s:randomchars was not initialized if you set g:zettel_random_chars. I hope it is fixed now.

bangedorrunt commented 4 years ago

I see, the s:randomchars was not initialized if you set g:zettel_random_chars. I hope it is fixed now.

you've done it! thank you so much

new zettel: regalxug
"~/workspace/note-to-self/regalxug.md" [New] 5L, 39C [w]
gleb108 commented 4 years ago

I've got similar error with ZettalNew command. I deleted vimwiki and vim-zettel plugins and reinstall them again with Vundle. But the issue remains current.

Error detected while processing function zettel#vimwiki#make_random_chars: line 1: E117: Unknown function: srand E15: Invalid expression: srand() line 2: E730: using List as a String E15: Invalid expression: range(g:zettel_random_chars)->map({-> (97+rand(seed) % 26)->nr2char()})->join('') new zettel: 20200905121930 Error detected while processing function zettel#vimwiki#zettel_new[1]..zettel#vimwiki#create[7]..zettel#vimwiki#make_random_chars: line 1: E117: Unknown function: srand E15: Invalid expression: srand() line 2: E730: using List as a String "20200905121930.md" 11L, 70C written

my .vimrc " Settings for Vimwiki let g:vimwiki_list = [{"path":"~/zettelkasten/", 'ext':'.md','syntax':'markdown','auto_tags': 1, 'auto_toc': 1}] let g:zettel_fzf_command = "rg --column --line-number --ignore-case --no-heading --color=always "

michal-h21 commented 4 years ago

@gleb108 hi, which Vim version do you use? It seems that random numbers are available only from Vim v8.1.2342, or in Neovim. I've seen some hacks for older Vim versions without built-in support for random numbers, but that would mean to support at least three different methods of random number generation. That could lead to quite difficult code to support :/

gkelly commented 3 years ago

Ubuntu 20.04.1 LTS is still on Vim 8.1.2269, fwiw, and doesn't have srand either.