Change working directory to currently opened file

前端 未结 3 1167
甜味超标
甜味超标 2021-02-01 18:52

How can I change my working directory to the path of the file that I currently have open?

Example

  1. current working directory is $HOME
3条回答
  •  既然无缘
    2021-02-01 19:22

    To change to the directory of the currently open file (this sets the current directory for all windows in Vim):

    :cd %:p:h
    

    You can also change the directory only for the current window (each window has a local current directory that can be different from Vim's global current directory):

    :lcd %:p:h
    

    In these commands, % gives the name of the current file, %:p gives its full path, and %:p:h gives its directory (the "head" of the full path).

提交回复
热议问题