randomfox: (Default)
[personal profile] randomfox
This Vim macro toggles between linebreak and nolinebreak mode. In linebreak mode, lines are wrap at word boundaries. In addition, the macro maps the Up, Down, Home, and End keys so that they behave more naturally in linebreak mode.


" Function for changing options so that we can edit paragraphs without
" a line break.
function! Set_no_lbr()
    " Break lines onscreen at words.
    set linebreak
    " Map up and down keys to line-oriented up and down.
    nnoremap <Up> gk
    vnoremap <Up> gk
    inoremap <Up> <C-o>gk
    nnoremap <Down> gj
    vnoremap <Down> gj
    inoremap <Down> <C-o>gj
    " Map home and end keys to line-oriented home and end.
    nnoremap <Home> g0
    vnoremap <Home> g0
    inoremap <Home> <C-o>g0
    nnoremap <End> g$
    vnoremap <End> g$
    inoremap <End> <C-o>g$
endfunction

" Reset all the options set by Set_no_lbr()
function! Reset_no_lbr()
    " Do not break lines at words.
    set nolinebreak
    " Remove mappings for the up and down keys.
    nunmap <Up>
    vunmap <Up>
    iunmap <Up>
    nunmap <Down>
    vunmap <Down>
    iunmap <Down>
    " Remove mappings for the home and end keys.
    nunmap <Home>
    vunmap <Home>
    iunmap <Home>
    nunmap <End>
    vunmap <End>
    iunmap <End>
endfunction

function! Toggle_no_lbr()
    if &linebreak
	call Reset_no_lbr()
    else
	call Set_no_lbr()
    endif
    set linebreak?
endfunction

" F3 toggles no-linebreak mode
nmap <F3> :call Toggle_no_lbr()<cr>
vmap <F3> <esc>:call Toggle_no_lbr()<cr>
imap <F3> <esc>:call Toggle_no_lbr()<cr>

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

randomfox: (Default)
randomfox

November 2012

S M T W T F S
    123
45678910
11121314151617
18192021222324
25262728 2930 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 12th, 2026 03:02 pm
Powered by Dreamwidth Studios