'Vim: can global marks switch tabs instead of the file in the current tab?

I'm in the process of learning vim, and i just learned about marks. Before this i found it useful to have all the source code files i'm working on in their own tabs. When i found out about "global" (capital letter) marks i thought it would be a great way to switch to a tab which already has the marked file open, and scroll to the correct spot in a quick way. However, i found out that jumping to a mark in a different file simply changes the file which the current tab is displaying, and this messes up my tabs setup. Is there a way to make the marks work with the tabs in the way that i want?



Solution 1:[1]

The problem is that the mark-jumping commands are designed to move to the mark within the current window as there can theoretically be many windows to the same file. You need to switch to a new window first using :sbuf or :tabnext or CTRL+WW. If you have set switchbuf=useopen,usetab then using :sbuf <otherfile> first will be sufficient to jump to the other tab where your file is open. But 'A will not create a new window for you (or re-use an existing one in another tab).

You can probably create a mapping for ' and ` which uses getpos(), setpos(), :sbuf and switchbuf to jump to an existing window in another tab, but it would involve writing a page of vimscript.

See :help switchbuf and :help getpos() and :help setpos().

Solution 2:[2]

Tabs may not be the best way to do what you are trying to do. When a file is open, it isn't necessarily open in just one tab. It's open in a buffer, which is a concept not tied to a tab.

In fact, you can have the same buffer open in multiple tabs (or even multiple panes within the same tab). A tab is more like a window into one or more of your currently open buffers.

It may be better to learn about how to switch between buffers in your current tab or pane. Just a suggestion.

Solution 3:[3]

    nno \ <Cmd>call To_global_mark()<cr>
    fun! To_global_mark()
       -tab drop /tmp/useless.md
        exe 'normal! `' .. toupper(input("To global mark: "))
    endf

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blues
Solution 2 thomasrutter
Solution 3 Good Pen