0

related to this post, How to make a toggle function in .vimrc that toggles multiple configs of almost same type?

@BLayer's code

func! MapRunCmd() abort
    if exists("b:rwfcmd1") && b:rwfcmd1
        nnoremap <buffer> <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<s-left><s-left><s-left><s-left><s-left><left>
        let b:rwfcmd1 = 0
    else
        nnoremap <buffer> <F4> :RunWithFile
        let b:rwfcmd1 = 1
    endif
endfunc
nnoremap <leader>x :call MapRunCmd()<cr>

Gist of this post is: "toggling an almost same command while working on an individual or multiple files"
this toggle function works as expected but it has a little problem.

Current situation:

here, :RunWithFile is not by default
Only after typing \x i can access to <F4>

typing \x for the first time activates :RunWithFile .. then typing \x for the second time, it toggles.

Expected solution:

I want :RunWithFile to be default. so that typing \x one time, vim can toggle easily.
:RunWithFile will be

:RunWithFile ~/Downloads/ > output.txt && cat output.txt

I won't have to type twice (activate :RunWithFile then toggle)

Hope you folks can help me fix this

PS: i tried to use b: g: equal to 1 outside this function
but it didn't work. Don't know why

let b:rwfcmd1 = 1
let g:rwfcmd1 = 1
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Dom
    Commented Sep 8, 2021 at 14:16

1 Answer 1

0

This is (roughly) how I would write this with your skeleton: we add an autocommand to do the initial mapping on any buffer enter. If you want it for particular files, change the autocommmand's pattern. If you want it for particular filetypes, you're better off putting the call MapRunCmd() in ~/.vim/after/ftplugin/{filetype}.vim.

function! MapRunCmd() abort
  if exists("b:rwfcmd1") && b:rwfcmd1
    nnoremap <buffer> <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<s-left><s-left><s-left><s-left><s-left><left>
    let b:rwfcmd1 = 0
  else
    nnoremap <buffer> <F4> :RunWithFile
    let b:rwfcmd1 = 1
  endif
endfunc

augroup map_to_RunWithFile
  autocmd!
  autocmd BufRead,BufNewFile * call MapRunCmd()
augroup END
nnoremap <leader>x :call MapRunCmd()<cr>

But consider if you can create a single global mapping like nnoremap <leader>x :call RunWithFileArgs()<cr> where RunWithFileArgs can use something like b:run_with_file_args if it exists (or nothing):

function! RunWithFileArgs() abort
  const args = get(b:, 'run_with_file_args', '')
  call feedkeys(':RunWithFile '..args)
endfunction

nnoremap <leader>x :call RunWithFileArgs()<cr>

Then in your ftplugins or autocommands or what have you, just let b:run_with_file_args = '…'. For <s-left> you probably would have to use "\<s-left>" with double quotes instead of single.

I take a similar approach with my simpl plugin and its use of b:interpreter.

Your Answer

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

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