rupa / z

z - jump around
Do What The F*ck You Want To Public License
16.23k stars 1.16k forks source link

How to use z in MacVim? #118

Open zhangchiqing opened 10 years ago

zhangchiqing commented 10 years ago

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!

pepegar commented 10 years ago

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

knu commented 10 years ago

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 :!.

pepegar commented 10 years ago

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

johnfb commented 10 years ago

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 notifications@github.comwrote:

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 .

pepegar commented 10 years ago

That's super handy! thanks @johnfb!

rperryng commented 5 years ago

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>)