4

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:

  • Find some way to open a file in a split from the terminal emulator
  • Find some way to change the cwd of vim to the cwd of the terminal emulator.

Does anyone have tips on either solution?

3
  • Tip: don't. Use Vim's built-in file handling features instead. Also, what did you try? Commented Sep 7, 2021 at 15:03
  • You can use the server feature to do this, but it requires you to modify the invoking command. Honestly, though, for this particular use-case, I wouldn't. Just jump into normal mode (<C-w><S-n>) and open it Commented Sep 7, 2021 at 19:16
  • 1
    Using Vim's built-in file handling or switching to normal more runs into the issue that Vim's cwd != the cwd of the terminal emulator. This is certainly not the biggest problem, but is still something of an annoyance. Commented Sep 8, 2021 at 13:50

4 Answers 4

3

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').

Sign up to request clarification or add additional context in comments.

3 Comments

Better to specify servername like vim --servername $VIM_SERVERNAME --remote foo.txt.
You can specify the server name but this is not necessary if you only have one instance of Vim running.
for nvim it should be vim --server $NVIM --remote foo.txt; --remote should be after all options
1

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.