or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/coc.nvim@0.0.x

docs

index.md
tile.json

tessl/npm-coc-nvim

tessl install tessl/npm-coc-nvim@0.0.0

LSP based intellisense engine for neovim & vim8.

window-management.mddocs/ui/

Window Management [Advanced]

Complexity: Advanced | Category: UI

Common Tasks: Check window state | Get window info | Control window size | Find windows

Window management utilities for working with Vim/Neovim windows, including querying window properties, managing window variables, and controlling window state.

Table of Contents

  • Common Tasks
  • Window Identification
  • Cursor Position
  • Window Visibility
  • Window Variables
  • Float Window Detection
  • Window Dimensions
  • Window Lookup
  • Window Control
  • Configuration
  • Usage Examples
  • Error Handling
  • Troubleshooting
  • See Also

Common Tasks

Get Window Tab Number

let tabnr = coc#window#tabnr(winid)

Find which tab contains a window.

Check Window Visibility

if coc#window#visible(winid)
  echo "Window is visible"
endif

Set Window Height

call coc#window#set_height(winid, 20)

Find Window by Variable

let winid = coc#window#find('my_marker', 'value')

Window Identification

coc#window#tabnr()

coc#window#tabnr(winid)
" Returns: number

Get tabpagenr of winid, return -1 if window doesn't exist.

Parameters:

  • winid: Window ID to query

Returns: Tab page number (1-indexed) or -1 if window invalid

Example:

let winid = win_getid()
let tabnr = coc#window#tabnr(winid)
if tabnr != -1
  echo 'Window is on tab' tabnr
endif

Cursor Position

coc#window#get_cursor()

coc#window#get_cursor(winid)
" Returns: list

Get cursor position in a window.

Parameters:

  • winid: Window ID to query

Returns: [line, column] array (1-indexed line, 0-indexed column)

Example:

let winid = win_getid()
let [line, col] = coc#window#get_cursor(winid)
echo 'Cursor at line' line 'column' col

Window Visibility

coc#window#visible()

coc#window#visible(winid)
" Returns: boolean

Check if window is visible on current tabpage.

Parameters:

  • winid: Window ID to check

Returns: 1 if visible, 0 if not visible or window invalid

Example:

let winid = g:my_float_window
if coc#window#visible(winid)
  echo 'Window is visible'
endif

Window Variables

coc#window#get_var()

coc#window#get_var(winid, name, ...)
" Returns: any

Get window variable or option value. Return v:null when name or window doesn't exist.

Parameters:

  • winid: Window ID
  • name: Variable name (use '&name' for options)
  • default: Optional default value if variable doesn't exist

Returns: Variable value or v:null/default if not found

Example:

" Get window variable
let winid = win_getid()
let value = coc#window#get_var(winid, 'my_var', 'default')

" Get window option
let wrap = coc#window#get_var(winid, '&wrap')

coc#window#set_var()

coc#window#set_var(winid, name, value)

Set window variable or option value (doesn't throw on error).

Parameters:

  • winid: Window ID
  • name: Variable name (use '&name' for options)
  • value: Value to set

Example:

" Set window variable
call coc#window#set_var(winid, 'my_var', 42)

" Set window option
call coc#window#set_var(winid, '&number', 1)

Float Window Detection

coc#window#is_float()

coc#window#is_float(winid)
" Returns: boolean

Check if window is a floating window.

Parameters:

  • winid: Window ID to check

Returns: 1 if floating window, 0 otherwise

Example:

let winid = win_getid()
if coc#window#is_float(winid)
  echo 'Current window is a float'
endif

Window Dimensions

coc#window#set_height()

coc#window#set_height(winid, height)

Set window height.

Parameters:

  • winid: Window ID
  • height: Height in lines

Example:

let winid = win_getid()
call coc#window#set_height(winid, 20)

coc#window#adjust_width()

coc#window#adjust_width(winid)

Automatically adjust window width based on buffer content. Sets width to fit longest line, up to g:coc_max_treeview_width.

Parameters:

  • winid: Window ID

Example:

call coc#window#adjust_width(g:my_tree_winid)

Window Lookup

coc#window#find()

coc#window#find(key, val)
" Returns: number

Find window by window variable (current tab only).

Parameters:

  • key: Variable name to search
  • val: Variable value to match

Returns: Window ID if found, -1 otherwise

Example:

" Find window with specific marker
let winid = coc#window#find('coc_float_kind', 'hover')

coc#window#bufnrs()

coc#window#bufnrs()
" Returns: list

Get list of visible buffer numbers.

Returns: Array of buffer numbers currently displayed in windows

Example:

" Get all visible buffers
let visible_buffers = coc#window#bufnrs()
for bufnr in visible_buffers
  echo 'Buffer' bufnr 'is visible'
endfor

Window Control

coc#window#close()

coc#window#close(winid)

Close window safely (doesn't throw errors).

Parameters:

  • winid: Window ID to close

Example:

call coc#window#close(g:coc_last_float_win)

coc#window#visible_range()

coc#window#visible_range(bufnr)
" Returns: list or v:null

Get visible line range for a buffer.

Parameters:

  • bufnr: Buffer number

Returns: [topline, botline] or v:null if buffer not visible

Example:

let range = coc#window#visible_range(bufnr('%'))
if range isnot v:null
  let [top, bot] = range
  echo 'Lines' top 'to' bot 'are visible'
endif

coc#window#visible_ranges()

coc#window#visible_ranges(bufnr)
" Returns: list

Get all visible line ranges for a buffer across all windows.

Parameters:

  • bufnr: Buffer number

Returns: Array of [topline, botline] pairs for each window showing buffer

Example:

" Get all visible ranges if buffer shown in multiple windows
let ranges = coc#window#visible_ranges(bufnr('%'))
for [top, bot] in ranges
  echo 'Visible range:' top '-' bot
endfor

Configuration

g:coc_max_treeview_width

let g:coc_max_treeview_width = 40

Maximum width for tree view windows when using coc#window#adjust_width().

Type: Number Default: 40

Example:

" Allow wider tree views
let g:coc_max_treeview_width = 60

Usage Examples

Window State Checker

function! CheckWindowState(winid) abort
  if !coc#window#visible(a:winid)
    echo 'Window not visible'
    return
  endif

  let tabnr = coc#window#tabnr(a:winid)
  let [line, col] = coc#window#get_cursor(a:winid)
  let is_float = coc#window#is_float(a:winid)

  echo 'Tab:' tabnr
  echo 'Cursor:' line ':' col
  echo 'Float:' is_float
endfunction

nnoremap <silent> <leader>ws :call CheckWindowState(win_getid())<CR>

Smart Window Closer

function! SmartCloseWindow() abort
  let winid = win_getid()

  " Don't close if it's the last window
  if winnr('$') == 1
    echo 'Cannot close last window'
    return
  endif

  " Get window type
  let is_float = coc#window#is_float(winid)

  if is_float
    call coc#window#close(winid)
    echo 'Closed float window'
  else
    close
  endif
endfunction

nnoremap <silent> <leader>q :call SmartCloseWindow()<CR>

Window Variable Manager

function! SetWindowMarker(marker, value) abort
  let winid = win_getid()
  call coc#window#set_var(winid, a:marker, a:value)
  echo 'Set' a:marker 'to' a:value
endfunction

function! GetWindowMarker(marker) abort
  let winid = win_getid()
  let value = coc#window#get_var(winid, a:marker)
  if value is v:null
    echo 'Marker not found'
  else
    echo a:marker . ':' value
  endif
endfunction

command! -nargs=+ SetMarker call SetWindowMarker(<f-args>)
command! -nargs=1 GetMarker call GetWindowMarker(<q-args>)

Find and Focus Window

function! FindAndFocusWindow(marker, value) abort
  let winid = coc#window#find(a:marker, a:value)

  if winid == -1
    echo 'Window not found'
    return
  endif

  let tabnr = coc#window#tabnr(winid)
  if tabnr != tabpagenr()
    execute 'tabnext' tabnr
  endif

  call win_gotoid(winid)
  echo 'Switched to window' winid
endfunction

nnoremap <silent> <leader>fw :call FindAndFocusWindow('buftype', 'terminal')<CR>

Visible Buffer Lister

function! ListVisibleBuffers() abort
  let buffers = coc#window#bufnrs()

  echo '=== Visible Buffers ==='
  for bufnr in buffers
    let name = bufname(bufnr)
    let name = empty(name) ? '[No Name]' : name
    echo 'Buffer' bufnr . ':' name
  endfor
endfunction

command! VisibleBuffers call ListVisibleBuffers()

Dynamic Window Height

function! SetWindowHeightByLines() abort
  let winid = win_getid()
  let bufnr = winbufnr(winid)
  let lines = line('$', winid)

  " Set height to content, max 20 lines
  let height = min([lines, 20])
  call coc#window#set_height(winid, height)

  echo 'Set window height to' height
endfunction

command! AutoHeight call SetWindowHeightByLines()

Window Options Manager

function! ToggleWindowOption(option) abort
  let winid = win_getid()
  let current = coc#window#get_var(winid, '&' . a:option)

  if current is v:null
    echo 'Option not found'
    return
  endif

  let new_value = !current
  call coc#window#set_var(winid, '&' . a:option, new_value)
  echo 'Toggled' a:option 'to' new_value
endfunction

nnoremap <silent> <leader>tn :call ToggleWindowOption('number')<CR>
nnoremap <silent> <leader>tw :call ToggleWindowOption('wrap')<CR>

Visible Range Highlighter

function! HighlightVisibleRange() abort
  let bufnr = bufnr('%')
  let range = coc#window#visible_range(bufnr)

  if range is v:null
    echo 'Buffer not visible'
    return
  endif

  let [top, bot] = range
  echo 'Visible lines:' top 'to' bot

  " Could add highlighting here
endfunction

command! ShowRange call HighlightVisibleRange()

Multi-Window Buffer Tracker

function! TrackBufferWindows(bufnr) abort
  let ranges = coc#window#visible_ranges(a:bufnr)

  if empty(ranges)
    echo 'Buffer' a:bufnr 'not visible'
    return
  endif

  echo 'Buffer' a:bufnr 'visible in' len(ranges) 'window(s)'
  for i in range(len(ranges))
    let [top, bot] = ranges[i]
    echo (i + 1) . '. Lines' top 'to' bot
  endfor
endfunction

command! -nargs=? TrackBuffer call TrackBufferWindows(<q-args> == '' ? bufnr('%') : str2nr(<q-args>))

Float Window Filter

function! GetNonFloatWindows() abort
  let all_wins = range(1, winnr('$'))
  let normal_wins = []

  for winnr in all_wins
    let winid = win_getid(winnr)
    if !coc#window#is_float(winid)
      call add(normal_wins, winid)
    endif
  endfor

  return normal_wins
endfunction

function! CloseAllFloats() abort
  let all_wins = range(1, winnr('$'))

  for winnr in all_wins
    let winid = win_getid(winnr)
    if coc#window#is_float(winid)
      call coc#window#close(winid)
    endif
  endfor

  echo 'Closed all floating windows'
endfunction

command! CloseFloats call CloseAllFloats()

Tree View Width Manager

function! ManageTreeWidth() abort
  " Find tree view window
  let winid = coc#window#find('coc_explorer', 1)

  if winid == -1
    echo 'Tree view not found'
    return
  endif

  " Auto-adjust width
  call coc#window#adjust_width(winid)
  echo 'Adjusted tree view width'
endfunction

command! AdjustTree call ManageTreeWidth()

Window Tab Navigator

function! NavigateToWindowTab(winid) abort
  let tabnr = coc#window#tabnr(a:winid)

  if tabnr == -1
    echohl Error
    echo 'Window does not exist'
    echohl None
    return
  endif

  if tabnr != tabpagenr()
    execute 'tabnext' tabnr
  endif

  " Focus the window
  for winnr in range(1, winnr('$'))
    if win_getid(winnr) == a:winid
      execute winnr . 'wincmd w'
      break
    endif
  endfor
endfunction

Error Handling

Window Not Found

let winid = coc#window#find('marker', 'value')
if winid == -1
  echohl WarningMsg
  echo 'Window not found'
  echohl None
else
  call win_gotoid(winid)
endif

Invalid Tab Number

let tabnr = coc#window#tabnr(winid)
if tabnr == -1
  echohl Error
  echo 'Window no longer exists'
  echohl None
  return
endif

Variable Not Set

let value = coc#window#get_var(winid, 'my_var')
if value is v:null
  " Use default value
  let value = 'default'
endif

Safe Window Close

function! SafeClose(winid) abort
  if !coc#window#visible(a:winid)
    return
  endif

  if winnr('$') <= 1
    echo 'Cannot close last window'
    return
  endif

  call coc#window#close(a:winid)
endfunction

Troubleshooting

Window ID Changes

  • Window IDs can change when windows are created/destroyed
  • Always verify window exists with coc#window#visible()
  • Store window variables as markers instead of IDs

Tab Switching Issues

  • Use coc#window#tabnr() before accessing window
  • Check if tab number is valid (-1 means not found)
  • Consider using autocmds to track tab changes

Window Options Not Applying

  • Verify option name is correct (use '&' prefix)
  • Check if window supports the option
  • Use coc#window#get_var() to verify setting

Float Window Detection

  • Not all popup windows are floating windows
  • Use coc#window#is_float() for reliable detection
  • Check window configuration in float creation

Visible Range Accuracy

  • Range depends on current window scroll position
  • May change during buffer updates
  • Use visible_ranges() for multiple window views

See Also

  • Float Windows - Floating window management
  • UI Functions - Additional UI utilities
  • Configuration - Window configuration settings