1

I don't know what could do this, sed, vim, whatever. Lets say I have 30 functions, to keep it simple, i'll show 3

    #first function 

    AddOne = [
 a+1==b, b+1==c
]

    # second function 

    AddTwo = [
 a+1==b, b+1==c

]

    # third function 

    MultiplyByTwo =  [
 a+1==b, b+1==c, c+1==d
]


    #Fourth Function

    MultiplyByThree [ 

a+1==b, b+1==c, c+1==d

]

This small example, shows that i have created my functions, the way i wanted to, they are similar to the first function, so i copy pasted, and changed their name and few parameters. all i want to do is to replace the 1 by 2 in the AddTwo function, and replace the plus by the power in the third function and the one by 2, and third function as well, and replace the 1 by 3.

Basically, i have a big file,full of functions, i want to edit these functions on the fly, to make them do what i want them to do

it doesn't have to be fully automated solution. what i'm looking for, is some plugin to some text editor, that would allow me to select a text and use the search and replace tool on that specific function, all editors have search and replace tools, but they search the entire file, i want to search the section of code i select using the mouse

8
  • i have chosen random tags for this question, please feel free to edit, remove, add whatever you want
    – Lynob
    Oct 8, 2014 at 0:15
  • 4
    It appears to me that you'll spend a lot more time automating the process than you will just making the changes manually. Oct 8, 2014 at 0:38
  • From the examples that you have shown, it might be far easier to generate the correct functions from scratch than to do an intelligent search-and-replace on the existing functions.
    – John1024
    Oct 8, 2014 at 1:32
  • @glennjackman it doesn't have to be fully automated solution. what i'm looking for, is some plugin to some text editor, that would allow me to select a text and use the search and replace tool on that specific function, all editors have search and replace tools, but they search the entire file, i want to search the section of code i select using the mouse
    – Lynob
    Oct 8, 2014 at 8:11
  • @John1024 please read my comment above
    – Lynob
    Oct 8, 2014 at 8:14

3 Answers 3

2

Two possibilities in vim:

  1. search and replace in the visual selection: choose a visual selection (v from normal mode), and then use:

      :'<,'>s/1/3/g
    

    you will notice that if you press ":" from visual mode the :'<,'> will appear automatically so you have just to type the search and replace command;

  2. (my preferred one) edit in vim, and when on a number in normal mode, CTRL-A will increment it, CTRL-X will decrement it --- that's one of the most useful shortcut in vim...

4
  • wow upvote from me, i'll try it in a bit and let u know
    – Lynob
    Oct 8, 2014 at 10:16
  • 1
    Rmano: +1 for solution 2. I had not seen that before... -------- I often have to process docs that are more than 2000+ lines long, and I'd like to know whether I can replace '<,'> by line numbers for a given section... E.g. From line 11 to line 497, execute the cmd as :[11-497]s/1/3/g or something like that. Note that my notation :[11-497] is pure fantasy. Anyway an answer would be helpful to avoid having to resort to VISUAL mode. Thanks.
    – Cbhihe
    Feb 26, 2016 at 19:12
  • 1
    @Cbhihe yes --- it's :11,497s/1/3/g, see vim.wikia.com/wiki/Ranges. Quite useful with set number :-)
    – Rmano
    Feb 26, 2016 at 19:29
  • +1 Got it! thanks. I had been looking at vim.wikia.com/wiki/Vim_Tips_Wiki but I generally find that info on vim is difficult to organize hierarchically, and thus to sift thru. It is not structured as in *nix man pages... Must adapt I guess. ;-)
    – Cbhihe
    Feb 27, 2016 at 10:29
2

Not sure if this is what you are looking for.

Below a script with with you can change name, digit and operator of functions by their index (0 = first function).

In the head of the script, you set a list of tuples with the desired settings, like:

[(index, ("NewName"), ("digit"), ("operator")), (index, ("NewName"), ("digit"), ("operator"))]

You can pick any index you'd like to change. If one or more of the properties do not need to be changed, leave the tuple empty.

Example: to only change the name of the function with index 2 (the third function, since 0 = the first):

[(2, ("NewName"), (), ())]

To change the first two functions:

changes = [(0, ("FuncName1"), ("2"), ("+")), (1, ("FuncName2"), ("2"), ("**"))]

Keeping different sets of functions

This script could be mainly of use if you keep different sets of function properties by keeping different changes = lines, and commenting out all except the one you currently need for example.

How to use

Copy the script below into an empty file, set the desired parameters for the functions to change, the path to the file, save it as script.py and run it by:

python3 /path/to/script.py

The script:

#!/usr/bin/env python3

# function / name / digit / operator
# (example)
changes = [(0, ("Name1"), (), ()), (1, ("Name2"), ("2"), ("**"))]

subject_file = "/path/to/file_withfunctions"
operators = ["+", "-", "*"] # ** has been taken care of in the script, you can use it in the "changes" line

with open(subject_file) as sample:
    lines = [line for line in sample.readlines()]

def first_occurrence(index, string, lines):
    area = lines[index:]
    func_line = [[i, area[i]] for i in range(len(area))if string in area[i]][0]
    func_line[0] = func_line[0]+index
    return func_line

functiondata = [[[i, lines[i]],first_occurrence(i, "==", lines)] for i in range(len(lines)) if "[" in lines[i]]

for item in changes:
    subject = functiondata[item[0]]
    if len(item[1]) != 0: # name
        name = subject[0][1].split()[0]
        newname = item[1]
        lines[subject[0][0]] = lines[subject[0][0]].replace(name, newname)
    if len(item[2]) != 0: # digit
        or_line = subject[1][1]
        character = [ch for ch in or_line if ch.isdigit()][0]
        new_line = or_line.replace(character, item[2])
        lines[subject[1][0]] = new_line
    if len(item[3]) != 0: # operator
        or_line = new_line
        if "**" in or_line:
            new_line = or_line.replace("**", item[3])
        else:
            character = [ch for ch in operators if ch in or_line][0]
            lines[subject[1][0]] = new_line
            new_line = or_line.replace(character, item[3])
        lines[subject[1][0]] = new_line

with open(subject_file, "wt") as sample:
    for line in lines:
        sample.write(line)
1

Emacs has a facility that you might find useful. It is called the Keyboard Macro Counter.

For example, if you enter the following sequence while defining a macro C-x C-k C-i C-x C-k C-i C-u C-x C-k C-i C-x C-k C-i the text ‘0112’ is inserted in the buffer, and for the first and second execution of the macro ‘3445’ and ‘6778’ are inserted.

Read more on the EmacsWiki page about Keyboard Macro Counters.

If you've never used emacs keyboard macros to perform interactive search/replace work, you'll also want to read the main page about keyboard macros.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .