randomfox (randomfox) wrote,
randomfox
randomfox

Vim: Toggle between linebreak and nolinebreak mode

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>

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments