Skip to content

Commit fefada8

Browse files
committed
Add Sum A Bunch Of Numbers In The Current File as a Vim TIL
1 parent dd13203 commit fefada8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1356 TILs and counting..._
13+
_1357 TILs and counting..._
1414

1515
---
1616

@@ -1529,6 +1529,7 @@ _1356 TILs and counting..._
15291529
- [Split The Current Window](vim/split-the-current-window.md)
15301530
- [Splitting For New Files](vim/splitting-for-new-files.md)
15311531
- [Source Original vimrc When Using Neovim](vim/source-original-vimrc-when-using-neovim.md)
1532+
- [Sum A Bunch Of Numbers In The Current File](vim/sum-a-bunch-of-numbers-in-the-current-file.md)
15321533
- [Swap Occurrences Of Two Words](vim/swap-occurrences-of-two-words.md)
15331534
- [Swapping Split Windows](vim/swapping-split-windows.md)
15341535
- [Swap The Position Of Two Split Windows](vim/swap-the-position-of-two-split-windows.md)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Sum A Bunch Of Numbers In The Current File
2+
3+
Let's say I have a bunch of big numbers on consecutive lines in the file I
4+
currently have open in Vim. Like this:
5+
6+
```
7+
418564
8+
921550
9+
1180181
10+
1234458
11+
2706100
12+
15954945
13+
16254608
14+
```
15+
16+
If I make a visual selection of those numbers and then hit `:`, it will open a
17+
command prompt for the beginning (`'<`) to the end (`'>`) of the visual
18+
selection. I can then shell out those lines to an external command by starting
19+
the command with `!`. The command to shell out to for this scenario is `awk`
20+
which can sum up values from a "file" in a single line.
21+
22+
The whole thing will look like this:
23+
24+
```
25+
:'<,'>!awk '{s+=$1} END {print s}'
26+
```
27+
28+
Hit enter. Then `awk` will produce the sum and replace the highlighted lines
29+
with that value.
30+
31+
```
32+
38670406
33+
```
34+
35+
[source](https://stackoverflow.com/a/450821)

0 commit comments

Comments
 (0)