Vimscript: how pass variable/float into string?
vim, vim-plugin
Solution
The problem is that you've calculated the ratio as a float type, but the `:resize` command only takes an integer value, as it evidently has to deal with whole lines / columns.
To convert, you can use the built-in `float2nr()` function, optionally after using `round()` on the float value first.
Problem
After reading the manuals about Vimscript, I wanted to creating my own Vim script. I thought it would be nice to have splitted windows in Vim with golden ratio from Wikipedia. When you're switching to another window in vsplit, you will get the golden ratio. To achieve that, I created this Vimscript. ``` function! GoToLeftWindow() execute "vertical resize 109" execute "wincmd h" endfunction function! GoToRightWindow() execute "vertical resize 109" execute "wincmd l" endfunction function! CreateNewWindow() execute "vsplit" execute "wincmd h" execute "vertical resize 109" execute "wincmd l" endfunction nnoremap <A-n> :call CreateNewWindow()<CR> nnoremap <silent> <left> :call GoToLeftWindow()<CR> nnoremap <silent> <right> :call CurrentWin()<CR> ``` So, when I have vertical windows, I switch from one window to another window, I will always have the golden ratio in the active window.Sounds nice.:) But I noticed when I'm working on different desktops, with different resolutions, the golden ratio is gone. To prevent that,I decide that Vim lets calculate the golden ratio. So on every system with different font size, you will always have the golden ratio. To make this possible, I learned more about Vimscript, and create this calculation: ``` function! GoToRightWindow() let curWin= winwidth(0) lockvar curWin let golden_ratio = 1.618 let result = curWin / golden_ratio let ratio = curWin - result execute "vertical resize " . ratio execute "wincmd l" endfunction ``` winwidth(0) (see :echo winwidth(0), is the width of your current window (0).) But I get the error 806, that there is a float in my string. So Vim don't let me use a Float as a String when concatenating.I tried to convert it to string with str2float(ratio), but it gives an error too. And propably my concentation is faulty too. Anyone have a suggestion, how I could get concatening the float with a string? Thanks in advance.