z icon indicating copy to clipboard operation
z copied to clipboard

How to use z in MacVim?

Open zhangchiqing opened this issue 12 years ago • 6 comments

z is great tool. I use it everyday!

Does z support MacVim? Something like typing ":z myproj" will set current working directory of MacVim to "myproj" folder will be very useful to me. Thanks a lot!

zhangchiqing avatar Sep 11 '13 23:09 zhangchiqing

You can use any command line app in MacVim through ":!" command, so ":!z Users" would work.

pepegar avatar Sep 24 '13 20:09 pepegar

That doesn't change the current directory of the Vim process. Also, z is an alias to a shell function _z_cmd that cannot be called simply with :!.

knu avatar Sep 25 '13 03:09 knu

Ah, right. I thought that z was a command, not a shell function.

pepegar avatar Sep 25 '13 11:09 pepegar

I don't know about macvim, but here is how to do with for vim in general that should work on any platform that vim and z work on. Create a shell script somewhere, I'll call it execz.sh with contents like:

. path/to/z.sh _z $* pwd

This gives you a shell script that acts like the shell command z but echos the final directory switched to rather than switching to it.

Then in your vimrc have something like

command -nargs=+ Z execute "cd " . system("path/to/execz.sh ")

This defines a new user command Z that takes one or more arguments and executes the shell script above with those arguments, takes the result then executes :cd result

Note that vim wont let you create user commands with lowercase letters.

On Wed, Sep 25, 2013 at 6:56 AM, José Luis García [email protected]:

Ah, right. I thought that z was a command, not a shell function.

— Reply to this email directly or view it on GitHubhttps://github.com/rupa/z/issues/118#issuecomment-25080108 .

johnfb avatar Sep 25 '13 17:09 johnfb

That's super handy! thanks @johnfb!

pepegar avatar Sep 25 '13 21:09 pepegar

I couldn't get @johnfb's suggestion to work for me, I'm posting an updated solution in case anyone else stumbles across this.

command! -nargs=+ Z execute "cd " . system('. ~/path/to/z.sh && _z -e ' . <q-args>)

This solution doesn't require creating another shell script file.

A little more verbose solution but it also echos the directory lookup

function! ZLookup(z_arg)
  let z_command = 'cd ' . system('. ~/path/to/z.sh && _z -e ' . a:z_arg)
  " Strip empty newline so that command line doesn't grow when echoing
  let z_command = substitute(z_command, "\n", "", "")
  execute z_command
  echo z_command
endfunction

" Change working directory using z.sh
command! -nargs=+ Z call ZLookup(<q-args>)

rperryng avatar Sep 07 '18 03:09 rperryng