有没有办法让 gVim 只运行一个实例,这样当用它打开一个新文件时,它会自动在当前运行实例的新选项卡中打开?
我知道你可以通过传递来做到这一点,--remote-tab-silent
但我想配置 gvim 以便这成为默认行为。即,我想键入gvim filename
并使其表现得好像我将--remote-tab-silent
选项传递给它一样。
gVim 7.2
编辑
我在窗户上(vista)
有没有办法让 gVim 只运行一个实例,这样当用它打开一个新文件时,它会自动在当前运行实例的新选项卡中打开?
我知道你可以通过传递来做到这一点,--remote-tab-silent
但我想配置 gvim 以便这成为默认行为。即,我想键入gvim filename
并使其表现得好像我将--remote-tab-silent
选项传递给它一样。
gVim 7.2
我在窗户上(vista)
如果您使用的是 bash shell(在 Linux/OS X/使用 Cygwin 上),请添加您的~/.bashrc
文件:
gvim () { command gvim --remote-silent "$@" || command gvim "$@"; }
在 Windows 上,我认为您可以使用gvim.bat
批处理脚本来实现相同的效果。
gvim.exe -p --remote-tab-silent %1 %*
如果 gvim.exe 不在您的路径中
Run > Search "Environment"
编辑当前用户或系统的 PATH 变量。
这取决于您的操作系统和外壳。使用 linux,您始终可以设置一个别名,例如:
alias gvim='gvim --remote-tab-silent'
在你的.bashrc
(如果你使用 bash 作为你的登录 shell)。
在 Windows 上,请参阅 Vim wiki 以获取解决方案:http: //vim.wikia.com/wiki/Launch_files_in_new_tabs_under_Windows。
我发现--remote-tab-silent
别名中的选项大部分都可以工作,除非我想将选项传递给 gvim(例如gvim --serverlist
)-在这种情况下,gvim 将选项视为文字文件名,这不好,因为首先那不是什么你想要,其次你必须从你现在被污染的 vim 会话中清理缓冲区。
对于以下某些情况,使用另一个别名或以不同方式解析vim
/是不切实际的。gvim
gvim
gvim /path/to/files
gvim --serverlist
gvim -p /path/to/file1 /path/to/file2
gvim -t tag filename
正如Tom Veiner建议的那样,我的解决方案是以下围绕gvim
( ~/.bin/gvim
)的包装脚本,但如果没有任何参数是选项,这只会使用现有服务器- 否则,将创建一个新服务器。gvim
#!/usr/bin/perl
use v5.10;
sub gvim { exec { '/usr/bin/gvim' } '/usr/bin/gvim', @_; }
if (scalar @ARGV) {
unshift @ARGV, '--remote-tab-silent' unless /^--?/ ~~ @ARGV;
gvim @ARGV
}
else {
chomp(my $serverlist = `gvim --serverlist`);
if (length $serverlist) {
gvim '--remote-send', '<Esc>:tabnew<CR>'
} else { gvim }
}
alias tvim="gvim --servername `gvim --serverlist | head -1` --remote-tab"
这使 vim 在同一 vim 实例的新选项卡中打开一个新文件。资料来源:http ://eustaquiorangel.com/posts/477
我有点厌倦了在 cygwin + windows 中解决这个问题,所以我终于做了一些事情。我从上面定义的包装脚本开始,但我最终使它变得更加健壮,并且能够为 *nix 和 Win 提供多环境支持。
#!/bin/bash
#bash wrapper for windows/cygwin gvim
#####################################################################
## Cygwin/*nix and Windows gvim wrapper script, alias friendly, path friendly
## Author: Matt Gregory (skyleach (AT) geemale (dot) com)
## Version: 1.5
## Date: Thu Jun 12 10:02:54 2014
## Known Bugs:
## Changes:
## Thu Jun 12 10:04:08 2014 : Initital posting to StackOverflow
#####################################################################
[[ -z ${WINVIM} ]] && WINVIM=true
[[ -z ${VIMRUN} ]] && export VIMRUN='' #scoping
if [[ ${WINVIM} = false ]]; then
[[ ! ${VIMRUN} ]] && VIMRUN='/bin/gvim'
ANS=$("${VIMRUN}" --serverlist | grep GVIM)
else
[[ ! "${VIMRUN}" ]] && VIMRUN='/cygdrive/c/Program Files/vim/vim74/gvim'
ANS=$(ps -Wsl | grep "${VIMRUN}" | sed -e "s/\s\+\([0-9]\+\).*/\1/g")
fi
[[ ! -z ${VIM} && ${WINVIM} = true ]] && export VIM=$(cygpath -wal "${VIM}")
RT="--remote-tab"
[[ $ANS ]] || unset RT
if [ ! -z ${DEBUG} ]; then
echo "WINVIM: ${WINVIM}"
echo "VIMRUN: ${VIMRUN}"
echo "ANS: ${ANS}"
echo "VIM: ${VIM}"
fi
#process arguments or stdin
if [ ${#} -ne 0 ]; then
[[ ! -z ${DEBUG} ]] && echo "Got arguments [${#}]:'${@}'"
for OFILE in "${@}"; do # if [ -f "${OFILE}" ] || [ -d "${OFILE}" ]; then
[[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
[[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
"${VIMRUN}" --servername GVIM $RT "${OFILE}" &
if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
RT="--remote-tab"
sleep 5 #give gvim time to start up, running too fast messes things up
else
sleep .3 #shorter sleep for tabs
fi
#fi
done
else
while read OFILE; do
[[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
[[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
"${VIMRUN}" --servername GVIM $RT "${OFILE}" &
if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
RT="--remote-tab"
sleep 3 #give gvim time to start up, running too fast messes things up
else
sleep .3 #shorter sleep for tabs
fi
done
fi
# vim: set filetype=sh:
如何有效地使用它:
将某些命令别名为 cygwin 和/或 windows gvim,如下所示:
echo "alias gwrap='WINVIM=false ~/src/localscripts/wgwrap'" >> ~/.bashrc echo "alias wgvim='wgwrap'" >> ~/.bashrc
注意:如果 gvim 的硬编码路径对您的系统不正确,您可以直接编辑脚本、别名和/或添加环境变量 WINVIM 和/或 VIMRUN。您可以像我在上面对 gwrap 所做的那样在别名中设置它们,或者您可以将它们添加到您的 .bashrc 或 Windows 系统环境中。
我经常发现我想转到文件中的特定位置。我发现这样做的唯一方法是:
gvim --remote-send '^[:tabnew +$lineno $filename ^M'
其中^[
是 esc(键入 ctrl-v esc)并且^M
是 enter(ctrl-v enter)。
希望这可以帮助。如果有人知道如何使用--remote-expr,请给我留言。
我正在使用 TotalCommander 进行文件浏览。它允许您按“F4”来编辑文件。因此,在“F4 编辑器”选项窗口中,您只需执行以下操作:
C:\Program Files (x86)\Vim\vim73\gvim.exe --remote-tab-silent
在我的 win7 64 位机器上工作。
上面的解决方案不会在第一次执行时启动 gvim 服务器,所以我使用:
ANS=`pgrep -fx "$VIM"`
# Launch server if needed
if [[ ! $ANS ]]; then
$VIM
fi
# Now open the file
if [[ $1 ]]; then
$VIM --remote-tab "$@"
fi
不适用于 WINDOWS
我使用 MacVim(快照 73)。
将此添加到您的 .bash_profile。
它不会生成“NO NAME”和错误消息。
vi() {
if [[ `mvim --serverlist` == 'VIM' ]]; then
command mvim --remote-tab-silent "$@"
else
command mvim "$@"
fi
}
使用 MacVim,我发现默认的服务器名称只是 VIM。坚持这个主题,我在我的 bashrc 中加入了以下函数,它就像一个魅力:
mvim() { ~/bin/mvim --servername VIM --remote-tab-wait-silent $* & }
gvimt () { [ $# -ne 0 ] && command gvim --remote-silent-tab $@ || command gvim $@; }
我实际上正在使用
gvim () {
if [ "$#" -gt 0 ]
then
gvim --remote-tab-silent "$@" &
else
gvim "$@" &
fi
}
它杀死错误并且仅在有参数时使用标志
更改注册表中的路径
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\gvim.exe\shell\edit\command
"Path2gvim\gvim.exe" "%1" -->
"Path2gvim\gvim.exe" --remote-tab-silent "%1"