3

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.

Julius Martin
  • 159
  • 2
  • 8
  • 1
    Other people have implemented this idea before ... [golden-ratio](http://www.vim.org/scripts/script.php?script_id=3690), [GoldenView.vim](http://www.vim.org/scripts/script.php?script_id=4529). It always helps searching around vim.org. – glts Jul 12 '14 at 21:24
  • 1
    I knew that plugin. But it have issues with my setup. And the author is not responding. So that's why I decided to write my own script. Thanks forYour reply anyway!:) – Julius Martin Jul 12 '14 at 21:42
  • glts, Whoa.I was commenting about GoldenView, but the golden-ratio worked fine here! Many many many thanks! Really! – Julius Martin Jul 12 '14 at 21:51

2 Answers2

9

string() converts variables in to string type.

:help string()
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
ways
  • 306
  • 1
  • 3
4

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324