Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Random 脚本以查找字符串并将其替换为随机字符和数字_Random_Vim_Replace - Fatal编程技术网

Random 脚本以查找字符串并将其替换为随机字符和数字

Random 脚本以查找字符串并将其替换为随机字符和数字,random,vim,replace,Random,Vim,Replace,是否可以为vim编写一个脚本,以便它查找字符串,例如GKFJJFK8483JD7。然后用另一个随机生成的字符串替换找到的字符串?它必须能够生成随机的数字字符串和其他字符 如果有人能帮我写这样的剧本,我将不胜感激 给你: function! ReplaceWithRandom(search) " List containing the characters to use in the random string: let characters = ['1', '2', '3', '

是否可以为vim编写一个脚本,以便它查找字符串,例如GKFJJFK8483JD7。然后用另一个随机生成的字符串替换找到的字符串?它必须能够生成随机的数字字符串和其他字符

如果有人能帮我写这样的剧本,我将不胜感激

给你:

function! ReplaceWithRandom(search)
    " List containing the characters to use in the random string:
    let characters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]

    " Generate the random string
    let replaceString = ""
    for i in range(1, len(a:search))
        let index = GetRandomInteger() % len(characters)
        let replaceString = replaceString . characters[index]
    endfor

    " Do the substitution
    execute ":%s/" . a:search . "/" . replaceString . "/g"

endfunction

function! GetRandomInteger()
    if has('win32')
        return system("echo %RANDOM%")
    else
        return system("echo $RANDOM")
    endif
endfunction
该函数可以这样调用:
:调用ReplaceWithRandom(“stringtoreplace”)
。它将把作为参数传递的字符串的所有出现处替换为由
characters
中列出的字符组成的随机字符串

注意,我包括了一个helper函数,它从系统中获取随机数,因为Vim不提供随机生成器

作为奖励,您可以将其作为固定通话的命令:

command! -nargs=1 RWR call ReplaceWithRandom(<f-args>)
function! ReplaceWithRandom(search)
    " List containing the characters to use in the random string:
    let characters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]


    " Save the position and go to the top of the file
    let cursor_save = getpos('.')
    normal gg

    " Replace each occurence with a different string
    while search(a:search, "Wc") != 0
        " Generate the random string
        let replaceString = ""
        for i in range(1, len(a:search))
            let index = GetRandomInteger() % len(characters)
            let replaceString = replaceString . characters[index]
        endfor

        " Replace
        execute ":s/" . a:search . "/" . replaceString 
    endwhile

    "Go back to the initial position
    call setpos('.', cursor_save)

endfunction