开发者

VIM: Replace all occurences of a word in the current C/C++ function

I've to replace all occurrences of a specific macro inside some(only some amongst dozens) C functions. Since the file is thousands of lines long, with several instances of the macro in all the functions, I'd like to replace all occurrences within the parti开发者_运维技巧cular function the cursor is currently placed. I know VIM provides navigation commands (like [[ to go to the beginning of the current function, and then % to find its matching closing brace) , but I can't figure out how to use them to come up with the required search-replace command. Can anyone help ?


Place your cursor on the first opening brace. Then type v% and you will see the function body get highlighted. Then type the replacement command :s/find/replace/g and hit enter. This will replace within the selected function.

Note: You will see you command prompt change to: :'<,'>:s/find/replace/g.


Although I would also recommend dogbane's solution, I thought I'd also mention the NrrwRgn plug-in. It's quite useful for working on a continuous subset of a buffer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜