Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #985 - Quickly Get Files in your Environment

Created: September 4, 2005 16:53 Complexity: basic Author: Gochess Version: 6.0 Karma: 15/9 Imported from: Tip#985

Abstract: 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:


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


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,


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


Couple of other tips... try _directly_ editing a directory,


:e $amen 


Navigate around your file system, using just the <CR> key. For virgins, this

is a holy smokes moment. The ".." you see indicates the parent directory one

level up. While editing a file, try the split-screen directory edit,


:Sex 


which is pretty darn easy to remember (actually it's short for Sexplorer).

For Window users, no more complaining about not being able to print the

directory (hey, now you can yank it).


Finally, environment variables are very useful in creating commands:


command! Egodel e $amen/theory_e/godel-relativity.txt 


With that in your vimrc, you can quickly edit your favorite file by

simply...


:Egod 


Hope this brightens your day! (which will re-occur, sooner or later)


Gochess

http://wiki.43folders.com/index.php/Gochess

Comments

Erratum; kindly add the quotes...

let $amen = '/home/amen' 

-


Gochess , September 4, 2005 17:24


or visual select filename and gf

Anonymous , September 5, 2005 15:42


I find that using ':S' to start the file explorer is far less disrupting to my thought process.

toomuchphp-vim--AT--yahoo.com , October 25, 2005 18:18


Advertisement