mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 16:18:01 +00:00
Add Sum A Bunch Of Numbers In The Current File as a Vim TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1356 TILs and counting..._
|
_1357 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1529,6 +1529,7 @@ _1356 TILs and counting..._
|
|||||||
- [Split The Current Window](vim/split-the-current-window.md)
|
- [Split The Current Window](vim/split-the-current-window.md)
|
||||||
- [Splitting For New Files](vim/splitting-for-new-files.md)
|
- [Splitting For New Files](vim/splitting-for-new-files.md)
|
||||||
- [Source Original vimrc When Using Neovim](vim/source-original-vimrc-when-using-neovim.md)
|
- [Source Original vimrc When Using Neovim](vim/source-original-vimrc-when-using-neovim.md)
|
||||||
|
- [Sum A Bunch Of Numbers In The Current File](vim/sum-a-bunch-of-numbers-in-the-current-file.md)
|
||||||
- [Swap Occurrences Of Two Words](vim/swap-occurrences-of-two-words.md)
|
- [Swap Occurrences Of Two Words](vim/swap-occurrences-of-two-words.md)
|
||||||
- [Swapping Split Windows](vim/swapping-split-windows.md)
|
- [Swapping Split Windows](vim/swapping-split-windows.md)
|
||||||
- [Swap The Position Of Two Split Windows](vim/swap-the-position-of-two-split-windows.md)
|
- [Swap The Position Of Two Split Windows](vim/swap-the-position-of-two-split-windows.md)
|
||||||
|
|||||||
35
vim/sum-a-bunch-of-numbers-in-the-current-file.md
Normal file
35
vim/sum-a-bunch-of-numbers-in-the-current-file.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Sum A Bunch Of Numbers In The Current File
|
||||||
|
|
||||||
|
Let's say I have a bunch of big numbers on consecutive lines in the file I
|
||||||
|
currently have open in Vim. Like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
418564
|
||||||
|
921550
|
||||||
|
1180181
|
||||||
|
1234458
|
||||||
|
2706100
|
||||||
|
15954945
|
||||||
|
16254608
|
||||||
|
```
|
||||||
|
|
||||||
|
If I make a visual selection of those numbers and then hit `:`, it will open a
|
||||||
|
command prompt for the beginning (`'<`) to the end (`'>`) of the visual
|
||||||
|
selection. I can then shell out those lines to an external command by starting
|
||||||
|
the command with `!`. The command to shell out to for this scenario is `awk`
|
||||||
|
which can sum up values from a "file" in a single line.
|
||||||
|
|
||||||
|
The whole thing will look like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
:'<,'>!awk '{s+=$1} END {print s}'
|
||||||
|
```
|
||||||
|
|
||||||
|
Hit enter. Then `awk` will produce the sum and replace the highlighted lines
|
||||||
|
with that value.
|
||||||
|
|
||||||
|
```
|
||||||
|
38670406
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](https://stackoverflow.com/a/450821)
|
||||||
Reference in New Issue
Block a user