Vim Tips Wiki
Advertisement
Tip 985 Printable Monobook Previous Next

created September 4, 2005 · complexity basic · author Gochess · version 6.0


What are some quick ways to access files? Environment variables and the gf command.

Using the mouse to navigate to a directory, then clicking-on an icon or shortcut is too laborious and time consuming. Vim is designed to minimize mouse usage.

Instead of a directory, one could open a file which contains filenames to edit. Once your cursor is placed within a filename, the command "gf" will magically open it. (I memorize that by 'get file'.)

Some operating systems allow the use of the space character in specifying directories and files. Bad idea, for that practically breaks the use of gf.

Assuming that your username is "amen" and that you adhere to the convention of no-spaces in names, we will proceed with some examples, in your directory called "theory_e" and a file called "godel-relativity.txt".

In Windows XP, the full path would look something like this:
C:/Documents and Settings/amen/My Documents/theory_e/godel-relativity.txt

Doing a gf on this mess will bring unpredictable results depending on where your cursor was placed on that line -- because it is not continguous. Now luckily that actually fit on one line. If there were subdirectories, that line will break past the edge, and again gf will malfunction.

Now gf is not buggy, but rather, the file specification is ugly. So a solution might be to create an environment variable (preferably in your _vimrc):

let $amen = 'C:/Documents and Settings/amen/My Documents'

Note that the offending spaces are contained within quotes -- which is the saving grace. Now let's read our notes, somewhere say from drive D:

Kurt Godel constructed a model which was consistent with Einstein's
general theory of relativity in which the direction of time was not
unidirectional, but rather, circular. Einstein's critique is found in
$amen/theory_e/godel-relativity.txt (section 43 needs editing).

Now doing a gf on that filename will open it. And notice the economy in style. If you move to another system where your user path is different, just change the assignment of $amen. Your text files need not be revised. For example, on your Unix machine, .vimrc might include this sane version:

let $amen = '/home/amen'

Generally, I prefer forward slashes when writing paths, to preserve multi-platform compatibility. So on a Windows machine which insists on back slashes, I use this conversion command often:

" Substitute back slash to forward SLASH.
command! -range Sslash <line1>,<line2>s;\\;/;g

Thus :Sslash will work on a single line, or a specified range, e.g. :7,14Sslash for lines 7 through 14. Or better yet, just visualize an area, and then execute the command via ":" which brings up the implied range :'<,'> -- which can be automated by the following visual mapping,

" Visualize the desired area, then hit ",s".
vmap ,s :Sslash<CR>

Comments

Or visual select filename and gf


Advertisement