0

New to vimscript. I'm writing a function that I want to be able to clear the contents of the current buffer and write this to the corresponding file (or alternatively, clear the contents of the file and then reflect this in the buffer). What I have so far is to basically have my mapping call the function and then after run the editor commands gg0dG and :w<CR>, but it seems like there is probably a more elegant way to do this, no?

function MyFunction()
    ... some things ...
endfunction

nnoremap <CR> :call MyFunction()<CR> <bar> gg0dG <bar> :w<CR>
3
  • Well if you wanted you could put the commands in the function. Instead of ggdG (which would require normal! in the function), you can use %delete – D. Ben Knoble Jul 16 '20 at 2:23
  • Yeah, IMO it's a little cleaner if you avoid complex commands and/or calling a mix of custom (non-builtin) functions and command-line commands if it's easily avoided. You can reduce it down to a single function call (either the existing one or a new one that calls the existing one). This isn't a major thing, though. – B Layer Jul 16 '20 at 5:49
  • Could be as well nnoremap <CR> :call MyFunction() <Bar> %d_ <Bar> w<CR> Just do not switch between too many modes until you know what you're doing. – Matt Jul 16 '20 at 5:55

Your Answer

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

Browse other questions tagged or ask your own question.