Vim Tips, Tricks and useful .vimrc modifications (part 2)

JasKinasis

Super Moderator
Staff member
Gold Supporter
Joined
Apr 25, 2017
Messages
1,881
Reaction score
2,769
Credits
16,359
Welcome to part 2 of this series of tips, tricks and (hopefully) useful .vimrc modifications.
Last time we covered quite a lot of information and had a couple of useful mods.
  • A quick, easy and ergonomical way to exit Insert mode, or command mode without leaving the home row.
  • Creating custom key-binds
  • Remapping the leader key,
  • Reloading our .vimrc file whilst still in vim,
  • Quickly finding our cursor.

This time we'll cover some tips, tricks and .vimrc modifications to aid us with buffer and tab management.

In order to load multiple files into vim, you can simply provide a list of files, or use globbing.
e.g.
Bash:
vim file1 file2 file3 file4
vim file{1,2,3,4}
vim file*
vim *.{h,cpp}

And whenever we open several files in this way in Vim, each file we open is loaded into a different buffer and we typically find ourselves looking at the first file from the list of files we passed to vim.
All of the other files that were in the list are loaded into vim.

And the first question many vim users have is - "Where are the rest of my files and how can I access them?", or "How do I switch to another file/buffer?".
The answer to that question is:
Hit escape to ensure you're in Normal mode (or use jj if you've been following along with this series) and then enter the command :ls.
That will show you a numbered list of buffers that are currently loaded. Make a note of the number next to the buffer you want to switch to.
Then enter the command :b [number], where [number] is the number that was next to the buffer you want to switch to.
So you have one command to list all of the buffers and another command to switch buffers. Is there a more efficient way to do this?
Yes, there is. What we can do is add a keybind that will run that functionality for us.

Open up your .vimrc and add the following code:
Code:
map <leader>l :ls<CR>:b
IMPORTANT NOTE: There is a trailing space at the end of the line. You MUST remember to include a space at the end of that line!
My thinking behind using <leader>l is l for "list buffers" - you could set it to another key if you like.
Switch back to normal mode using <escape> (or jj) and save your .vimrc using :w.

If you followed my first post, reload your .vimrc file using the <leader>r key combo (by default the leader key is \, but if you've been following along, you may have redefined the leader key to , or something else). I use the default for my leader key so I'd use \r to reload my .bashrc.
And if you haven't been following along, use the command :source $MYVIMRC to reload your .vimrc. If you want to know more about that command - take a look at the previous article in the series.

Now you've reloaded your .vimrc - without quitting vim - let's open a new file using the command:
Code:
:e ~/deleteme.txt
Now we're in a new, blank file called deleteme.txt. The buffer containing our .vimrc is still in memory. So we have two files/buffers open.
How do we switch back to the buffer containing our .vimrc? Yup, you guessed it - we can use the <leader>l keybind we just defined.

Type <leader>l (where <leader> is whatever your current leader key is. The default is \. If you followed the previous article - you may have chosen to remap the leader key to ,).
Anyway, press <leader>l and you'll see a list of open buffers with numbers - you'll also see that we're in COMMAND mode and that the :b command has already been typed out in the command line. So all you have to do is enter the number of the buffer you want to switch to, or press enter to stay on the current file/buffer.

Our .vimrc should be the first buffer in the list, so to get back to it - we just need to enter the number 1.

From now on - if you have a lot of files open in vim and you want to quickly switch from one buffer to another buffer, you can use <leader>l to see a list of open buffers and then you can just enter the number of the file you want to switch to. Simple!
One thing to be aware of though - if you have made any changes to the current buffer, you must save the buffer with :w before attempting to switch to another buffer.
And if you choose to stay on the buffer you're already in, just press enter, or <escape> (or jj) to cancel!

This modification is great if you have a lot of buffers open and you want to see what files you have open when you switch. But if you only have a small number of buffers you might just want to cycle to the next/previous buffer. Is there a way that we can do that? The answer is - YES!

The commands for switching to the next/previous buffer are :bprev and :bnext. And a quick way to make those commands accessible to us would be to add keybinds for them.

So, switch to the buffer containing your .vimrc if you haven't already and then add the following to your .bashrc:
Code:
map <leader>m :bnext<CR>
map <leader>n :bprev<CR>

Again - save and reload your .vimrc. Now if you press <leader>m, you'll switch to the next buffer. Using <leader>n will switch to the previous buffer. Simple! For these particular shortcuts, I didn't have any mnemonic's for these shortcuts. m and n are directly below the home-row - so they're convenient and ergonomic. Again, you could set these to any other keys you want to use.

What if we've finished with a file and we want to close/unload a buffer from memory. Is there an easy way to do that?
Yes, there is!

The best thing to do in that situation is to use :ls to see a list of open buffers and make a note of the number of the buffer we want to close.
To close a buffer, we use :bd [number] where [number] is the number of the buffer we want to close.
OR - if we want to kill the current buffer, we use the command :bd - which will close the current buffer and switch us to the next one.
And to close without saving: :bd!
So, with that in mind - our next couple of modifications define some keybinds to allow us to quickly close a buffer:
Code:
map <leader>k :ls<CR>:bd 
map <leader>K :bd!<CR>
IMPORTANT NOTE:
There is a trailing space at the end of the first keybind, after the bd .
My thinking with this shortcut was to use k for "kill buffer" - but, you could remap it to <leader>c for "Close buffer".

You know the deal by now. Save your .vimrc and re-load it, to activate the new keybinds.
The first keybind <leader>k will bring up a list of open buffers and we can enter a buffer number to tell it which buffer to close, or just press enter to close the current buffer. But NOTE that if you're closing the current buffer, it will only work if the current buffer has been saved.
The second keybind <leader>K will just close the current buffer without saving it.

So now, I want you to use one of the above keybinds to close/unload/delete the buffer for ~/deleteme.txt.

As we saw earlier, when we opened up ~/deleteme.txt - opening a new buffer is as easy as typing :e /path/to/file - so there's not much point adding a shortcut for that.
So that's just about it for buffers.

Save your .vimrc and exit vim for a little while.

Tab management in vim
Next we'll look at opening files in tabs. If you've used any GUI text editors, tabs are something that you'll no doubt be very familiar with. It should be no surprise that vim also supports tabs. Tabs can be used in vim as you can in other editors, but that wasn't their original purpose. In Vim, most experienced users prefer to work with buffers and tabs are usually only used to provide different views of buffers - so one tab might have one buffer open, the next tab might have a split pane with one part of the same buffer open in one pane and another part of the buffer open in another pane. Or one split will have one buffer/file and the other split will have a different buffer/file. I won't go into the window splitting features of vim today.
But I will cover using tabs in the way that many people will already be familiar with.

To open files in tabs in vim, we use the -p option/switch and provide a list of files to open.
e.g.
Bash:
vim -p ~/file{1..4}

Now we should see four new, empty files open in tabs, called file1, file2, file3 and file4. And we should see in the tab-list at the top, that we're in file1.
To switch to the next tab, use the command :tabn. To switch to the previous tab, use the command :tabp
You can also re-arrange the tabs by using :+tabm to move a tab to the right, or :-tabm to move a tab to the left.
So if you want to make tab2 and tab3 switch places - use :tabn to switch to tab2, then use +tabm and tab2 will be moved to the right of tab3.
To switch their positions back again, use -tabm to move tab2 one place to the left.
And to open a file in a new tab, you can use the :tabnew command, optionally adding a path/filename for the file. If you don't specify a path/filename, you'll just have an unnamed buffer, which will have no name until you save it and specify a file-name for it.
Again - it's quite cumbersome having to remember all of these commands, so let's add some keybinds for them to make things faster.

So the first thing we'll do is quit out of vim.
As we have multiple buffers open, with empty files that we don't really want to save - the shortest way to quit out of vim without saving is :x! :x will automatically save all changes to files before exiting, so :x! tells it NOT to save.

And then reopen our .vimrc and add the following keybinds:
Code:
map <leader>o :tabp<CR>
map <leader>p :tabn<CR>
map <leader>O :-tabm<CR>
map <leader>P :+tabm<CR>
map <leader>i :tabnew
NOTE: The :tabnew keybind above, has a trailing space at the end of the line
As always, save and reload your .vimrc to pick up the new shortcuts.

Now we have some more new leader keys combos to help us with tab management:
For switching tabs, we have:
<leader>o and <leader>p for switching to the previous/next tab, respectively.

For re-organising/re-ordering tabs, we have:
<leader>O to shift the current tab one place to the left.
<leader>P to shift the current tab one place to the right.

And finally, we have:
<leader>i for opening a new tab.
With this shortcut, you can either press enter to open a new tab, with an unnamed buffer, or type the path to the file you want to create/open in the new tab.
When typing paths/file-names in vim, you can also use the tab-key for auto-completion to speed things up a little.

With tabs, you aren't locked into whatever file you originally opened in the tab either. You could switch to any other open buffer.
So for example, with multiple files loaded into tabs - you could use any of your new buffer management shortcuts to switch to any other buffer whilst in any tab. So you could set all four tabs up to view the same file.

And that's it in terms of shortcuts, for this particular topic at least.
Some of the keybinds used in this series might not be to everybody's tastes but - these are all keybinds that are in my .vimrc and that I use every day. I find they save me from having to remember a lot of vim commands. And they also save me from having to make a few extra keystrokes.

So what have we got out of the series so far?
  • A quick and easy alternative to <escape>.
  • Remapping the leader key.
  • A way to reload our updated .vimrc, to allow us to activate our latest .vimrc changes without having to exit and restart vim.
  • A quick way to find the cursor, on even the most cluttered wall of text.
  • A bunch of custom keyboard shortcuts to help us manage our buffers and tabs.

Future vim topics I want to cover (in no particular order) are:

  • A complete guide to copy/paste and vim registers. Including how to use the system clipboard and to safely/securely delete sensitive text.
  • Recording and playing back macros using vim registers.
  • The dot operator . - which is an absolute godsend!
  • Search and replace.
  • Jump-lists, book-marks.
  • Window-splitting.
  • Using CTags out of the box in vim, to jump to symbol definitions in code.
  • Auto-completion and spell checking
  • diffing files with vim

Finally before I go - here's a link to a really good youtube video which recommends some .vimrc modifications, to make the most out of Vim without using any plugins.
How to do 90% of what plugins do (With just Vim)
It's a great video, with some awesome tips. Check it out!
 

Staff online

Members online


Top