2

How can I extend a command in vim?

I want to do it in two situations,

  1. After a :diffget or :diffput I always want to run a :diffupdate
  2. After a :NERDTreeToggle I want to run a <C-w>=
José Luis
  • 3,713
  • 4
  • 33
  • 37
  • Do you want to _replace_ the default functionality, or create your own mapping which incorporates both functions (which is more typical)? – Michael Berkowski Apr 17 '13 at 16:09
  • @MichaelBerkowski I don't want to have a new command, so I suppose I want to replace the default functionality, but I'm not sure about the difference between replace and map – José Luis Apr 17 '13 at 16:59

3 Answers3

3

I am unaware of any autocmd events that would be triggered for your scenarios. However a few custom mappings might be helpful.

You can change the default dp and do mappings to also do a :diffupdate

nnoremap dp dp:diffupdate<cr>
nnoremap do do:diffupdate<cr>

Note there are times where you cannot use dp and/or do and must use :diffput/:diffget. In these cases I would suggest you create a commands like so:

command! -nargs=? -range=1 -bar Diffput <line1>,<line2>diffput <args>|diffupdate
command! -nargs=? -range=1 -bar Diffget <line1>,<line2>diffget <args>|diffupdate

Or you can just map :diffupdate

nnoremap <f8> :diffupdate<cr>

NERDTree

nnoremap <leader>N :NERDTreeToggle<cr><c-w>=
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
0

Maybe you can have a look to the vim macro. It is probably suitable for what you want to do :).

lthms
  • 509
  • 3
  • 10
0

Create your own custom function in VimScript in your .vimrc that wraps several commands.

Here's one I use to launch a Clojure Repl in a buffer using several plugins:

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

I then call this command with :Repl (note that your custom commands must always start with an uppercase letter).

Source: Automate running several vim commands and keystrokes

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75
  • 1
    You need exactly no `:execute` calls here. – ZyX Apr 17 '13 at 20:03
  • Also note that `normal! i` is useless. For use in functions there is `startinsert`. – ZyX Apr 17 '13 at 20:04
  • `:startinsert` is run by `:ConqueTerm`. Thus it *seems* `:normal! i` works, but it actually does not because scheduling entering insert mode have already been done earlier. – ZyX Apr 17 '13 at 20:13
  • You can just turn the function into a two-liner `ConqueTerm lein repl` | `set syntax=clojure`. Though for such simple case I would use plain command: `command! Repl :execute 'ConqueTerm lein repl' | set syntax=clojure` (note execute is required here because usually (it is the default) user-defined commands are not defined with `-bar` thus cannot be chained this way. `ConqueTerm` is one of such usual comands). – ZyX Apr 17 '13 at 20:35
  • This command is also a good candidate for `-bar`: `Repl | echo "abc"` is useless without it: complains about trailing characters instead of running `Repl` then `echo "abc"`. In fact, if I were Bram I would have made `-bar` the default. – ZyX Apr 17 '13 at 20:37
  • Do you think you could put your re-write of this code in a gist? – noahlz Apr 17 '13 at 21:50
  • What for? It is too small and local. Wondering why don’t you want to edit the answer, previous versions are saved in any case. – ZyX Apr 18 '13 at 18:05
  • I will go back and edit my answer...when I have a free moment and fully understand you answer - BTW - if you have enough rep you can edit another user's answers. – noahlz Apr 18 '13 at 19:12
  • I know. I never saw such edits except for some typo fixes thus I assume I should not do this as this is not a thing I should do. – ZyX Apr 19 '13 at 02:40