I always used [[to go to the beginning of the function, and then use% to go to the end of the function. I used mt and mb to mark the top and bottom of the function respectively. Then to search and replace in the marked upper and lower lines : 't,' bs / pattern / newpattern / g . It always worked for me. I am sure you can create a macro for this.
Visual selection (vi}) is much simpler and faster. He knows the position of the cursor. So, if the cursor is inside the fucntion subunit, then vi} selects all the rows in this block. If you want to select the entire function, you need to place the cursor outside the sub-blocks, and then vi}. This is great for function blocks that fit into the current window. For functions that are outside the current window, the selection is lost after scrolling up.
I really like the vi vi visual choice because it is much simpler and faster, but sometimes I have to resort to the old school method.
source share