3

I want to automate running several commands in vim, i.e. by typing :repl. The commands are:

:ConqueTerm lein repl
<Esc>
:set syntax=clojure
<i>

How do I define a custom vim function (command) that executes the above?

About the above:

  • clojure - the Clojure programming language (syntax provided by vim-clojure-static
  • ConqueTerm - a vim plugin that runs a shell interactively in your vim buffer
  • lein - Leiningen, a Clojure build tool
noahlz
  • 10,202
  • 7
  • 56
  • 75

2 Answers2

2

you could just create a function, and put your commands in that function:

fun! LeinCMD()
    execute 'ConqueTerm lein repl'
    execute 'set syntax=clojure'
    execute 'normal! i'
endf
command! Repl call LeinCMD()

you could source above codes, and type :Repl and Enter to test if it works for you.

EDIT

very nice comments by @Zyx. I just put them in answer, so that readers won't miss valuable information in future.

  1. You need exactly no :execute calls here.
  2. :normal! i is useless, to start insert mode from functions there is :startinsert and :call feedkeys(), former should be preferred. // Note: :normal! i “works” here because :startinsert is run by :ConqueTerm. I.e. due to the way Conque is written you don’t need :startinsert at all, but if it was not already there :normal! i would continue to do nothing useful.

Just mentioned: it would be much nicer to use command -bar: this way you won’t need :execute to chain your command with pipe symbol (try Repl | echo "Here" with and without -bar as a second argument to :command). I think it have been the default option, don’t know why Bram likes to have bad defaults in a number of places.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thanks! For posterity, here is an answer on how to "source" the codes: http://stackoverflow.com/questions/5326430/vim-how-to-execute-content-of-the-buffer – noahlz Mar 31 '13 at 21:45
  • 1
    1. You need exactly no `:execute` calls here. 2. `:normal! i` is useless, to start insert mode from functions there is `:startinsert` and `:call feedkeys()`, former should be preferred. // Note: `:normal! i` “works” here because `:startinsert` is run by `:ConqueTerm`. I.e. due to the way Conque is written you don’t need `:startinsert` at all, but if it was not already there `:normal! i` would continue to do nothing useful. – ZyX Apr 17 '13 at 20:09
  • @ZyX I appreciate your great comment and explanation! I learned from your comment. thank you very much! – Kent Apr 17 '13 at 20:15
  • 1
    Just mentioned: it would be much nicer to use `command -bar`: this way you won’t need `:execute` to chain your command with pipe symbol (try `Repl | echo "Here"` with and without `-bar` as a second argument to `:command`). I think it have been the default option, don’t know why Bram likes to have bad defaults in a number of places. – ZyX Apr 17 '13 at 20:33
  • @Zyx thank you so much. though I have ever read the `-bar`, I thought commands (no matter buildin or customer cmds) can be joined by `|` into a chain is kinda "default".... learned from u again. btw, I updated answer and added your comments, I hope you don't mind. – Kent Apr 17 '13 at 20:53
0

Have you tried?

function custom_function ()
  execute 'ConqueTerm lein repl'
  execute 'set syntax=clojure'
  return custom_function
endfunction

i don't know if this code will work as i have not tested it