0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

今日のスクリプト(gVim):ウィンドウの位置と幅(2)

Last updated at Posted at 2020-04-02

Windowの位置と幅,高さを任意に変更します.

◆やりたいこと

Windowの位置と幅,高さを変更するスクリプト(前回アップしたのはこちら → 「今日のスクリプト: (Vim) Windowサイズと位置の調整」)の拡張です.

前のバージョンは決め打ちでした.
• SetLines()でフル行数か直前か
• SetWidth()で幅いっぱいか直前のトグル

これはこれでシンプルでいいです.
が,固定の位置,幅,高さに戻したいことがあります.

それで,それぞれの関数に引数をもたせました.
ただ,引数がない時のエラーがうざかったので,
**「引数 に '0'を入れるとトグル」**になるようにしました.

◆まずは高さ:SetLines(N) 関数

""" (Toggle)Set win lines Full <-> previous
"""
map <Leader>j :call SetLines(0)
map ,wj :call SetLines(0)
""
""" SetLines(N) -- N=0 --> Toggle, N: line num
function! SetLines(nlin)
if a:nlin == 0
if !exists("g:oline")
let g:oline = &lines
let &lines=999
else
let &lines = g:oline
unlet g:oline
endif
else
let &lines = a:nlin
endif
endfunction

◆幅: SetWidth(N) 関数

"
"""""" (Toggle)Set win width Full <-> previous
"
map <Leader>@ :call SetWidth(0)
map ,w@ :call SetWidth(0)
map ,wl :call SetWidth(0)
""
"""""" SetWidth(N) function. N=0 --> Toggle / N --> width number
"" command! -nargs=+ SetWidth call SetWidth()
function! SetWidth(nwide)
if a:nwide == 0
if !exists("g:owide")
let g:owide = &columns
let &columns=999
else
let &columns = g:owide
unlet g:owide
endif
else
let &columns = a:nwide
endif
endfunction

◆3つの関数をcall → ひとつのキーバインドに割付け: ",w1"

""
"" Set winpos to Initial pos
""
""  初期値を入れておく.
let iniposx = 160
let iniposy = 20
let iniwide = 90
let inilines = 35
map <Leader>1 :call SetWinpos(iniposx,iniposy)
map ,w1 :call SetWinpos(iniposx,iniposy):call SetWidth(iniwide):call SetLines(inilines)
""
"" iniposx.... はunletしたほうが行儀が良い?そんなことしらん.
""

◆ポジション関数は変更なし: SetWinpos(x,y)

""
"" set winpos to 2nd Display
let newposx = -1180
let newposy = -980
map <Leader>w :call SetWinpos(newposx, newposy)
map ,ww :call SetWinpos(newposx, newposy)
""
"""""" (Toggle)Set win Position to x,y <-> previous
command! -nargs=+ SetWinpos call SetWinpos()
function! SetWinpos(posx, posy)
if !exists("g:oposx")
let g:oposx = getwinposx()
let g:oposy = getwinposy()
echo "winpos "a:posx . " " . a:posy
execute "winpos " a:posx . " " . a:posy
else
echo "winpos "g:oposx . "\  " . g:oposy
execute "winpos " g:oposx . " " . g:oposy
unlet g:oposx g:oposy
endif
endfunction

スクリプトは以上です.

◆呼び出し方をまとめると,

  • 全幅トグル: ,wl
  • 全高トグル: ,wj
  • 別ポジション: ,ww
  • 初期ポジション: ,w1

ま,キーバインドはご自由に.

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?