I’ve said this a thousand times. But I won’t be tired of saying that once again. Vim is just too good to have a single command assigned to a single task. Once again, Vim proves how fancy it could get with its Modal-Editing scheme. This time, we’re talking about Ctrl + O. No. It won’t help you open files in Vim like it does elsewhere. But it does 2 cool things that’ll give you some stories to tell your grandkids. “Back in my day when we used Vim…”
But hold on. Vim is not going anywhere anytime soon. You grandkids may have children who could possibly end up using Vim.
Change position in a Jump list
Vim has a feature called the “Jump List”, which saves all the locations you’ve recently visited, including their line number, column number, and what else not in the .viminfo file, to help you get exactly the position you were last in. Not only does it save the locations in your current buffer, but also previous buffers you may have edited in other Vim sessions. Which means, if you’re currently working on a file, and there aren’t many last-location saves in this one, you’ll be redirected to the previous file you had edited. But how do you do that? Simply press Ctrl + O, and it’ll get you back to the previous location you were in, or more specifically, your cursor was in.
If you want to go back to the newer positions, after you’re done with what you wanted to do, you can then press Ctrl + i to go back to the newer position. This is exceptionally useful when you’re working with a lot of project files at a time, and you need to go back and forth between multiple blocks in different files. This could instantly give you a boost, as you won’t need to have separate buffers opened up or windows to be setted up, you can simply jump between the files and edit them. Ain’t that crisp and fresh, huh?
Quick command input with Ctrl + O
Ctrl + O is probably not meant for a single task, as far as Vim’s philosophy is concerned. The jumping mentioned in the previous section only works when you’re in Normal Mode, and not when you’re in Insert Mode. When you press Ctrl + O in Insert Mode, what happens instead is that you’ll enter Normal Mode, and be able to execute a single command, after which Vim will automatically switch back to Insert Mode. Unable to interpret what I’m tellin’ ya?
Hopefully now you do! What I essentially did here, is that first I pressed Ctrl + O in Insert Mode, which gets me to the normal mode to allocate me a single task to be executed. So I then carry out the task I want, which in my case was to delete the current line and start over, after which Vim automatically sends me to Insert Mode.
Remap ctrl + O to open Explorer
Some of us folks may not really want to use the Quick Command Input feature in Vim Insert Mode, and would wish to open a file instead, like they conventionally do in most other programs.
You cannot directly create a function which will prompt you to open a file like it would elsewhere, because to open a file when you’re inside Vim, you’ll need to specify the full path to the file, and assigning a fixed command to Ctrl + O won’t allow you to open different files with different paths on demand. The only work around for this would be to use Vim’s built-in plugin Netrw, instead. It may not be as cool and breezy, but it should get your job done. To do this, add the following in in your .vimrc:
inoremap <C-o> <Esc>:Explore<CR>
You’ll see that a sort of File Explorer pops up in a separate Window above the current file in Vim. From there, you can navigate to the file you want to open and press Enter to open that file in that window where the file explorer started. To focus only on that file, and close the other file you were previously editing, first navigate to that by pressing Ctrl + ww, and then save the file and exit it. Once you exit that file, you’ll see the pane closes and you;re only left with the window where you opened the new file.
Using the File Explorer method is just a workaround for those who’re constantly used to opening files with the shortcut. Because for the most part, you’d probably not want to mess around with the Vim mappings until you really need to. On the other hand, the already mapped quick command input feature is quite useful once you start using it. It is also quite essential to map normal mode commands in Insert Mode that involves coming to the Normal Mode only for once to execute a command. Nevertheless, it all depends on how you wish to use it. No one shall blame you for that, nerdy cat.