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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/894811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:12:32  来源:igfitidea点击:

Open files in existing Gvim in multiple (new) tabs

bashubuntuvimtabsalias

提问by kajaco

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 中打开一堆空标签来解决这个问题,这使我可以右键单击文件名并选择“在现有的无名 gvim 中打开”。现在我使用 Ubuntu,上下文菜单上没有这样的东西。有什么办法可以从命令行执行此操作吗?

回答by Ayman Hourieh

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

如果 vim 是用clientserver选项编译的,你可以这样做。使用以下标志启动您的 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 中的客户端服务器功能非常方便。它不仅限于打开文件;它可用于使用命令行向 vim 发送任何命令。例如,要远程关闭 vim 实例,您可以使用:

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

回答by Steve K

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.

这将打开一个新的空白标签页并打开文件浏览器。你可以用鼠标点击你的方式或使用键盘。单击或点击要打开的文件上的 Enter 键。尝试使用键盘将光标放在要打开的文件上,然后按“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.

回答by matt burns

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:

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

vim context menu

vim 上下文菜单

回答by Jan Valiska

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 [[  ]]; 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

回答by user1021852

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,但没有它就行不通。

回答by Serge Stroobandt

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

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

  • gvimis called without filename when no gviminstance is running; gvimis started.
  • gvimis called without filename in the presence of a gviminstance; an empty new tab is opened.
  • gvimis called with a filename with or without a gviminstance; a tab is opened showing the file.
  • gvim当没有gvim实例运行时,在没有文件名的情况下调用;gvim开始了。
  • gvimgvim实例存在的情况下不带文件名调用;打开一个空的新选项卡。
  • 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