0

I'd like to insert uuids into my tests when I need them without having to leave vim. I've tried to create a python3 plugin to do this:

import neovim 
import uuid

@neovim.plugin
class GenerateUuid(object):
    def __init__(self, vim):
        self.vim = vim
        self.current_line = vim.current.line
    

    @neovim.function('GetUuid')
    def getUuid(self, args):
        id = str(uuid.uuid4())
        # copy to clipboard
        self.vim.current.line = self.current_line + id

When I edit a new line in my file to look like:

// start of file
import fs from 'fs';

let id = 

and run :exec GetUuid() I see that it inserts the uuid into the first line instead to look like this:

import fs from 'fs'; 10436f81-fe16-43a4-abe4-9e4b2599854f

I'd like to append it at the current cursor position and copy it to the clipboard. Any help would be appreciated.

2 Answers 2

1

This is not directly an answer to your question because I don't feel like digging in some python plugin code. This is just an alternative way covering only the need you presented.


What about something much simpler than your whole plugin? For example you could add these lines to your vimrc:

command! GetUuid let @a=systemlist("python -c 'import uuid; print(uuid.uuid4())'")[0]|norm! "ap
inoremap <uniq_mapping> <C-o>:GetUuid<CR>

The first line creates a new command :GetUuid. This command uses :h systemlist() to get the output of the shell command python -c 'import uuid; print(uuid.uuid4())' (which prints a uuid). It puts the output of the command in the register a and then paste the register after the cursor position.

I'm using systemlist() instead of :h system() to avoid getting the newline in the result. There is a drawback of poluting your register a but you could choose any other one. To avoid that, one could use :h :put with the following command but it has the issue of putting the inserted text on a different line:

command! GetUuid put =systemlist('python3 -c \"import uuid; print(uuid.uuid4())\"')[0]

And the second line is an example of how to create an insert mode mapping to add the uuid without leaving the insert mode.

3
  • possibly :command GetUUID put =systemlist('python3 -c \"import uuid; print(uuid.uuid4())\"')[0] | join?
    – D. Ben Knoble
    Feb 22 at 14:49
  • @D.BenKnoble that didn't work for me, I had to use command! GetUUID put =systemlist('python3 -c \"import uuid; print(uuid.uuid4())\"')[0] | norm! kJ But I suspect there are some edge cases where this would not be perfect neither.
    – statox
    Feb 22 at 15:48
  • Oh, maybe -join then?
    – D. Ben Knoble
    Feb 22 at 15:53
1

In my question, I asked for a Python3 neovim plugin solution. This answer is just for completion:

def insert_at_cursor(self, value):
       row, col = self.vim.current.window.cursor
       current_line = self.vim.current.buffer[row - 1]
       new_line = current_line[:col] + value + current_line[col:] 
       self.vim.current.buffer[row - 1] = new_line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.