Do you like the relative line numbers setting?

set relativenumber

I do, but sometimes I’d like to see the actual line numbers when going to a line in a stack trace for example.

Anyway, try

let g:show_relative_numbers = 1

function! ToggleRelativeLineNumber()
  if g:show_relative_numbers
    set number!
  else
    set relativenumber!
  endif

  let g:show_relative_numbers = !g:show_relative_numbers
endfunction

nnoremap ;l :call ToggleRelativeLineNumber()<CR>

Then you can just do:

;l

to swap between relative and normal line numbers