Vim Tips Wiki
Register
(Add subpage to TipProposed to fix link)
(Minor reword + explain where to put script + references)
Line 9: Line 9:
 
|subpage=/200802
 
|subpage=/200802
 
}}
 
}}
I was interested in using <tt>gf</tt> (goto file) in my Python source code to open Python files. To enable this to work, I added the following to my the vim file that is loaded when I start to edit a *.py file:
+
Here is how to use <tt>gf</tt> (goto file) in Python source code to open an imported Python file. Put the following in a script that Vim will source when you edit a *.py file:
   
 
<pre>
 
<pre>
 
python << EOF
 
python << EOF
 
import os
 
import sys
 
import sys
 
import vim
 
import vim
import os
 
 
 
for p in sys.path:
 
for p in sys.path:
# Check for existance before trying to add.
+
# Add each directory in sys.path, if it exists.
 
if os.path.isdir(p):
 
if os.path.isdir(p):
# Replace spaces in paths to avoid errors.
+
# Command 'set' needs backslash before each space.
 
vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
 
vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
 
EOF
 
EOF
 
</pre>
 
</pre>
   
This allows you to use gf or CTRL-W_CTRL_F to open the file under the cursor. It works pretty well, particularly for imports of the form:
+
This allows you to use <tt>gf</tt> or Ctrl-W Ctrl-F to open the file under the cursor. It works pretty well, particularly for imports of the form:
 
 
<pre>
 
<pre>
 
import abc.def.module
 
import abc.def.module
Line 32: Line 30:
   
 
But not so well with:
 
But not so well with:
 
 
<pre>
 
<pre>
 
from abc.def import module
 
from abc.def import module
 
</pre>
 
</pre>
  +
  +
Place the script in one of the following files:
  +
*<tt>~/.vim/after/ftplugin/python.vim</tt> &nbsp;&nbsp; (on Unix systems)
  +
*<tt>$HOME/vimfiles/after/ftplugin/python.vim</tt> &nbsp;&nbsp; (on Windows systems)
  +
  +
Create any directories which do not exist. If the file python.vim does not exist, create it containing the above script (nothing else is needed). If python.vim does exist, add the above. To find out which is your home directory, run Vim and use the command <tt>:echo $HOME</tt>.
  +
  +
==References==
  +
*{{help|python}}
  +
*{{help|gf}}
  +
*{{help|CTRL-W_CTRL-F}}
  +
*{{help|after-directory}}
   
 
==Comments==
 
==Comments==

Revision as of 03:05, 19 March 2008

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created February 7, 2008 · complexity basic · author Legner · version 7.0

Here is how to use gf (goto file) in Python source code to open an imported Python file. Put the following in a script that Vim will source when you edit a *.py file:

python << EOF
import os
import sys
import vim
for p in sys.path:
    # Add each directory in sys.path, if it exists.
    if os.path.isdir(p):
        # Command 'set' needs backslash before each space.
        vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
EOF

This allows you to use gf or Ctrl-W Ctrl-F to open the file under the cursor. It works pretty well, particularly for imports of the form:

import abc.def.module

But not so well with:

from abc.def import module

Place the script in one of the following files:

  • ~/.vim/after/ftplugin/python.vim    (on Unix systems)
  • $HOME/vimfiles/after/ftplugin/python.vim    (on Windows systems)

Create any directories which do not exist. If the file python.vim does not exist, create it containing the above script (nothing else is needed). If python.vim does exist, add the above. To find out which is your home directory, run Vim and use the command :echo $HOME.

References

Comments