Opening a related file in vim - vim

In Vim, how can I get the name of the current file, pass it to an external script, read the output of the script (which will be a filename) and open that file in vim in a new buffer? For example, I'm editing lib/foo.rb and want to open spec/foo_spec.rb automatically.

Here is a simple solution using backtick expansion:
:e `foo.sh %`
% is expanded by Vim to the current buffer name,
then the backtick expression is evaluated in a sub-shell,
and the result is used as argument for :e.
See :help expand() for %, and :help backtick-expansion for… backtick expansion.

Related

How to make Vim ignore file extensions when opening files through the command line/shell?

Consider the following directory tree:
root/include/file.hpp
root/source/file.cpp
root/images/file.png
The command line is inside the directory root.
In the vimrc file, there is set wildignore=*.png.
If you open Vim in the folder root and run :next */file.*, it opens only file.hpp and file.cpp.
However, if you launch Vim from command line with vim */file.*, it opens all three files.
So, when feeding it a filename, it first loads the files, then vimrc? Is there a way to ignore extensions when opening files with Vim through the command line? Or to make Vim load vimrc first?
In the first scenario, the glob expansion is done by Vim and thus obeys the rules in your vimrc.
In the second scenario, the glob expansion is done by your shell and there's no reason to expect it to obey the rules in your vimrc.
You can do something like $ vim -c "next */file.*", which essentially opens Vim without a filename and executes next */file.*.
Or you can exclude the pattern directly in your shell. Assuming you have extglob set, this can be done in bash with $ vim !(file.png).
When doing :next */file.* from within Vim, vim expands the wildcard and filters by wildignore. When doing vim */file.* from your shell, the shell expands the wildcard, and passes all three files to Vim.
Depending on your shell, this will probably work instead:
vim +"args */file.*"

Why vim disallows globbing with :e command?

I use buffers as "tabs" in Vim, and open new files using :e[dit] command. Why can't I use globbing with :e command?
:e some_dir/*
E77: Too many file names
Just use :n instead:
:n some_dir/*
You can use globbing with :edit and other commands; however, there must be a single, unique result of the glob, because the :edit command only takes a single file. (How else should it display multiple matches in the single current window?)
If you want to edit multiple files sequentially, you can use :args or :argadd instead. Note that the :split command does not take multiple files, neither (probably because of the risk that many matches will inadvertently cause an impractically large number of window splits), but you could write your own :Split command that provides this functionality.
You should use :args or :argadd instead.
:args src/*
:tab all
First command loads all files from src/ folder to buffers and second command puts each buffer then into separate tabs.
:help arglist will give more information

Edit a file with single quotes in the filename in Vim

I've run into an odd problem in Vim. I would like to drag and drop a file from my desktop or file manager into Vim and edit it. Gvim handles this behavior correctly.
When I attempt to do the same thing in console Vim, the path to the file name is inserted instead. For example, if I drag and drop the file /home/myuser/foo.matic, it will apply the text string '/home/myuser/foo.matic' to the current buffer.
If I type :edit, then drag and drop the file name, Vim treats '/home/myuser/foo.matic' as a new directory.
I believe the problem here is the quotes before and after the file path. These appear to be inserted by both gnome-terminal and terminator. Is there a way to strip these quotes from the file name when dragging and dropping? Alternatively, is there a way for Vim to ignore the quotes?
You can’t make vim own :e command to do what you need, but you can define your own one. Most straightforward solution - make shell parse what was intended to be parsed by the shell - is listed below:
command -nargs=? -bang -bar E :execute "e<bang> ".fnameescape(system("echo -n ".<q-args>))
. This command accepts only :e[!] {file} variant, no +cmd and ++opts are allowed.

How to echo special characters like %, <cfile> in Vim?

Say I wanna see what directory I am in, I type
:echo .
and get the error message:
E15:Invalid expression .
How can I echo out those special characters like %, ., <cfile>?
There is a dedicated command to print the current directory path:
:pwd
(See :help :pwd and the whole “The current directory” section in the
help: :help current-directory.)
To quickly find out what paths Vim command-line specials are expanded
to, use
:echo expand('%:p:h')
or, shorter,
:!echo %:p:h
The former command is based on the expand() function that expands
wildcards and special keywords in a given argument (see :help expand).
The latter command takes advantage of the fact that wildcards are
expanded before running an external command (see :help cmdline-special).
You need to use expand()
For example
:echo expand("%:h")
prints the actual directory (of the currenct buffer).
See also :help expand
If you want to inquire about the current-directory (which is buffer independant), you should just du a :pwd. (See :help current-directory)
(Edit: changed from :cd to :pwd as per comment of ib).

how to copy editing file in same location but different name with VIM

I am trying to find a combination of commands, macro or a plugin to copy the file I am editing in VIM (actually macvim) to the same location but with a different name. I am sure this could be achieved with a simple:
!cp $CURRENT_FILE_PATH $NEW_PATH
but I don't know what variables to use nor the syntax.
:saveas %:p:h/new_name
For more information:
:h :saveas
:!cp % %:h/new_name
% expands to the current filename
%:h expands to the current filename's directory
To learn more:
:help expand()
:help filename-modifiers

Resources