从 Linux 终端重用 Vim 实例

发布于 2024-07-23 15:30:30 字数 334 浏览 5 评论 0原文

通过利用 vim 中的远程功能,是否可以重用 vim 实例来根据需要加载多个文件。

如果能在同一个终端实例中拥有该功能,那就太好了。

我更热衷于有一个基于选项卡的界面,它在 vim 7+ 中可用 场景应该是

  1. 打开终端
  2. vim file1.cpp
  3. 编辑 - 保存 - Ctrl+Z 提示
  4. 打开另一个文件
    • vim file2.cpp
  5. 现在我们在同一个编辑器中打开了 file1.cpp 和 file2.cpp

这可能吗?!

By making use of the remote feature in vim, is it possible to reuse an instance of vim to load up multiple files as needed.

It will be nice to have that facility from within the same terminal instance.

I am more keen to have a tab based interface, which available in vim 7+
The scenario should be

  1. Open terminal
  2. vim file1.cpp
  3. Edit - Save - Ctrl+Z to get to prompt
  4. open another file
    • vim file2.cpp
  5. Now we have file1.cpp and file2.cpp open in the same editor

Is that possible?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一身骄傲 2024-07-30 15:30:30

我不确定这是否可以完全按照您指定的方式完成,但是使用在本地计算机上运行的 vim 服务器可能可以完成非常类似的事情。

在 Vim 中查看 :help remote.txt

如果您的 vim 版本是使用 +clientserver 编译的,您可以使用 vim 创建一个 vim 服务器,然后在其上执行命令,例如打开另一个文件。
--servername 开关可以创建新服务器,--remote 开关可以向其发送其他命令或文件。

例如

vim --servername test file1.txt
vim --servername test --remote file2.txt

我已经看过了,我在一台计算机上的 xubuntu 上作为标准使用的 vim 没有它,但是有一些说明 此处 如果您已编译它,可能会有所帮助。 如果不是,安装 gvim 和符号链接显然是一个选项(因为 gvim 默认包含它),或者从源代码编译二进制文件。

编辑:
我对 gvim 进行了更多的尝试,但看起来不可能在终端中做到这一点。 Control-Z 在进程级别暂停作业。 我认为它可能适用于屏幕,但除非 gvim 在图形窗口中启动,否则似乎不会发生任何通信,

I'm not sure if this can be done in exactly the manner that you're specifying, but something very similar can probably be done using a vim server running on your local machine.

Look into the :help remote.txt in Vim.

If your version of vim was compiled with +clientserver you can use vim to create a vim server, and then execute commands on it, e.g. opening another file.
The --servername switch can create a new server, and the --remote switch can send additional commands or files to it.

e.g.

vim --servername test file1.txt
vim --servername test --remote file2.txt

I've had a look, and the vim I'm using as standard on xubuntu on one of my computers doesn't have it, but there are some instructions here that may help if yours has it compiled. If it isn't, installing gvim and symlinking is apparently an option (as gvim has it included by default), or compiling the binaries from source.

Edit:
I've had more of a play with gvim and this doesn't look possible to do this within the terminal. Control-Z suspends the job at the process level. I thought it might work with screen, but no communication seems to take place unless gvim has launched in a graphical window,

水水月牙 2024-07-30 15:30:30

如果您使用 +clientserver 编译 VIM,这很容易做到,如 Andy 在之前的回答中建议

我执行了以下操作:

我将 VIM 作为服务器启动:

vim --servername abc

我使用 CTRL+Z 暂停它并执行以下操作:

vim --servername abc --remote ~/.cshrc
fg 

现在 VIM 已打开 ~/.cshrc

然后我做了:

vim --servername abc --remote ~/.aliases
fg 

现在 VIM 有一个带有 ~/.cshrc 的缓冲区,另一个带有 ~/.aliases 的缓冲区。

然后我这样做了:

vim --servername abc --remote-tab ~/foo_bar
fg 

现在,我的 VIM 有一个选项卡,其中打开了两个先前的缓冲区,而另一个选项卡则打开了 ~/foo_bar

在调用情况下,VIM 在终端中运行,而不是作为 GUI 运行。

This is easy to do if you compiled VIM with +clientserver, as Andy suggested in a previous answer.

I did the following:

I started up VIM as a server:

vim --servername abc

I suspended it with CTRL+Z and did:

vim --servername abc --remote ~/.cshrc
fg 

Now VIM had ~/.cshrc open.

Then I did:

vim --servername abc --remote ~/.aliases
fg 

Now VIM had one buffer with ~/.cshrc and another with ~/.aliases.

Then I did:

vim --servername abc --remote-tab ~/foo_bar
fg 

And now I VIM had one tab with the two previous buffers open and another tab with ~/foo_bar open.

In call cases VIM was running in the terminal, not as a GUI.

向日葵 2024-07-30 15:30:30

我有一些建议给你,尽管这两个建议都不是你所说的。 第一个是 NERD Tree,它为您提供了一个很好的基于树的文件浏览器用于打开其他文件。 非常便利。 我还设置了一个热键 (ctrl+o) 来打开 书呆子树 对我来说。 我在 ~/curr/trunk 中保留我正在进行的任何项目的别名,因此这对我来说总是有效:

map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>

我建议的另一件事是取消 ctrl+z。 它有点笨重,我认识的每个使用该方法的人都容易迷失方向,最终导致 3 或 4 个 vim 在后台运行。 看看如何为自己打开一个外壳。 我使用 ;s 的映射来执行 shell 脚本:

map ;s :!~/.vim/shell.sh<CR>

执行:

#!/bin/sh
/usr/local/bin/bash -l;

我的 .profile 中也有一些魔法,可以让我明显地看到我在 VIM 中:

if [ "$VIMRUNTIME" != "" ] ; then
    export PS1="\u@\h \W \t$ vim> "
fi

I have a couple suggestions for you, though neither is exactly what you're talking about. The first is NERD Tree, which gives you a nice tree-based file browser for opening other files. Very handy. I also set up a hot key (ctrl+o) to open NERD Tree for me. I keep an alias of whatever project I'm on in ~/curr/trunk, so this always works for me:

map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>

The other thing that I would suggest is to do away with ctrl+z. It's somewhat clunky, and everyone I know who uses that method tends to get lost and end up with 3 or 4 vim's running in the background. Take a look at how to open a shell for yourself. I use a map for ;s to execute a shell script:

map ;s :!~/.vim/shell.sh<CR>

Which executes:

#!/bin/sh
/usr/local/bin/bash -l;

I also have a bit of magic in my .profile for making it obvious I'm in VIM:

if [ "$VIMRUNTIME" != "" ] ; then
    export PS1="\u@\h \W \t$ vim> "
fi

</2 cents>

勿忘初心 2024-07-30 15:30:30

您可以按以下方式拆分当前屏幕并打开两个(或更多)文件:

对于水平拆分,请执行:

:sp 'filename'

对于垂直拆分,请执行:

:vsp 'filename'

要在两个屏幕之间切换,请按 ctrl+w,然后使用箭头键导航到您要编辑的任何文件。

另外,如果您只想切换文件(并且只打开一个文件),您可以这样做:

:e 'filename'

You can split the current screen and open two (or more) files in the following way:

For a horizontal split, do:

:sp 'filename'

and for a vertical split, do:

:vsp 'filename'

To tab between the two, hit ctrl+w, then use an arrow key to navigate to whichever file you want to edit.

Also, if you want to just switch files (and only have one open), you can do this:

:e 'filename'
寂寞清仓 2024-07-30 15:30:30

G'day,

或者如果您想拥有多个文件,但一次使用整个 vim 窗口处理一个文件,您只需输入即可

:e 'filename'

打开新文件。 您可以多次执行此操作。 要查看当前打开的文件,请输入

:ls

要在打开的文件之间切换,可以使用 cntl-^ (shift-cnt-6),这将在主文件和辅助文件之间交替(用 % 和# 在文件列表中)

或者您可以输入

:n b

其中 n 是您想要在“ls”命令显示的列表中的文件开头的编号。

HTH

欢呼,

G'day,

Or if you want to have multiple files but use the whole vim window for one file at a time you can just enter

:e 'filename'

to open the new file. You can do this multiple times. To see what you've currently got open, enter

:ls

To bounce between the files you've got open you can use cntl-^ (shift-cnt-6) and that will alternate between the main and secondary files (shown with a % and # in the file list)

Or you can enter

:n b

where n is the number at the beginning of the file you want in the list shown by the 'ls'command.

HTH

cheers,

独﹏钓一江月 2024-07-30 15:30:30

你可以在 vim 上设置隐藏功能,以允许它打开多个文件:

:set hidden

然后你可以打开任意多个文件,而不必每次要切换“缓冲区”时都保存它们":

:e 'filename'

您有几个命令来导航缓冲区:

:bnext
:bprev
:buffers

当然,正如 samoz 提到的,您可以拆分屏幕以在同一窗口上查看多个缓冲区。

我建议阅读这个关于vim的一个很好的介绍,它会拯救你很多时间。

祝你好运!

You can set the hidden feature on vim, to allow it to have multiple files open:

:set hidden

Then you can open as many files as you want, without bothering to save them every time you want to switch a "buffer":

:e 'filename'

You have several commands to navigate the buffers:

:bnext
:bprev
:buffers

Of course, as mentioned by samoz, you can split your screen to see multiple buffers on the same window.

I'd suggest to read this for a good introduction about vim, it will save you a lot of time.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文