Vim Tips Wiki
(my opinions on related tips (and add to comment above))
Line 1: Line 1:
{{duplicate|226|487|985}}
 
 
{{TipImported
 
{{TipImported
 
|id=299
 
|id=299
Line 26: Line 25:
   
 
You can return to the previous buffer using Ctrl-^ or Ctrl-o.
 
You can return to the previous buffer using Ctrl-^ or Ctrl-o.
  +
  +
==Rough merge in from 487==
  +
Often when editing a file in Vim, you may have a filename/line number pair such as "vim.h:1506" indicating a file you wish to open at the line number listed.
  +
  +
The '''<tt>gF</tt>''' command will do this for you, although ':' is included in 'isfname' under Windows, so you'll need to get rid of it first with '''<tt>set isfname-=:</tt>'''
  +
*{{help|gF}}
  +
*{{help|CTRL-W_F}} (same thing, but split the window for the new file)
  +
*{{help|CTRL-W_gF}} (same thing, but open a new tab for the new file)
  +
  +
When such file-name/line number pairs are the result of compiling code, the following commands are also useful:
  +
*{{help|:cn}}
  +
*{{help|:cl}}
  +
*{{help|:cfile}}
  +
  +
;Plugin
  +
The {{script|id=2184|text=file:line plugin}} allows you to use combinations of file name and line number, like "vim.h:1506", as an argument to Vim.
  +
  +
When you open ''file:line'', the script checks if ''file'' exists and ''line'' is a number. If so, Vim opens ''file'' at the correct ''line'' line number.
  +
  +
==Rough merge in from 226==
  +
I use the command '<tt>gf</tt>' quite often. But with this command the current buffer is hidden. To avoid that I use the following mapping :
  +
<pre>
  +
map gw <Esc>:sp %<CR> gf
  +
</pre>
  +
  +
With this mapping the file under the cursor is opened after a horizontal split.
  +
  +
;Comments
  +
The same operation can be done more reliably by
  +
<pre>
  +
function! SplitOpen()
  +
let v:errmsg = ""
  +
normal gf
  +
if v:errmsg == ""
  +
sbp
  +
endif
  +
endf
  +
map gw <Esc>:call SplitOpen()<CR>
  +
</pre>
  +
  +
----
  +
Or even more simply by "<tt>CTRL-W f</tt>".
  +
  +
----
  +
Yes, <tt>CTRL-W f</tt> does this, but with the original idea you have a chance to make a little change, for example I prefer vertical split very often, but there is no hotkey for it in vim, so I use this idea and map:
  +
<pre>
  +
:nn ^W^F :vsplit<CR>gf
  +
</pre>
   
 
==Names containing spaces==
 
==Names containing spaces==
  +
===Rough merge in from 985===
{{todo}}
 
  +
What are some quick ways to access files? Environment variables and the <tt>gf</tt> command.
*''Merge [[VimTip985]] here.''
 
  +
  +
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:<br>
  +
<tt>C:/Documents and Settings/amen/My Documents/theory_e/godel-relativity.txt</tt>
  +
  +
Doing a <tt>gf</tt> 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):
  +
<pre>
  +
let $amen = 'C:/Documents and Settings/amen/My Documents'
  +
</pre>
  +
  +
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:
  +
<pre>
  +
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).
  +
</pre>
  +
  +
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:
  +
<pre>
  +
let $amen = '/home/amen'
  +
</pre>
  +
  +
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:
  +
<pre>
  +
" Substitute back slash to forward SLASH.
  +
command! -range Sslash <line1>,<line2>s;\\;/;g
  +
</pre>
  +
  +
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,
  +
<pre>
  +
" Visualize the desired area, then hit ",s".
  +
vmap ,s :Sslash<CR>
  +
</pre>
  +
  +
;Comments
  +
Or visual select filename and <tt>gf</tt>
   
  +
===From original 299===
 
You may need to open a file that has a space in its name. One approach is to visually select the name (including its embedded spaces), then type <tt>gf</tt>. For example, you could position the cursor on the first character of the file name, then press <tt>v</tt> to start visual selection, then repeatedly press <tt>E</tt> to move to the end of the next WORD until the whole name is selected, then type <tt>gf</tt> to go to the file.
 
You may need to open a file that has a space in its name. One approach is to visually select the name (including its embedded spaces), then type <tt>gf</tt>. For example, you could position the cursor on the first character of the file name, then press <tt>v</tt> to start visual selection, then repeatedly press <tt>E</tt> to move to the end of the next WORD until the whole name is selected, then type <tt>gf</tt> to go to the file.
   
Line 71: Line 165:
   
 
----
 
----
 
 
I've made my opinion known regarding the "related tips" that John has linked to by '''being bold''' and just marking them for merge or moving them to "see also", but I don't know what to do with this one:
 
I've made my opinion known regarding the "related tips" that John has linked to by '''being bold''' and just marking them for merge or moving them to "see also", but I don't know what to do with this one:
 
 
*[[VimTip691|691 Use gf to open a file via its URL]]
 
*[[VimTip691|691 Use gf to open a file via its URL]]
   
 
First, it needs a little work to handle escaped characters like %20 for spaces, etc. After this is done, I'm not sure whether it is separate enough of a concept for a new tip, or whether it should be merged as well. I'm leaning toward leaving it separate, but if not, we could add another section just below the one about paths with spaces.
 
First, it needs a little work to handle escaped characters like %20 for spaces, etc. After this is done, I'm not sure whether it is separate enough of a concept for a new tip, or whether it should be merged as well. I'm leaning toward leaving it separate, but if not, we could add another section just below the one about paths with spaces.
 
 
--[[User:Fritzophrenic|Fritzophrenic]] 16:15, January 6, 2010 (UTC)
 
--[[User:Fritzophrenic|Fritzophrenic]] 16:15, January 6, 2010 (UTC)
  +
:Thanks. I'm pretty ruthless these days and I have just done a "rough merge" in from the three you chose as duplicates (226, 487, 985), and I have replaced each of the merged-in tips with a redirect to here. I do it like that because it's easy, but also because it puts the whole tips (lightly massaged, but no content change) in the history on this page which sometimes helps in the future when checking what's happened. [[User:JohnBeckett|JohnBeckett]] 04:04, January 7, 2010 (UTC)

Revision as of 04:04, 7 January 2010

Tip 299 Printable Monobook Previous Next

created 2002 · complexity basic · version 7.0


The following commands open the file name under the cursor:

gf open in the same window ("goto file")
<c-w>f open in a new window (Ctrl-w f)
<c-w>gf open in a new tab (Ctrl-w gf)

When writing a program, it is helpful to set the 'path' option to list the directories with your include files. Then you can easily open an include file.

If there are several files in your 'path' that match the name under the cursor, gf opens the first, while 2gf opens the second, and 3gf opens the third, etc.

You can return to the previous buffer using Ctrl-^ or Ctrl-o.

Rough merge in from 487

Often when editing a file in Vim, you may have a filename/line number pair such as "vim.h:1506" indicating a file you wish to open at the line number listed.

The gF command will do this for you, although ':' is included in 'isfname' under Windows, so you'll need to get rid of it first with set isfname-=:

When such file-name/line number pairs are the result of compiling code, the following commands are also useful:

Plugin

The file:line plugin allows you to use combinations of file name and line number, like "vim.h:1506", as an argument to Vim.

When you open file:line, the script checks if file exists and line is a number. If so, Vim opens file at the correct line line number.

Rough merge in from 226

I use the command 'gf' quite often. But with this command the current buffer is hidden. To avoid that I use the following mapping :

map gw <Esc>:sp %<CR> gf

With this mapping the file under the cursor is opened after a horizontal split.

Comments

The same operation can be done more reliably by

function! SplitOpen()
  let v:errmsg = ""
  normal gf
  if v:errmsg == ""
    sbp
  endif
endf
map gw <Esc>:call SplitOpen()<CR>

Or even more simply by "CTRL-W f".


Yes, CTRL-W f does this, but with the original idea you have a chance to make a little change, for example I prefer vertical split very often, but there is no hotkey for it in vim, so I use this idea and map:

:nn ^W^F :vsplit<CR>gf

Names containing spaces

Rough merge in from 985

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

From original 299

You may need to open a file that has a space in its name. One approach is to visually select the name (including its embedded spaces), then type gf. For example, you could position the cursor on the first character of the file name, then press v to start visual selection, then repeatedly press E to move to the end of the next WORD until the whole name is selected, then type gf to go to the file.

Another approach is to adjust the isfname option.

To have a space (ASCII 32) considered as a valid character for a file name, add the following to your vimrc:

:set isfname+=32

On Dos/Windows (only), a further approach is to use the 8.3 form of the path to avoid spaces:

:set path+=C:\\DOCUME~1\\MYUSER~1\\MYDOCU~1

(or similar) in order to access C:\Documents and Settings\My User Name\My Documents. (Note that double backslashes are used here in the :set command to get single backslashes in the value. Alternately, forward slashes could have been used inside Vim.

See also

References

Comments

You can list the files in 'path' that match the name under the cursor with:

:echo globpath(&path, expand('<cfile>'))

The plugin searchInRuntime overrides gf and CTRL-W_f to ask which file must be opened if several match. Alternatives for :sp and :vsp are also provided.

Note that if your path contains many directories or recursive searches, it can take a very long time to find all matches, and it might be better for you to just stop at the first file found.


I've made my opinion known regarding the "related tips" that John has linked to by being bold and just marking them for merge or moving them to "see also", but I don't know what to do with this one:

First, it needs a little work to handle escaped characters like %20 for spaces, etc. After this is done, I'm not sure whether it is separate enough of a concept for a new tip, or whether it should be merged as well. I'm leaning toward leaving it separate, but if not, we could add another section just below the one about paths with spaces. --Fritzophrenic 16:15, January 6, 2010 (UTC)

Thanks. I'm pretty ruthless these days and I have just done a "rough merge" in from the three you chose as duplicates (226, 487, 985), and I have replaced each of the merged-in tips with a redirect to here. I do it like that because it's easy, but also because it puts the whole tips (lightly massaged, but no content change) in the history on this page which sometimes helps in the future when checking what's happened. JohnBeckett 04:04, January 7, 2010 (UTC)