https://stackoverflow.com/questions/25684559/what-is-the-difference-between-g-and-s-commands-in-vim
:g executes a command on all lines that match a regex:
:g/LinesThatMatchThisRegex/ExecuteThisCommand
Example:
:g/hello/d
This will delete (d) all lines that contain hello.On the other hand, :%s just performs a search (on a regex) and replace throughout the file:
:%s/hello/world/g
(The g there means global so it will replace all of them, not just one per line, you can also use the c flag (:%s/hello/world/gc) if you want to confirm each replacement manually).
This replaces all occurrences of hello with world.Both the :g and :%s commands support regular expressions.
The s command means substitute and the % means throughout the buffer. So %s means substitute throughout the entire buffer. You can also give a line range:
:10,15s/hello/world/g
This will execute the search and replace seen earlier on only lines 10 to 15 (inclusive).
What is the difference between :g and :%s commands in vim
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...