Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace selection with a new zettel containing the selection #158

Open
MartyLake opened this issue Feb 4, 2025 · 3 comments
Open

Replace selection with a new zettel containing the selection #158

MartyLake opened this issue Feb 4, 2025 · 3 comments

Comments

@MartyLake
Copy link
Contributor

Hello,
I have some simple "foo" idea, so I create a new zettel and write about it. After some writing, it occurs to me that some later paragraph deserve its own "bar" zettel. Because writing is incremental, I do not know before hands that I should write the paragraph to the "bar" zettel already.

Actual

Right now, what I do is

  1. select lines about "bar" in "foo" zettel
  2. delete
  3. create new zettel for "bar"
  4. paste
  5. go back to "foo" buffer
  6. insert zettel link to "bar"

Expected / wish

I wish that I could do something like

  1. select lines about "bar" in "foo" zettel
  2. call dedicated function: in "foo", the selection is deleted and replaced by a link to "bar", a new zettel that is created in a split or tab (configurable?) containing already the text that was selected

Thanks in advance,
Best

@MartyLake
Copy link
Contributor Author

MartyLake commented Feb 4, 2025

I found a function (undocumented ZettelNewSelectedMap) that almost does this !

I modified it and got

function! ZettelCaptureSelected(title)
  let selection = zettel#vimwiki#get_visual_selection()
  let title = a:title
  let name = zettel#vimwiki#new_zettel_name(title)
  " prepare_template_variables needs the file saved on disk
  execute "write"
  " make variables that will be available in the new page template
  let variables = zettel#vimwiki#prepare_template_variables(expand("%"), title)
  let linktitle = "\\\\0"
  if match(vimwiki#path#current_wiki_file(), zettel#vimwiki#path()) != 0
    " if we are NOT in the zettelkasten, we should use absolute (vimwiki
    " root-relative absolute) paths for the name
    let idx = a:0 ? a:1 : vimwiki#vars#get_bufferlocal('wiki_nr')
    let prefix =  zettel#vimwiki#get_option('rel_path', idx)
    let name = "/" . prefix . name
    let linktitle = prefix . linktitle
  endif
  " replace the visually selected text with a link to the new zettel
  " \\%V.*\\%V. should select the whole visual selection
  execute "normal! :'<,'>s:\\%V.*\\%V.:" . zettel#vimwiki#format_link( name, linktitle) ."\<cr>\<C-o>"
  call zettel#vimwiki#zettel_new(title, variables)
  " paste selection at end of file
  execute "normal! Go" . selection
endfunction

EDIT: the original post had some typo. I found it and now it works 🎉 so I corrected the post.

@michal-h21
Copy link
Owner

I am doing it in this way:

  1. select lines about foo
  2. delete
  3. write a title for the new note
  4. select the title
  5. press z, which calls ZettelNewSelectedMap and creates a new note with the selected title
  6. paste the deleted content

@MartyLake
Copy link
Contributor Author

Well, now you have a function that does it at once !

I think ZettelNewSelectedMap deserves its own help section, maybe something like #159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants