1

I have the following lines in my .vimrc:

nnoremap <nowait><silent> ¡ :colorscheme tokyonight<CR>
nnoremap <nowait><silent> ™ :colorscheme tone<CR>
nnoremap <nowait><silent> £ :colorscheme darktooth<CR>
nnoremap <nowait><silent> ¢ :colorscheme soft-morning<CR>
nnoremap <nowait><silent> ∞ :colorscheme gruvbox<CR>
nnoremap <nowait><silent> § :colorscheme shirotelin<CR>
hi Normal ctermbg=NONE guibg=NONE
hi! NonText ctermbg=NONE guibg=NONE guifg=NONE ctermfg=NONE

How should I do to make all of these nnoremap mappings to execute the two lines

hi Normal ctermbg=NONE guibg=NONE
hi! NonText ctermbg=NONE guibg=NONE guifg=NONE ctermfg=NONE

at the end?

1 Answer 1

3

You have several ways to do it:

  • You can use <bar> which is the key code for | to be used in a mapping (Do not use | directly in your mapping):

    nnoremap <leader>a :echo "fizz" <bar> echo "buzz"<CR>
    

    This is the equivalent of running :echo "fizz" | echo "buzz" in the command line.

  • You can also map it has two normal mode commands:

    nnoremap <leader>a :echo "fizz"<CR>:echo "buzz"<CR>
    

    Which is the equivalent of running :echo "fizz" then :echo "buzz"

  • If things get more complicated I'd go with a function which makes it easier to group your commands and add logic if needed.

    function! MyFunction()
        colorscheme tokyonight
        hi Normal ctermbg=NONE guibg=NONE
    endfunction
    
    nnoremap <leader>a :call MyFunction()<CR>
    
  • And to keep it more concise you could even use a user defined command:

    function! MyFunction()
        colorscheme tokyonight
        hi Normal ctermbg=NONE guibg=NONE
    endfunction
    
    command! ColorschemeCommand call MyFunction
    
    nnoremap <leader>a :ColorschemeCommand<CR>
    

You can even lookup :h :command-nargs to add arguments to your commands so that you can reuse the same command with different arguments for all of your mappings.


Edit

As Ben pointed out in the comments, you particular use case could also benefit from the autocommand event :h ColorScheme. If you are not familiar with autocommands they are a built-in way to run commands after a trigger (here after loading a colorscheme). See :h autocmd for more details.

5
  • 1
    or autocmd ColorScheme
    – D. Ben Knoble
    Apr 12, 2021 at 11:25
  • Indeed in this particular case that also works.
    – statox
    Apr 12, 2021 at 11:27
  • Thank you so much! I take the third approach while I have to write function! instead or I will get error. Is the reason because I need to call it many times? Apr 12, 2021 at 11:36
  • 1
    @job_start I edited my answer to add the !: This is often needed ! when you create a function. This allows to redefine a function with the same name which can happen when you source your vimrc several time. See :h E122
    – statox
    Apr 12, 2021 at 11:49
  • 1
    @job_start (somewhat) recent vim versions have made an exception so that ! is not necessary when the same script that defines the function overwrites it (so that re-sourcing works). So it shouldn’t be necessary… obviously, whatever works for you is preferable.
    – D. Ben Knoble
    Apr 12, 2021 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.