(Remove html character entities) |
(Reordered methods in order of usefulness. Unpersonalized. Exchanged cmap for cabbr.) |
||
Line 11: | Line 11: | ||
|category2= |
|category2= |
||
}} |
}} |
||
− | It |
+ | It is sometimes frustrating to open files deep in the directory tree. This tip contains some solutions for opening another file in the same directory as the current file. Add one of the following snippets to your [[vimrc]]: |
+ | |||
+ | == Method 1 == |
||
+ | This method uses a ''keyword'' which you're unlikely to use to make a command line abbreviation {{help|:cabbr}} which expands into the path of the current file |
||
<pre> |
<pre> |
||
+ | cabbr <expr> %% expand("%:h") |
||
− | " Edit another file in the same directory as the current file |
||
− | " uses expression to extract path from current file's path |
||
− | " (thanks Douglas Potts) |
||
− | if has("unix") |
||
⚫ | |||
− | else |
||
− | map ,e :e <C-R>=expand("%:p:h") . '\'<CR> |
||
− | endif |
||
</pre> |
</pre> |
||
+ | Thus, when typing <tt>:e %%/</tt> on the command line, it will expand to <tt>:e <path_of_current_file>/</tt>, and you can use completion or just hit enter to load the directory listing. Of course, this abbreviation works anywhere on the command line, so you can use it for <tt>:cd</tt>, <tt>:grep</tt> etc. |
||
− | Then when you type <tt>,e</tt> in normal mode you can use tab to complete to the file. You can also expand this to allow for splitting, etc. |
||
+ | The <tt>%%</tt> ''keyword'' can be anything you prefer. |
||
− | |||
− | An alternative is to expand the current file directory while you're on the command line with something like: |
||
+ | ==Method 2== |
||
+ | This method uses a mapping to enter <tt>:e <path_of_current_file>/</tt> on the command line — just supply a file name and hit enter. |
||
<pre> |
<pre> |
||
⚫ | |||
− | if has("unix") |
||
− | cmap %/ <C-R>=expand("%:p:h") . '/'<CR> |
||
− | else |
||
− | cmap %/ <C-R>=expand("%:p:h") . '\'<CR> |
||
− | endif |
||
</pre> |
</pre> |
||
+ | Here the expression register {{help|id="=}} is used to extract the path from the current file. Note that this method is less versatile than [[Easy_edit_of_files_in_the_same_directory#Method_1|Method 1]]. |
||
− | This has the advantage that you can use it with any command, such as <code>:e</code>, <code>:w</code>, <code>:cd</code> etc. For example, typing: |
||
− | <pre> |
||
− | :cd %/ |
||
− | </pre> |
||
+ | ==References== |
||
− | Would become something like: |
||
+ | * {{help|:cabbr}} |
||
− | <pre> |
||
+ | * {{help|expand()}} |
||
− | :cd /home/username/foo/ |
||
+ | * {{help|cmdline-special}} |
||
− | </pre> |
||
+ | * {{help|filename-modifiers}} |
||
+ | * {{help|:map}} |
||
+ | * {{help|id="=}} |
||
+ | * {{help|mapleader}} |
||
==Comments== |
==Comments== |
Revision as of 16:09, 4 September 2009
created February 24, 2001 · complexity intermediate · author scott · version 5.7
It is sometimes frustrating to open files deep in the directory tree. This tip contains some solutions for opening another file in the same directory as the current file. Add one of the following snippets to your vimrc:
Method 1
This method uses a keyword which you're unlikely to use to make a command line abbreviation :help :cabbr which expands into the path of the current file
cabbr <expr> %% expand("%:h")
Thus, when typing :e %%/ on the command line, it will expand to :e <path_of_current_file>/, and you can use completion or just hit enter to load the directory listing. Of course, this abbreviation works anywhere on the command line, so you can use it for :cd, :grep etc. The %% keyword can be anything you prefer.
Method 2
This method uses a mapping to enter :e <path_of_current_file>/ on the command line — just supply a file name and hit enter.
map <Leader>e :e <C-R>=expand("%:p:h") . '/'
Here the expression register :help "= is used to extract the path from the current file. Note that this method is less versatile than Method 1.
References
- :help :cabbr
- :help expand()
- :help cmdline-special
- :help filename-modifiers
- :help :map
- :help "=
- :help mapleader
Comments
See also Set_working_directory_to_the_current_file for another solution to this problem.