Shortcut to open file in Vim

前端 未结 19 1045
误落风尘
误落风尘 2020-12-22 16:30

I want to open a file in Vim like in Eclipse using Ctrl + Shift + R, or via the Ctrl + N option of autofill. Invoke a

19条回答
  •  春和景丽
    2020-12-22 17:08

    I use a couple of shortcuts in my .vimrc file (exact syntax below).

    They are based on the fact that in 90% of the cases, I want to open another file in the same directory as the file that I am currently editing, or in a directory that is very close in the hierarchy to that edited file.

    Here's what the commands do do:

    ,cd : Change the current working directory to the directory that the current file you are editing is in.

    ,e : Opens a file with the current working directory already filled in so you have to specify only the filename.

    Put these into your .vimrc:

    map ,e :e =expand("%:p:h") . "/" 
    
    map ,cd :cd %:p:h 
    

    Here's a sequence of events:


    1. You are editing a file called test.java in "/home/prog"
    2. ,cd -> Current working directory now becomes "/home/prog"
    3. ,e -> Expands to ":e /home/prog" so that you can just fill in the file name, say test.h.

    1. ,e -> Expands to ":e /home"
    2. tab -> Cycle through subdirectories of /home
    3. enter -> cd to the directory you want say /home/prog
    4. ,e -> Expands to ":e /home/prog"

提交回复
热议问题