File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For 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 )
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments