Kutub Gandhi
Kutub Gandhi

Reputation: 43

How can I open files within vim's terminal emulator

Using vim, I use :term to open the terminal emulator. After cd /path/to/project within the terminal emulator, I have a file called foo.txt. If I did vim foo.txt to open it as I would in a normal terminal, it would open vim within vim which causes a variety of issues. I see two potential solutions:

Does anyone have tips on either solution?

Upvotes: 3

Views: 1314

Answers (4)

gdupras
gdupras

Reputation: 751

Inside Vim's builtin terminal, do

vim --remote foo.txt

This assume that your Vim was compiled with the +clientserver option. You can check with :version or :echo has('clientserver').

Upvotes: 2

Peruz
Peruz

Reputation: 433

With Neovim, this seems to work too

mvim() {
local fnames_realpath=()
local fnames=( "$@" )
for fname in "${fnames[@]}"; do
    # echo "$fname"
    local fname_realpath=$(realpath "$fname")
    # echo "$fname_realpath"
    fnames_realpath+=("$fname_realpath")
done
if pgrep nvim >/dev/null 2>&1; then
    for fname_realpath in "${fnames_realpath[@]}"; do
        nvim --server $NVIM_LISTEN_ADDRESS --remote-send "<C-\><C-N>:tabfirst | e $fname_realpath<CR>"
    done
fi
if ! pgrep nvim >/dev/null 2>&1; then
    rm -f "$NVIM_LISTEN_ADDRESS"
    nvim --listen $NVIM_LISTEN_ADDRESS "${fnames_realpath[@]}"
fi
}

Updated to handle white spaces in the paths and multiple file names, including file names with escaped white spaces.

Upvotes: 0

Samuel Williams
Samuel Williams

Reputation: 1

You have a couple of options.

One would be to get your shell to print the full path of the file that you would like to edit (for example, by using bash's realpath ./<file_to_edit_goes_here> command), then exit insert mode, place your cursor over the filepath that was printed, and use gf to open the filepath under the cursor (see :help gf).

Alternatively, if you are using Neovim, I've written a plugin called nvim-unception to open files without nesting Neovim sessions by using Neovim 0.7's built-in RPC functionality.

Upvotes: 0

Kutub Gandhi
Kutub Gandhi

Reputation: 43

vim --remote works on vim, but neovim compiles without clientserver. neovim-remote seems to be an alternative for neovim.

Upvotes: 1

Related Questions