2

私はvimの自動フォーマットを次のように単一のキーバインドで切り替えようとしています(たとえば、有効にfo+=aなっていない場合は有効にし、そうでない場合は無効にしfo-=aます)。

nnoremap <leader>a "magic goes here"

条件付きのexistscheckを使用することを考えましたが、見つかりませんでした。これどうやってするの?

4

3 に答える 3

3

これは私がすることです:

function! ToggleAutoFormat()
    if -1==stdridx(&fo, 'a')
        set fo+=a
    else
        set fo-=a
    endif
endfunction

nnoremap <leader>a :call ToggleAutoFormat()
于 2013-02-21T22:15:03.140 に答える
3

魔法は以下のスニペットの「&」です

:help expr-option

nnoremap <leader>a call ToggleFormat()

function! toggleFormat()
      if &formatoptions !~ 'a'
          set fo+=a
      else
          set fo-=a
      endif
  return 0 
endfunction
于 2013-02-21T22:17:20.257 に答える
0

いくつかの調整を組み込んだ、Lighthartの回答の更新バージョン:

function! ToggleFormat()
      if &formatoptions !~ 'a'
          set fo+=a
      else
          set fo-=a
      endif
      "" print the value of formatoptions once we're done
      set formatoptions
endfunction

"" End with <CR> to instantly run the function when triggered.
nnoremap <leader>a :call ToggleFormat()<CR>
于 2022-02-16T02:00:47.527 に答える