On 11/12/2011 06:49 PM, Gelonida N wrote:
On 11/12/2011 11:46 PM, AK wrote:

def setreg(regname, val):
      """ sets register regname to string val
          all it does is replace " with \"
          and use the 'let' command to set a register
      """
      val = val.replace('"',r'\"')
      vim.command('let @%s = "%s"' % (regname, val))


def uppercase_a(regname):
      """ sample command to show the use of setreg() """
      reg_a = vim.eval('@' +  regname)
      reg_a = reg_a.replace('a', 'A')
      setreg('a', reg_a)

With above python function I could now do

:py setreg('a', 'ABC"\'\tDEF\nGHI')

and register a would contain the string as specified in python.


Should this do the job or does anybody see potential improvements?



I think that should be fine.. test if it works with unicode text?
Hmm, probably this will fail. good point.
For the time being I'm fine without unicode, meaning
  with buffers containing \n \r \t ' " characters
and perhaps some ASCII text as well ;-)

I'm not quite sure why you want to set a register contents, typically
you want to put register contents somewhere, so if you're using this
python code, why not have it skip the step and put the text where
you need it, e.g. in a buffer?


Probably you're right, that what I was asking for is rarely useful.
I just started learning about python scripting within vim.

Knowing almost nothing about vim scripting,
but knowing basic vim commands and knowing python quite well
I was looking for the 'basic' commands, that would allow me to interact
with vim buffers and registers, (probably I still have to learn about
selections / marks)

My thinking was, that if I can access, buffers, registers, marks
and if I can inject key strokes, that I should be able to
write almost anything, that I would like to do (even if there might be
more elegant solutions if I knew more of the vim scripting commands)

One potential use case of setting a register would be to get
text from a selection, buffer, register, process it and dump the result
in the clipboard, such, that I could paste it into another application
(web browser / console window, whatever)
If I understood well I could do this by setting the '+' or '*' register.


I mostly use command, eval, buffers, windows, current.buffer and current.line in vim python scripts.

From your earlier message: to get the selection, do something with it
and replace with new text, just cut it to a register using "ax normal
command, then simply insert new text at current cursor location.

 -ak

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to