Get a shell command for changing to the current directory
From Vim Tips Wiki
created 2006 · complexity basic · author John L. Clark · version 6.0
This tip is supposed to provide an easy way to synchronize the current directory between a Vim session and a shell. The most naive approach is to simply build a command for changing to the current directory in Vim, and then paste and execute that command within your shell. This is the approach we take here. The following command line places a string of the form
cd "<current directory" in the paste register (@+):
let @+="cd \"" . escape(getcwd(), "\"") . "\""
Here, we escape any double-quote characters in the path name because quotes are used in the string itself as argument delimiters. You could then map this to a key sequence or use it in other ways. I've chosen to make it the definition for a command, as follows:
com Scd let @+="cd \"" . escape(getcwd(), "\"") . "\""
"Scd" could stand for "Save current directory" or "Shell cd". Of course, you could use a different name entirely. If you wanted to generate a 'cd' command for the directory of the current file, instead of the current working directory, you could use 'expand("%:p:h")' instead of 'getcwd()' in the above expressions, as mentioned in VimTip64. I like to keep the two actions separate, so I have another command for quickly changing to the directory of the current file:
com Fcd cd %:p:h
Where "Fcd" could stand for "File current directory".