From 222c1cf8ead37ae2b1488f3393087af4f4114d6f Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 19 Dec 2015 19:41:11 -0600 Subject: [PATCH] Add Increment All The Numbers as a vim til. --- README.md | 1 + vim/increment-all-the-numbers.md | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 vim/increment-all-the-numbers.md diff --git a/README.md b/README.md index 3a0bb3b..044b492 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Help For Non-Normal Mode Features](vim/help-for-non-normal-mode-features.md) - [Horizontal to Vertical and Back Again](vim/horizontal-to-vertical-and-back-again.md) - [Incremental Searching](vim/incremental-searching.md) +- [Increment All The Numbers](vim/increment-all-the-numbers.md) - [Interactive Buffer List](vim/interactive-buffer-list.md) - [Joining Lines Together](vim/joining-lines-together.md) - [Jump To Matching Pair](vim/jump-to-matching-pair.md) diff --git a/vim/increment-all-the-numbers.md b/vim/increment-all-the-numbers.md new file mode 100644 index 0000000..2812e69 --- /dev/null +++ b/vim/increment-all-the-numbers.md @@ -0,0 +1,34 @@ +# Increment All The Numbers + +Vim's substitution feature can be used for more than simple static text +replacement. Each replacement can be the result of some operation on the +original text. For instance, what if we'd like to increment all numbers in the +buffer? We can achieve this by searching for all numbers and then using `\=` +with `submatch`. Whenever the replacement string of a substitution starts +with `\=`, the remainder of the string is evaluated as an expression. + +Given the following text in our buffer: + +``` +1 2 a b c 45 123 1982 +``` + +We can run the following substitution command: + +``` +:%s/\d\+/\=submatch(0)+1/g +``` + +This will transform all digits in the buffer resulting in: + +``` +2 3 a b c 46 124 1983 +``` + +Want to decrement all the numbers instead? + +``` +:%s/\d\+/\=submatch(0)-1/g +``` + +See `:h sub-replace-expression` for more details.