0

My .vimrc code

autocmd FileType cpp nnoremap <F4> :RunWithFile 
autocmd FileType cpp nnoremap <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<s-left><s-left><s-left><s-left><s-left><left>

When i need to use any of the two, i have to uncomment/comment out every time.
opening my vimrc file and editing it.. such a drag
finally i intend to do something about it.

Can anyone help me make a config in vimrc which toggles these (almost same) commands?

PS: i searched about this everywhere. yet couldn't find a way how to do that.
I did find a thread Toggle function in vim in stackoverflow but solution is to make a plugin (which i don't want)

therefore, it's not a keymap which i can just unmap and it's done. You can see from my vimrc, those two are same mapping but slightly different (additional commands)

5
  • 1
    What criteria determine whether to use one config or the other? And are you looking to swap between the two while working on an individual file or just have the swap take place the next time the autocommand is triggered?
    – B Layer
    Sep 6 at 8:38
  • @BLayer i want to swap between the two while working on an individual file
    – Mega Bang
    Sep 6 at 8:47
  • @BLayer your code was good.. only problem is that only after typing \X i could access to <F4> .. :RunWithFile was not by default .. but toggle was great :)
    – Mega Bang
    Sep 6 at 10:00
  • how can i fix it? You deleted your answer right after you gave me a hint
    – Mega Bang
    Sep 6 at 10:09
  • forgot to tell you.. maxim-kim's code doesn't work
    – Mega Bang
    Sep 6 at 10:58
0

If I understand correctly, Maxim Kim's solution was close to perfect, the only missing thing is that the exact command to run should be specific to the current project. IOW, the value to use should be buffer local.

As this is something I do a lot in my plugins, I've factorized everything regarding option fetching to option toggling (I also have a :Toggle command).

Anyway. A poor man's solution could be the following.

" ~/ftplugin/cpp/cpp_yourmappings.vim
" When I have several ft-specific things to define I avoid polluting the .vimrc

" Little helper that simplifies my lh#option#get() command
" See https://old.reddit.com/r/vim/comments/p5zgdp/can_someone_explain_variables_to_me/h9cx7f5/
let s:GET_OR = { key, default -> filter([w:, b:, t:, g:, {key : default}], {_,v -> has_key(v, key)})[0][key]}

let s:k_option_name = 'run_cpp_with_options'

" yap + p from Maxim Kim's solution
function! s:RunCpp() abort
    let with_options = s:GET_OR(s:k_option_name, 0)
    if with_options
        let cmd = ":RunWithFile ~/Downloads/ > output.txt && cat output.txt\<s-left>\<s-left>\<s-left>\<s-left>\<s-left>\<left>"
    else
        let cmd = ":RunWithFile"
    endif

    call feedkeys(cmd)
endfunction

function! s:toggle(scope) abort
    let dict = a:scope == 'b' ? (b:) : (g:)
    let dict[s:k_option_name] = ! s:GET_OR(s:k_option_name, 0)
    echo printf('%s:%s <- %s', a:scope, s:k_option_name, dict[s:k_option_name])
    " I haven't tried to handle cases like b:foo and g:foo exist and we want to toggle g:foo
endfunction

" To execute
nnoremap <buffer> <silent> <F4> :<c-u>call <sid>RunCpp()<cr>
" To toggle locally
nnoremap <buffer> <silent> <F5> :<c-u>call <sid>toggle('b')<cr>
" To toggle globally, without resetting any previously existing b:values
nnoremap <buffer> <silent> <F6> :<c-u>call <sid>toggle('g')<cr>

Note: I haven't tested the code. If the feedkeys() part doesn't work, I'll revert to old solutions that always work: a simple return cmd + <c-r>= should do. (I've learned to avoid feedkeys() but I've trusted previous answer)

12
  • it doesn't work in my neovim v0.5.0 .. Could you please rewrite blayer's code as i mentioned?
    – Mega Bang
    Sep 6 at 11:37
  • Could you paste me the error message? Sep 6 at 11:37
  • i removed <silent> from the mapping and used.. but still it shows no error.. does nothing
    – Mega Bang
    Sep 6 at 11:39
  • I've just tested it with nvim 0.4.4 where it works fine. Do you at least have the (new!, see the edit) log on <F5>? If not may be it's because I've replaced clumsy autocommands with a filetype plugin that needs to be dropped in the right configuration directory (that I don't know precisely w/ nvim). ftplugins have to be enabled, and the file needs to be put into the right place. Sep 6 at 12:56
  • 1
    Tested with Linux v0.5.0 appimage as well. Sep 6 at 13:02

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.