Edit in the same neovim instance #1956
Replies: 3 comments 2 replies
-
You can use https://github.com/mhinz/neovim-remote. " ~/.config/nvim/init.vim
let $GIT_EDITOR="nvr" |
Beta Was this translation helpful? Give feedback.
-
Provided you are running neovim 0.7 you can do the following: Run you main neovim instance like this:
Now in your lazygit config add the following:
This will open the file in your main neovim instance ready for editing when you press This works great but I would like to be able to run a nvim instance per repo rather than a global instance where I send all files. In order to achieve this I would to use a small wrapper script but I need to work out how to get lazygit to send the full file path or the filename and the repo directory path. The templated filneame variable is relative to the repo, therefor I cant tell what repo the file resides in in my wrapper script. |
Beta Was this translation helpful? Give feedback.
-
Hello. I am going to share my neovim - lazygit setup, maybe it will be useful for someone. I have this in my .zshrc, nvim function will generate random filename for neovim server, and launch neovim with environment variable MY_NVIM_LISTEN_ADDRESS. gen_str() {
tr -dc A-Za-z0-9 </dev/urandom | head -c "${1:-13}"
echo ''
}
nvim() {
if [ "$1" = "--name" ]; then
test -n "$2" || {
echo "Server name can not be empty" 1>&2
exit 1
}
server_name="$2"
shift 2
else
server_name=$(gen_str 12)
fi
if [ ! -e "/tmp/$server_name" ]; then
# NOTE: NVIM_LISTEN_ADDRESS got deleted by nvim
local MY_NVIM_LISTEN_ADDRESS
MY_NVIM_LISTEN_ADDRESS="/tmp/$server_name"
MY_NVIM_LISTEN_ADDRESS="$MY_NVIM_LISTEN_ADDRESS" command nvim --listen "$MY_NVIM_LISTEN_ADDRESS" "$@"
else
echo "/tmp/$server_name already exist. Use another name" 1>&2
fi
} I have this script in my PATH, it can be used with various programs inside neovim or just in integrated terminal inside neovim. I also use it with lf file manager, if you do not need lf part just remove the part with LF_ID and -f flag. Let's assume that script name is "nvt". #!/bin/bash
# VARIABLES:
# LF_ID: id of lf that should be quit inside neovim
# SELECT_LINE: line to select in editor
# SELECT_COL: column to select in editor
# FILE: file to open
usage() {
echo "nvt [-f lf-id] [-l line] [-c column] <path>"
exit 1
}
open_with_nvr() {
command="<c-\\><c-n>:lua USER_EXTERNAL.open_file_from_toggleterm(\"${FILE}\", \"${SELECT_LINE}\", \"${SELECT_COL}\")<cr>"
nvr --servername "$MY_NVIM_LISTEN_ADDRESS" --remote-send "$command"
}
parse_arguments() {
optstring=":f:l:c:"
arg_count=0
while getopts ${optstring} arg; do
case "${arg}" in
f)
arg_count=$((arg_count + 2))
if [ -z "$LF_ID" ]; then
LF_ID="${OPTARG}"
fi
;;
l)
arg_count=$((arg_count + 2))
if [ -z "$SELECT_LINE" ]; then
SELECT_LINE="${OPTARG}"
fi
;;
c)
arg_count=$((arg_count + 2))
if [ -z "$SELECT_COL" ]; then
SELECT_COL="${OPTARG}"
fi
;;
:)
echo "ERROR: -${OPTARG} requires an argument" 1>&2
usage
;;
?)
echo "Invalid option: -${OPTARG}."
usage
;;
esac
done
if [ "$#" -le "$arg_count" ]; then
echo "File path is not specified"
usage
fi
FILE="${*: -1}"
FILE="$(realpath "$FILE")"
}
parse_arguments "$@"
# NVIM_LISTEN_ADDRESS will be deleted by nvim
if [ -z "$MY_NVIM_LISTEN_ADDRESS" ]; then
shift "$arg_count"
if [ -n "$SELECT_LINE" ] && [ -n "$SELECT_COL" ]; then
start_command="+${SELECT_LINE}zv${SELECT_COL}|"
elif [ -n "$SELECT_LINE" ]; then
start_command="+${SELECT_LINE}"
fi
if [ -n "$start_command" ]; then
nvim "$start_command" "$@"
else
nvim "$@"
fi
else
open_with_nvr "$@"
# quit lf
if [ -n "$LF_ID" ]; then
lf -remote "send $LF_ID quit"
fi
fi The USER_EXTERNAL.open_file_from_toggleterm is custom function defined in my neovim config. It will open files with specific line and column, if you do not need that just replace command variable with command="<c-\\><c-n>:e $FILE<cr>" or something like that. This is the mentioned function in my neovim config function USER_EXTERNAL.open_file_from_toggleterm(path, line, col)
if vim.bo.filetype == "toggleterm" then
require("toggleterm").toggle()
end
vim.cmd("e " .. path)
vim.defer_fn(function()
-- Calculate correct column and line, convert to number
local col_num = tonumber(col)
if col_num ~= nil then
col_num = col_num - 1
if col_num ~= nil and col_num < 0 then
col_num = 0
end
end
local line_num = tonumber(line)
if line_num ~= nil and col_num ~= nil then
-- if line and column are available set cursor
local ok, error = pcall(vim.api.nvim_win_set_cursor, 0, { line_num, col_num })
if not ok then
print("Failed to move to cursor with line and column:", error, line_num, col_num)
end
elseif line_num ~= nil then
-- if only line is available set cursor to begginging of the line
local ok, error = pcall(vim.api.nvim_win_set_cursor, 0, { line_num, 0 })
if not ok then
print("Failed to move to cursor with line only:", error, line_num, col_num)
end
end
end, 100)
end And finally in lazygit config I have
Maybe this is too complicated but it works for me. |
Beta Was this translation helpful? Give feedback.
-
I am using lazygit in a terminal window of neovim.
I wonder, can I configure it in way, so that when I press
e
to edit a file, it doesn't open a new neovim instance inside a terminal inside neovim, but just opens that file in a buffer in the main neovim instance?Beta Was this translation helpful? Give feedback.
All reactions