0

So i'm new to vim, and specially new to vimscript, and messing around i made the following code to get the name for a file and then create said file in a new buffer. I want a way to check if FileName_ is empty, and if so, to exit the mapping.

function s:EnterFileName()
    call inputsave()
    let g:FileName_ = input("File name: ")
    call inputrestore()
endfunction
nnoremap <leader>sn  :call <SID>EnterFileName()<cr> <bar>:e <C-r>=FileName_ <cr> <bar> startinsert<cr> <c-o>:w <cr>
1

You asked to check the variable in the mapping but I'm going to suggest an alternative approach that will make things a lot easier and give you more flexibility if you want to change anything later.

Since you are using a function anyways my advice is to put everything in there. That simplifies things and avoids some issues you might have with escaping characters and using compound commands. Plus, you're talking about adding a conditional and things are going to start getting really crowded in that mapping line.

For example:

function s:EnterFileName()
    let l:filename = input("Enter filename: ")
    while len(l:filename) == 0
        let l:filename = input("Invalid. Enter filename:")
    endwhile
    exe ":e " . l:filename
    exe "norm i "
    w
endfunction

nnoremap <leader>sn  :call s:EnterFileName()<cr>

(I removed inputsave/inputrestore just to focus on the new stuff.)

This uses a while loop to continually query the user until they enter a non-empty string. Then it opens the file with the entered name, inserts text (looks like you just insert a space (?) ... modify as you need), and saves the file.

You'll notice a couple uses of :exe. That's needed when using a variable with an Ex command (first instance) and so we can get a space in the insertion part. (Again, modify as needed.)

The mapping simply calls the function.

9
  • This answers my question, but for some reason, is a little bit slower than what i had, also, for some reason the exe "norm i" is not putting me in insert mode in the new file
    – Akuseru
    Oct 28 '20 at 4:58
  • Do you want to be in insert mode when the mapping completes? If so I can fix that. (on the last line of the function just add startinsert I think) Can you give me more detail on the "slow" part. How are you seeing that manifest itself?
    – B Layer
    Oct 28 '20 at 5:01
  • to your first question, yes, i want to be in insert mode when the mapping is done. regarding the "slow" part, the moment i enter the name and press enter, there is a slight delay until it opens the new buffer, while in the original one that i proposed it was almost instantaneous, i tried it without the while and the results didn't change
    – Akuseru
    Oct 28 '20 at 5:07
  • Hmm. I don't think that should be happening (I don't see it). Could be something in your config. Let me think about it.
    – B Layer
    Oct 28 '20 at 5:18
  • you were right, i tested again in bare vim and the results were identical, thank you!
    – Akuseru
    Oct 28 '20 at 5:24

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.