Turning relativenumber option off/on when opening/closing the gdb terminal window When I was not sure about how to do this, I asked this question on StackOverflow, the body of which is the following:


  As regards line numbers, when doing normal file editing, I prefer to
  have the following setting

set number
set relativenumber

  
  because the former tells me where I am, the latter helps me using
  j and k effectively.
  
  However, when debugging with
  gdb,
  using the termdebug package and the :Termdebug command, I often
  want to set breakpoints; hence, I'd like to turn the latter option
  off, executing the set norelativenumber command on a global scope,
  so that all files I'm editing show the actual line numbers.


Reading the answer, and Vim help pages a bit more, I ended up with the following solution, which is now part of my ~/.vimrc file:

" Source the termdebug plugin
packadd termdebug

" Add mapping to load termdebug
noremap &lt;silent&gt; &lt;Leader&gt;td :call MyTermdebug()&lt;CR&gt;

" turn off relativenumber,
" start Termdebug,
" and create autocmd to turn relativenumber back on
function! MyTermdebug()
  call SetRelNumInAllWin(v:false)
  augroup ClosingDebugger
    au!
    autocmd BufUnload !gdb call SetRelNumInAllWin(v:true)
  augroup END
  Termdebug
endfunction

" set/unset relativenumber in all windows if flag is v:true/v:false
function! SetRelNumInAllWin(flag)
  let current_win_id = win_getid()
  tabdo windo call SetRelNum(a:flag)
  call win_gotoid(current_win_id)
endfunction

" set/unset relativenumber in current window if flag is v:true/v:false
function! SetRelNum(flag)
  if &amp;number
    if a:flag
      set relativenumber
    else
      set norelativenumber
    end
  end
endfunction
Two things off the top of my head:

Use mode-specific mappings, so nnoremap limits the mapping to normal mode. (Great job on the use of non-recursive mappings.)

You can access options as vimscript variables with &amp;relativenumber. SetRelNum is sort of unecessary: I would have written

tabdo windo let &amp;relativenumber = a:flag


(And if that doesn’t work because of some odd scope rule, you can exec it with everything but a:flag in quotes. You may have to pass 0/1 instead of v:false/v:true etc.)