AsafSamuel
AsafSamuel

Reputation: 13

Vim scripting with manipulation over selected text

I'm translating an app (javascript) and I want to make it faster. I need a way to take selected text for example "Lorem Ipsum", replace it with "strings.lorem_ipsum". Then I want to go to the end of the file where my translation object is and add a field "lorem_ipsum: 'Lorem Ipsum' ". My thought was to create a function that does:

1) Create a marker (ma)
2) Copy selected text to variable (str1)
3) let str2= join(split(tolower(str), " "), "_");
4) Replace str1 with {strings.str2}
5) Go to the translation object (/\s+en:\s+{)
6) Add line, and add str2: 'str1'
7) Jump back to the marker (`a)

Can someone explain how to do it the right way so I can map it to a specific key?

Input:

<Text>Lorem Ipsum</Text>
...
strings = {
  en : {

Output:

<Text>{strings.lorem_ipsum}</Text>
...
strings = {
  en : {
    lorem_ipsum : 'Lorem Ipsum'

Upvotes: 0

Views: 112

Answers (1)

XPlatformer
XPlatformer

Reputation: 1218

This works for me:

Put the following in your .vimrc (restart vim) and then visually select Lorem Ipsum and press l

function! Lorem()
    let str1 = getreg("@")
    let str2 = join(split(tolower(str1), " "), "_")
    call append(line('$'), "    " . str2 . " : '" . str1 . "'")
    return "{strings." . str2 . "}"
endfunction

vnoremap l s<C-R>=Lorem()<CR><ESC>

Upvotes: 1

Related Questions