Skip to content

vim-scripts/GlobalOptions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

This is a mirror of http://www.vim.org/scripts/script.php?script_id=4414

DESCRIPTION
For some Vim options, it may be helpful to make a particular setting only for
certain buffers or windows. Some options offer this, but when the option is
only globally scoped, this cannot be easily done.
For example, the 'scrolloff' option can be set to a high value to effectively
center the current line in the middle of the window. But one may only want
this for certain filetypes, or only in a particular window.

HOW IT WORKS
When a buffer- or window-local global option is set, autocmds are set up that
modify the global option depending on the currently active buffer / window.
Because there can be buffer-local autocmds, but not window-local ones, the
approach taken is slightly different.

USAGE
:SetBufferLocal
                        Show all global options made buffer-local.
:SetBufferLocal {option}?
                        Show the buffer-local value of global {option} (if
                        set).
:SetBufferLocal {option}={value}
                        Set the global {option} to {value}, but only in the
                        current buffer. Effectively turns {option} into a
                        buffer-local option.
:SetBufferLocal {option}<
                        Remove the buffer-local value of {option}, so that the
                        global value will be used. Like :setlocal {option}<

:SetWindowLocal
                        Show all global options made window-local.
:SetWindowLocal {option}?
                        Show the window-local value of global {option} (if
                        set).
:SetWindowLocal {option}={value}
                        Set the global {option} to {value}, but only in the
                        current window. Effectively turns {option} into a
                        window-local option.
:SetWindowLocal {option}<
                        Remove the window-local value of {option}, so that the
                        global value will be used. Like :setlocal {option}<

Alternatively, you can also directly use the following API functions.
Note that these do less error checking than above commands.
GlobalOptions#SetBufferLocal({option}, {value})
GlobalOptions#ClearBufferLocal({option})
GlobalOptions#SetWindowLocal({option}, {value})
GlobalOptions#ClearWindowLocal({option})

EXAMPLE
Place the current line in the middle of the window by setting 'scrolloff' (a
global option) to a large value, but only for the current window:
    :SetWindowLocal scrolloff=999

Place the current line in the middle of the window by setting 'scrolloff' (a
global option) to a large value, but only for a certain filetype (best placed
into ~/.vim/ftplugin/{filetype}.vim):
    :call GlobalOptions#SetBufferLocal('scrolloff', 999)
    :let b:undo_ftplugin = 'call GlobalOptions#ClearBufferLocal("scrolloff")'
Note that we use the API function in the undo command to avoid an error when
the buffer-local option has already been unset. (And also for setting it, to
be consistent and because it's slightly more efficient.)

About

Turn global options into buffer- or window-local ones.

Resources

Stars

Watchers

Forks

Packages

No packages published