user1299784
user1299784

Reputation: 2097

Vim go to previous cursor position when switching to a buffer

I switch from buffer 1 to buffer 2 and my cursor is at line 100. I use j/k to scroll around and land at line 120. Then I use ctrl-O to switch back to buffer 1, and then ctrl-I to go to buffer 2.

I expect that the cursor should be at line 120, as that is where I left off, but to my annoyance, the cursor is at line 100. How can I get vim to go to line 120?

Upvotes: 2

Views: 1080

Answers (1)

romainl
romainl

Reputation: 196926

The commands you are using are working as advertised. The problem is that you don't use the right commands for what you are trying to do.

<C-o> and <C-i> are not related to buffers at all. Their job is to let you move up and down the jump list, no more no less. The fact that they lead you to another buffer is a side effect. Don't rely on side effects: if you don't want to navigate the jump list, don't use them.

If you need to alternate between two buffers, the right command is <C-^> (or <C-6> on some keyboards).

If you need to switch to a specific buffer, use :b <tab> or :ls followed by :bn with n being the number of the desired buffer.

Upvotes: 7

Related Questions