I am trying to write vim function which should make window to go to previous open buffer. How can I do it from command line? My first suggest was to find out some CTRL-O command line alternative.
The jumplist is not the best way to do buffer navigation. Vim has a great number of buffer navigation tools which are probably better used for this task.
<c-^>
/<c-6>
- jump to the alternative file or if providing a count to a certain buffer number. See:h CTRL-6
:b
- jump to a specific buffer. Takes buffer number or partial filename. See:h :b
:sb
- split variant of:b
:ls
/:buffers
- list opened buffers. See:h :ls
#
used to represent the alternative file which is often the previous buffer. e.g.:b#
. See:h c_#
- Common mapping
nnoremap <leader>b :ls<cr>:b<space>
If you really do want to use the jump list you may want to look at Ingo Karkat's EnhancedJumps plugin or see vim-buffer-history plugin which is sort of similar.
You can also use Ctrl + o
to go to the previous buffer, going backwards. You can keep pressing the same command to go to the previous, previous buffer.
You can also use Ctrl + i
to go to the next buffer, going forward. I.e, if you had pressed Ctrl + o
to go to the previous buffer, you can use Ctrl + i
to go to the first/original buffer.
This works for me, it takes me back to the alternate
buffer. For me that is always the previous buffer:
function! MoveBack()
edit #
endfunction
I found the solution in use of normal
command. There would be such a line in my function:
exe "normal \<C-O>"
-
2That doesn't go to previous open buffer. It goes to the last position in your jump list which could be in the same buffer. What about
Ctrl-^
? – B Layer Dec 4 '17 at 10:06 -
1Also you shouldn't need
exe
. Trynormal! ^O
where^O
is inserted by hitting Ctrl-V followed by Ctrl-O. – B Layer Dec 4 '17 at 10:10 -
good point. If i_Ctrl-v occupied, see
:h i^v
for alternative (usually Ctrl-q). Another approach is usedigraph
, in this case, in insert mode, first type Ctrl-k, then type Ctrl-o twice will insert Ctrl-o. – qeatzy Dec 5 '17 at 2:52
b
in normal mode)? – Martin Tournoij Dec 4 '17 at 9:14