2

Currently I have my vim set up to update the statuslline based on what mode the editor is in. Like if I'm in insert mode then the statusline turns red. Magenta in replace mode. Green in command mode.

" statusline setup
set laststatus=2
set statusline=\
    \ %F\ \|\ %h%w%m%r\ \|\ %=%(\|\ L\:\ %l\ \|\ C\:\ %c%V\ \|\ %=\ %P\ %)
hi statusline cterm=none ctermbg=green ctermfg=black
function! InsertStatuslineColor(mode)
    if a:mode=='i'
        hi statusline ctermbg=red
    elseif a:mode=='r'
        hi statusline ctermbg=magenta
    else
        hi statusline ctermbg=red
    endif
endfunction
au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertChange * call InsertStatuslineColor(v:insertmode)
au InsertLeave * hi statusline ctermbg=green
hi statusline ctermbg=green

I'd like to know if it's possible have vim update an external file based on what mode the editor is currently in. For instance, is it possible to have it change the statusline color and then also perform a shell command in the background? My ultimate goal here is to have my led backlit keyboard match the color of the statusline. I can achieve these color switches via cli like:

echo "FF00FF" > /sys/class/leds/system76::kbd_backlight/color_left

Is there a way to work this into my .vimrc to achieve my goal or nah?

  • 1
    Hi. Did you use the search function up above? Sounds like you are just asking how to execute a shell command from Vim (whether it's from some vimscript or via the : command line it'll be the same thing). There should be many answers that give you guidance on that. – B Layer Jul 13 at 1:59
1

You can use the writefile() function to write to an external file from a Vimscript function or mapping.

(Using external shell commands through ! or the system() function would be another possibility, but sticking to a native feature of Vim is probably better.)

Consider that you'll need to have permissions to write to the aforementioned file from Vim under the user you're running Vim as, I'm not sure whether that will be the case or whether that driver will only allow root to write to it.

Also note that, if you have multiple instances of Vim running and all are using the same mappings, they're likely to fight each other on setting your keyboard lights...

Something like the below (based on your original code) might work:

let g:keyboard_backlight_controller = '/sys/class/leds/system76::kbd_backlight/color_left'
function! InsertStatuslineColor(mode)
    if a:mode=='i'
        hi statusline ctermbg=red
        call writefile(['FF0000'], g:keyboard_backlight_controller)
    elseif a:mode=='r'
        hi statusline ctermbg=magenta
        call writefile(['FF00FF'], g:keyboard_backlight_controller)
    else
        hi statusline ctermbg=red
        call writefile(['FF0000'], g:keyboard_backlight_controller)
    endif
endfunction
function! NormalStatuslineColor()
    hi statusline ctermbg=green
    call writefile(['00FF00'], g:keyboard_backlight_controller)
endfunction
au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertChange * call InsertStatuslineColor(v:insertmode)
au InsertLeave * call NormalStatuslineColor()
call NormalStatuslineColor()
  • 1
    Variables! Pull out the filename and only type it once – D. Ben Knoble Jul 13 at 12:47
  • 1
    @D.BenKnoble Done! Naming is hard though... – filbranden Jul 13 at 13:06
  • 1
    k, this is exactly what I was wanting this works perfectly. And to answer your questions, I already sorted out the file permissions issue and as for multiple instances of vim, you're right, it presents problem that I am aware of and ready to handle. This is not a great thing for everyone to do, I admit, but for my bizarrely specific use case, this works fantastic! – Jon Red Jul 15 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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