在多个(新)选项卡中打开现有Gvim中的文件

时间:2022-01-11 00:13:39

I have put some aliases in my .bashrc to open a group of project files in gvim, each in their own tab:

我在.bashrc中放了一些别名,在gvim中打开一组项目文件,每个项目都在自己的标签中:

gvim -p <list of file names using absolute paths>

This is all well and good, except there are several groups of files I might want to move between at any given time (my current project uses Ruby on Rails, so that explains that). What would be really awesome is if I could append the new tabs to an existing instance of gvim. In my last position I worked on Vista; I got around this by opening a bunch of empty tabs in gvim, which allowed me to right-click on a filename and choose "Open in existing No-Name gvim." Now I use Ubuntu and there's no such thing on the context menu. Is there any way to do this from the command line?

这一切都很好,除了我可能想要在任何给定时间之间移动的几组文件(我当前的项目使用Ruby on Rails,所以这解释了)。如果我可以将新选项卡附加到现有的gvim实例,真正棒极了的是什么。在我的最后一个位置,我在Vista上工作;我通过在gvim中打开一堆空标签来解决这个问题,这让我可以右键单击文件名并选择“在现有的No-Name gvim中打开”。现在我使用Ubuntu,在上下文菜单中没有这样的东西。有没有办法从命令行执行此操作?

6 个解决方案

#1


If vim is compiled with the clientserver option, you can do it. Start your vim instance with the following flag:

如果使用clientserver选项编译vim,则可以执行此操作。使用以下标志启动vim实例:

$ gvim --servername GVIM  # GVIM is the server name. It can be anything.

To open more tabs in this instance, you can run the command:

要在此实例中打开更多选项卡,可以运行以下命令:

$ gvim --servername GVIM --remote-tab file1 file2 file3 ...

The clientserver feature in vim is very handy. It's not limited to opening files; it can be used to send any command to vim using the command-line. For example, to close a vim instance remotely, you can use:

vim中的clientserver功能非常方便。它不仅限于打开文件;它可以用于使用命令行向vim发送任何命令。例如,要远程关闭vim实例,您可以使用:

$ gvim --servername GVIM --remote-send '<Esc>:wqa<CR>'

#2


From inside of Gvim, type :tabe {file_name}. This opens the named file in a new tab. If you aren't fond of typing long filenames, try this:

从Gvim内部输入:tabe {file_name}。这将在新选项卡中打开命名文件。如果您不喜欢输入长文件名,请尝试以下操作:

:tabnew
:e .

This will open a new, blank tab page and open a file browser. You can mouse click your way around or use the keyboard. Click or hit the enter key on the file you want to open it. Try using the keyboard to position the cursor over the file you want to open and then hit 't'. This opens the selected file in a new tab, keeping the file browser open in the first tab. This might be a fast way to open a bunch of files.

这将打开一个新的空白标签页并打开文件浏览器。您可以通过鼠标单击或使用键盘。单击或按下要打开它的文件上的回车键。尝试使用键盘将光标放在要打开的文件上,然后点击“t”。这将在新选项卡中打开所选文件,从而在第一个选项卡中保持文件浏览器处于打开状态。这可能是打开一堆文件的快捷方式。

There are a whole lot of things you can do with tab pages that might make life easier. To get to the relevant section in Vim's on line help manual, type :h tabpage.

使用标签页可以做很多事情,可以让生活更轻松。要访问Vim在线帮助手册中的相关部分,请键入:h tabpage。

#3


Want your Windows context menu to allow you to open files in a new tab of the currently open gvim window?

想要Windows上下文菜单允许您在当前打开的gvim窗口的新选项卡中打开文件吗?

Save this as as a file called temp.reg and double-click it to add the settings to your registry. Be sure to modify the path to vim if yours is different.

将其另存为名为temp.reg的文件,然后双击它以将设置添加到注册表。如果您的路径不同,请务必修改vim的路径。

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim]
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim\command]
@="\"C:\\Program Files (x86)\\Vim\\vim73\\gvim.exe\" -p --remote-tab-silent \"%1\" \"%*\""

You will now have a context menu like this:

您现在将拥有这样的上下文菜单:

在多个(新)选项卡中打开现有Gvim中的文件

#4


Linux users may use this kind of script:

Linux用户可能会使用这种脚本:

#!/bin/bash

ANS=`pgrep -fx "gvim --servername GVIM"`

echo $@

if [[ ! $ANS ]]; then
    gvim --servername GVIM
fi

if [[ $1 ]]; then
    gvim --servername GVIM --remote-tab "${@}"
fi

And then edit gvim.desktop file for using this script:

然后编辑gvim.desktop文件以使用此脚本:

Exec=/home/user/bin/my_gvim_script.sh %F

#5


There is a way:

有一种方法:

n*.cpp|tab ba

or if you like to split:

或者如果你想拆分:

n*.cpp|sba

If you wish to know more:

如果您想了解更多:

:help ba

and I don't know what is n , but it would not work without it.

我不知道n是什么,但没有它就行不通。

#6


Here is my gvim start-up script. It is an extension of previous answers. It ensures only one gvim instance will run when gvim is called under all circumstances:

这是我的gvim启动脚本。它是以前答案的延伸。它确保在所有情况下调用gvim时只运行一个gvim实例:

  • gvim is called without filename when no gvim instance is running; gvim is started.
  • 没有运行gvim实例时,不使用文件名调用gvim; gvim开始了。

  • gvim is called without filename in the presence of a gvim instance; an empty new tab is opened.
  • 在存在gvim实例的情况下,在没有文件名的情况下调用gvim;打开一个空的新选项卡。

  • gvim is called with a filename with or without a gvim instance; a tab is opened showing the file.
  • 使用带或不带gvim实例的文件名调用gvim;将打开一个显示文件的选项卡。

This will mimic the standard behaviour of other editors.

这将模仿其他编辑器的标准行为。

#!/bin/bash
exec=/usr/bin/gvim  #the path to gvim
if [ $# -eq 0 ]
  then  # no filename given
    if [ -z $($exec --serverlist) ]
      then  # no filename given and no gvim instance
        $exec -f --servername GVIM > /dev/null 2>&1
      else  # no filename given, but a gvim instance exists
        $exec -f --servername GVIM --remote-send ':tabnew<CR>' > /dev/null 2>&1
    fi
  else  # filenames given
    $exec -f --servername GVIM --remote-tab "$@" > /dev/null 2>&1
fi

#1


If vim is compiled with the clientserver option, you can do it. Start your vim instance with the following flag:

如果使用clientserver选项编译vim,则可以执行此操作。使用以下标志启动vim实例:

$ gvim --servername GVIM  # GVIM is the server name. It can be anything.

To open more tabs in this instance, you can run the command:

要在此实例中打开更多选项卡,可以运行以下命令:

$ gvim --servername GVIM --remote-tab file1 file2 file3 ...

The clientserver feature in vim is very handy. It's not limited to opening files; it can be used to send any command to vim using the command-line. For example, to close a vim instance remotely, you can use:

vim中的clientserver功能非常方便。它不仅限于打开文件;它可以用于使用命令行向vim发送任何命令。例如,要远程关闭vim实例,您可以使用:

$ gvim --servername GVIM --remote-send '<Esc>:wqa<CR>'

#2


From inside of Gvim, type :tabe {file_name}. This opens the named file in a new tab. If you aren't fond of typing long filenames, try this:

从Gvim内部输入:tabe {file_name}。这将在新选项卡中打开命名文件。如果您不喜欢输入长文件名,请尝试以下操作:

:tabnew
:e .

This will open a new, blank tab page and open a file browser. You can mouse click your way around or use the keyboard. Click or hit the enter key on the file you want to open it. Try using the keyboard to position the cursor over the file you want to open and then hit 't'. This opens the selected file in a new tab, keeping the file browser open in the first tab. This might be a fast way to open a bunch of files.

这将打开一个新的空白标签页并打开文件浏览器。您可以通过鼠标单击或使用键盘。单击或按下要打开它的文件上的回车键。尝试使用键盘将光标放在要打开的文件上,然后点击“t”。这将在新选项卡中打开所选文件,从而在第一个选项卡中保持文件浏览器处于打开状态。这可能是打开一堆文件的快捷方式。

There are a whole lot of things you can do with tab pages that might make life easier. To get to the relevant section in Vim's on line help manual, type :h tabpage.

使用标签页可以做很多事情,可以让生活更轻松。要访问Vim在线帮助手册中的相关部分,请键入:h tabpage。

#3


Want your Windows context menu to allow you to open files in a new tab of the currently open gvim window?

想要Windows上下文菜单允许您在当前打开的gvim窗口的新选项卡中打开文件吗?

Save this as as a file called temp.reg and double-click it to add the settings to your registry. Be sure to modify the path to vim if yours is different.

将其另存为名为temp.reg的文件,然后双击它以将设置添加到注册表。如果您的路径不同,请务必修改vim的路径。

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim]
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim\command]
@="\"C:\\Program Files (x86)\\Vim\\vim73\\gvim.exe\" -p --remote-tab-silent \"%1\" \"%*\""

You will now have a context menu like this:

您现在将拥有这样的上下文菜单:

在多个(新)选项卡中打开现有Gvim中的文件

#4


Linux users may use this kind of script:

Linux用户可能会使用这种脚本:

#!/bin/bash

ANS=`pgrep -fx "gvim --servername GVIM"`

echo $@

if [[ ! $ANS ]]; then
    gvim --servername GVIM
fi

if [[ $1 ]]; then
    gvim --servername GVIM --remote-tab "${@}"
fi

And then edit gvim.desktop file for using this script:

然后编辑gvim.desktop文件以使用此脚本:

Exec=/home/user/bin/my_gvim_script.sh %F

#5


There is a way:

有一种方法:

n*.cpp|tab ba

or if you like to split:

或者如果你想拆分:

n*.cpp|sba

If you wish to know more:

如果您想了解更多:

:help ba

and I don't know what is n , but it would not work without it.

我不知道n是什么,但没有它就行不通。

#6


Here is my gvim start-up script. It is an extension of previous answers. It ensures only one gvim instance will run when gvim is called under all circumstances:

这是我的gvim启动脚本。它是以前答案的延伸。它确保在所有情况下调用gvim时只运行一个gvim实例:

  • gvim is called without filename when no gvim instance is running; gvim is started.
  • 没有运行gvim实例时,不使用文件名调用gvim; gvim开始了。

  • gvim is called without filename in the presence of a gvim instance; an empty new tab is opened.
  • 在存在gvim实例的情况下,在没有文件名的情况下调用gvim;打开一个空的新选项卡。

  • gvim is called with a filename with or without a gvim instance; a tab is opened showing the file.
  • 使用带或不带gvim实例的文件名调用gvim;将打开一个显示文件的选项卡。

This will mimic the standard behaviour of other editors.

这将模仿其他编辑器的标准行为。

#!/bin/bash
exec=/usr/bin/gvim  #the path to gvim
if [ $# -eq 0 ]
  then  # no filename given
    if [ -z $($exec --serverlist) ]
      then  # no filename given and no gvim instance
        $exec -f --servername GVIM > /dev/null 2>&1
      else  # no filename given, but a gvim instance exists
        $exec -f --servername GVIM --remote-send ':tabnew<CR>' > /dev/null 2>&1
    fi
  else  # filenames given
    $exec -f --servername GVIM --remote-tab "$@" > /dev/null 2>&1
fi