Basically, I'm editing files that have include file names and I want a quick way of opening the file without having to type the path/filename:
include('inc/doctype.inc.php');
Is there an easy way to do this? (Ideally, I'd like to use :tabnew to open the file in a new tab.)
Thanks.
Use the gf shortcut. Move your cursor on a path string, the exact cursor position is not important and then press gf in normal mode. gf stands for "goto file".
See vims help page with :h gf:
Uses the 'isfname' option to find out which characters
are supposed to be in a file name. Trailing
punctuation characters ".,:;!" are ignored.
Uses the 'path' option as a list of directory names
to look for the file. Also looks for the file
relative to the current file.
Uses the 'suffixesadd' option to check for file names
with a suffix added.
If the file can't be found, 'includeexpr' is used to
modify the name and another attempt is done.
To get back, use Ctrl-o in normal mode.
Note: This command brings the cursor position to older positions in the jump list. The opposite command is Ctrl-i which brings the cursor to newer positions in the jump list.
Put the cursor on the filename, then Ctrl+wgf
:h ctrl-w_gf
Related
I want to create a key combination that would copy the text under the cursor, open the :find command, yank the word and then press Tab to autocomplete to the first filename in the list, so (in 99% of cases) I would have just to press enter to open the file.
map <Leader>o yw:find <C-R>"<Tab>
However, when I press <Leader>o, I get :find FileName^I in the command line instead. How can I make it react the same way as if I pressed the key myself?
You need the wildcharm options:
set wildcharm=<C-z>
map <Leader>o yw:find <C-R>"<C-z>
See :help 'wildcharm'.
Here is a more solid, non-recursive, alternative that doesn't clobber the unnamed register for no reason:
nnoremap <leader>o :find <C-r><C-w><C-z>
The gf (goto file) command should do the same thing as your mapping. This opens the file whose name is under or after the cursor.
If not getting the expected behaviour, it's worth checking the contents of the isfname option (use set isfname? to check). This specifies a list of characters that are treated as valid characters for file path names.
It’s also worth checking / setting the contents of Vim’s path option which lists the directories which are searched when using gf, :find and similar commands, e.g., the default setting on MS Windows is .,,:
. searches relative to the directory of the file currently being edited
,, (empty string) searches the current directory; use the cd command to check (or set) your current directory.
See
help gf
help isfname
help path
I have a few text files, each for its own purposes. (Like: download.txt, questions.txt, word-meaning.txt etc.)
questions.txt:
I put all my question, doubt approaching in my mind to this file to ask/clear when I go online (I've no access to internet everytime of the day). I delete that line from the file when I ask that question.
download.txt:
I keep names of all packages or zipballs or tarballs in this file and download when I am connected.
word-meaning.txt:
I am not a native English speaker, so whenever I see any word which's meaning I don't in my native language, I write that down in this file and use Google Translate to translate it to my native language when I am connected.
In all above cases I have to go to last line of the file everytime I have to add anything to those lists.
My Question:
Can I make vim go to line line, last character of the file and then go in in insert mode? I will alias that to something like vimll to use it with these type of files.
Similar Question:
How do I start vim in insert mode?
You can define an autocmd to go into insert mode at the end of the file whenever one of your files is loaded into a Vim window:
autocmd BufWinEnter questions.txt,download.txt,word-meaning.txt $|startinsert!
You can do this in terminal:
vim filename.ext +$ +starti!
To go to the last line, last character of the file filename.ext and then in insert mode.
You can also alias that for your convenience of use, so add the following in your .bash_aliases file:
alias vimll='vim +$ +starti!'
I work almost exclusively in the terminal, and very often I need to view files that appear in error stacktraces. A very common format is /some/file:99, where 99 is the line number. I'd like to be able to copy that line, and open goto that line easily in vim.
What I'm looking for is the ability to do :e /some/file:99, and vim automatically opens that file at line 99. Does something like this exist? If not, is it possible to write a plugin for it?
The edit command can take options, so try this:
:e +99 /some/file
Just found this link:
http://vim.runpaint.org/basics/opening-files/
You may prefix the filename with +linenumber to instruct Vim to jump to the given line after opening. For example, vim +7 todo.list or :e +100 treatise.txt. If you omit linenumber, i.e. you prefix the filename with +, Vim will jump to the end of the file.
If you can redirect /some/file:99 to a file then you can jump to /some/file at line 99 by just pressing gF when you cursor is on file's name.
This plugin was designed with this specific purpose in mind: file:line
Where does this /some/file:99 comes from ? Is it the output of some external command ? If so, you should read the documentation related to the quickfix mode. (:h quickfix).
If I have a file that contains a complete path for a file, is there a way to highlight the filename (using visual mode) and open the file (preferably in a split screen)?
Here is the behavior I would like: if the file name contains a / character, assume it is a full path (i.e. the current directory is root). Otherwise, use the current folder (i.e. default behavior). Is this possible?
Put the cursor on the filename and type gf (in command mode). Or use CTRL+W | CTRL+F to open in another window. See also :help gf (no, it's not your girlfriend).
gf command opens file under cursor.
I write a lot of shell scripts and I am constantly having to enter in filepaths. I am wondering if anyone knows of a way to get Vim to autocomplete filepaths while in insert mode, just like when you are in your favorite shell you tab to complete the path of the directory or file.
Extra credit if you can get CTRLD functionality of the shell into Vim insert mode (i.e. to see what files/directories are in the current path).
For file name omni completion, you can use:
Ctrl-XCtrl-F
There's ctrl-x ctrl-f
:he compl-filename
To build on #CMS and #michael excellent answers
When using ctrl+X ctrl+f command sequence it will display a list of files in the current directory. I spend a minute looking for the right key to move up and down between the different filenames. The correct keys are Ctrl-n and Ctrl-p. You can use almost any other key (like Space) to select and continue typing.
In addition, if you are not already at the file/directory you would like to insert, you can go down a file tree structure as follows:
Optionally enter some part of the directory. You can even use ../../ for example!
Press ctrl+X ctrl+f and select the first item in the tree from the list.
Next press ctrl+f again while the correct item is highlighted to display a list of the next level down the directory tree structure.
You can keep doing this until you find the directory/file you would like to insert in the document.
I experienced similar problem. I found solution like:
sudo apt-get install realpath
And in VIM naviagte to file with in normal mode type:
:r !realpath /path/to/file
When you are navigating in non-insert mode after !realpatch you are able to use our key button.
VOILA! TAB is working again!
edit: excuse me, I landed here from a google result for "vim insert file absolute path"
(first leave insert mode with esc or ctrl+c) ;)
from normal mode, on a blank line
!!readlink -f #
this will run a command, and substitute # with the current file name, readlink will resolve a canonical name, and !! will write the output where the cursor was
note, this needs to be done on a blank line, as the content of the line will be fed as stdin to the subcommand.