17

I wrote a plugin: https://github.com/sk1418/HowMuch which supports visual selection and does math calculations. By default the plugin supports three math-expression evaluation engines: Gnu bc, python, and vimscript. You can do the calculations on a certain one or let the plugin automatically choose one for you. It works with your example like this: For ...


17

If you don't want to use plugins or drop to a bash script, you can do something like the following: c-V {motions} "ay copy column into "a :let @a = substitute(@a, 'c-V c-J', '+', 'g') replace the column newlines with + ic-R=c-Ra run the replaced "a through the expression register Alternatively: make the expression history entry re-usable for further sums ...


15

Here is why : 42 / 0 tends to +infinity And how does Vim represent the largest number available ? 2147483647 See :h limits Furthermore, the float2nr function documentation states : When the value of {expr} is out of range for a |Number| the result is truncated to 0x7fffffff or -0x7fffffff. NaN results in -0x80000000. So you have here your 2 numbers :...


12

:r!awk '{sum+=$6} END {print "Total: "sum}' % Explanation: :r ........... read (put result in this file) ! ............ external command awk .......... external tool {sum+=$6} .... sixth field (awk considers spaces as field separator) END .......... at the end {print "Total: "sum} --> string "Total: " plus your result % ............ current file I have ...


9

This behavior is documented under eval section: When dividing a Number by zero the result depends on the value: 0 / 0 = -0x80000000 (like NaN for Float) >0 / 0 = 0x7fffffff (like positive infinity) <0 / 0 = -0x7fffffff (like negative infinity) (before Vim 7.2 it was always 0x7fffffff)


9

Basic increment Here is a simple macro to perform the action: :nnoremap <leader>a m`lv$xh<c-a>p`` :nnoremap <leader>x m`lv$xh<c-x>p`` In normal mode you m` Mark your location l move one character to the right v$x cut to the end of the line h move back to the original position <c-a> increment (or decrement) p paste back your ...


7

You can't use a variable directly with :res. Compare the help text for :res, :s, :echo and :exe: :res[ize] -N :res :resize CTRL-W_- CTRL-W - Decrease current window height by N (default 1). :[range]s[ubstitute]/{pattern}/{string}/[flags] [count] For each line in [range] replace a match of {...


7

My csv plugin allows this. Use the :SumCol command and be sure to read the documentation.


6

This seems to work quite well, and does fairly sane things when using <C-a> on a 9 (95 becomes 105), or using <C-x> on a 0 (105 becomes 95): nnoremap g<C-a> :call search('\d', 'c')<CR>a <Esc>h<C-a>lxh nnoremap g<C-x> :call search('\d', 'c')<CR>a <Esc>h<C-x>lxh First, we search for any digit, ...


6

Making a plugin or coding this in vimscript seems a little heavy. I believe in a plugin-free vim, and good composition with external tools. Here is a 1-time command, based on user2571881's, that works even if the buffer has not been saved. :%!awk -F '|' '{print; sum+=$4}; END {print "Total: "sum}' If you want to save this command for future use, you might ...


5

As none of your numbers contains a decimal point (:help floating-point-format), Vim treats the expression as integer division, and also gives an integer result. Simply specify one or both numbers as floats, and you'll get the expected result: <C-R>=7.0/8<CR> gives 0.875. For more control (number of digits, precision, padding), use printf() with ...


5

Here's a version I wrote using a substitute expression: map <c-a> :s/\d/\=submatch(0) < 9 ? submatch(0) + 1 : submatch(0)/<CR> map <c-x> :s/\d/\=submatch(0) > 0 ? submatch(0) - 1 : submatch(0)/<CR> Each one just looks for the first digit character on the line, and adds or subtracts one if it's in the range [0-8] or [1-9], ...


4

If the columns are properly aligned, this can be done with a simple oneliner. first select the column in block-wise visual mode as other answers have demonstrate -> CTRL-V + move the cursor yank the selection with y type: :echo eval(join(split(@", '\_s\+'), '+')) which splits the text yanked on spaces and new lines, rejoin the element with + character, and ...


4

You can use this command: com! -range=% Sum :echo eval(join(getline(<line1>,<line2>),'+')) It basically gets the lines in the given range (by default whole file) as a list, and evaluates it by joining each list item with a + between each other.


4

What worked for me was: :%!awk '{print; total+=$1}END{print total}'


2

It is because vim represents the signed integers on 32 bits which makes that -2147483648 actually is the smallest number in Vim and not -2147483647 as you could think. let foo = -2147483647 echo foo - 1 => -2147483648 let foo = -2147483648 echo foo - 1 => 2147483647 On 32 bits the max value of an integer is 2^31-1 when the min value is -2^31. For an ...


2

This behavior is useful in Calculus when using something called a Limit. Lim n -> 0^+ of 1/n = +inf This can also be written as: As n -> 0^+, 1/n -> +inf It is read like so.. As n approaches zero from the right, the function 1/n approaches positive infinity. To see a visual explanation of this reasoning, pop over to http://www.wolframalpha.com/input/?i=...


1

Here's a fun (albeit slightly hacky) answer, although I believe it's the shortest answer you'll get: :%norm Dj@"<C-v><C-a><cr> p (note that <C-v> means ctrl-v, and <C-a> means ctrl-a) It works by doing the following on every line: Deleting it (into the unnamed register) Running the unnamed register as a count, and ...


1

External cli tool csvstat from csvkit :!csvstat -d '|' -H -c 4 --sum % 69.5 Short explanation of the options -d DELIMITER Delimiting character of the input CSV file. Here |. -H Specify that the input CSV file has no header row. -c COLUMNS A comma separated list of column indices or names to be examined. Defaults to all columns. --sum Only output sums. ...


1

vmap ++ from plugin vmath by Damian Conway Install plugin from github (only 178 sloc) e.g. $ wget https://raw.githubusercontent.com/thoughtstream/Damian-Conway-s-Vim-Setup/master/plugin/vmath.vim -P ~/.vim/pack/manual/start/damians-tools/plugin Add mapping to your vimrc vmap <silent><expr> ++ VMATH_YankAndAnalyse() However, I would suggest ...


Only top voted, non community-wiki answers of a minimum length are eligible