1. Home
  2. Training Library
  3. Cloud Computing Fundamentals
  4. Courses
  5. Vim Graphical User Interfaces: Gvim and MacVim

Vim Graphical User Interfaces: Gvim and MacVim

Contents

keyboard_tab
Vim Graphical User Interfaces

The course is part of this learning path

Vim Masterclass
11
6
3
Vim Graphical User Interfaces: Gvim and MacVim
Overview
Difficulty
Intermediate
Duration
15m
Students
35
Description

The other courses in the Vim Masterclass learning path cover topics related to the command line or textual user interface version of Vim. There are, however, a couple of good reasons to use a graphical version of Vim and we will explore them in this course.

Intended Audience

This course is ideal for anyone who wants to edit text files in Vim using a graphical interface.

Prerequisites

If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.

Resources

If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.

Transcript

All the other lessons in this course have covered topics related to the command line or textual user interface version of Vim. The good news is that all of that knowledge you already have about Vim is transferable to the graphical user interface or GUI version of Vim. So anything you already know how to do in Vim will also work in the GUI version too. With that said, there are a couple of good reasons to use a graphical version of Vim. For example, if you're a Linux admin and you work with Vim all day on all of your servers, you might want to leverage that knowledge even when you're working in a graphical desktop environment. That graphical environment might be your Linux desktop, a Mac, or even a Window system. This way you don't have to use one editor in one place and another editor in another place. Another reason you might want to use a graphical version of Vim is that you really don't use the command line as part of your normal workflow. For example, I don't know many people who leave a command prompt open all the time on their window system. It's just a heavy, graphical, focused environment. It makes sense to run Vim as a standalone graphical application as opposed to running it in a terminal window. Yet another reason to use the GUI version of Vim is to take advantage of GUI only features. For example, if you want to access your systems clipboard or you want to use the File Explorer to open files, or you want to use the scroll wheel on your mouse to skim through a document. These options aren't available to you in the command line only version of Vim. Start the graphical version of Vim like you would any other graphical application on your OS. For example, if you're on a window system you can use the Start menu to navigate to Vim. If there are multiple options, select GVim which is the graphical version of Vim. You can also use the Windows Search feature and look for GVim that way. If you're using a Mac, you'll find MacVim in your applications folder. Of course you can also search for it using Spotlight. On a Linux or Unix system, you have a couple of options. First, you can the graphical version of Vim from the command line by typing GVim. You can also use the menuing system for your graphical environment. Just because you have the textual version of Vim installed, doesn't mean you have the GUI version installed as well. So if you don't find MacVim installed on your Mac system or GVim reports command not found on a Linux system, then you'll need to install the graphical version. You'll find the official versions of Vim at vim.org. So go there and install it if need be. Of course Linux users can use the package manager for their OS and install it from there if you'd like. If you do, you'll be looking for something like Vim-X11, Vim-gnome something similar. One of the most obvious differences between the graphical version of Vim and the textual version of Vim is that the graphical version of Vim includes menus. These menus will be very similar to most other graphical application menus. You see the usual file menu, edit menu and so on. In MacVim, you can take advantage of some Mac conventions like Command + O to Open a file, Command + S to Save the file and so on. Keep in mind that you can still use all the commands you've learned. So how would you save a file normally? That's right. You would use :W Enter. If we just hover over the menu option here, you'll see a little box appear that shows what command will be executed when you click on this menu option. You can see that it's :W. So when I click on save, it's as if I had type :W and pressed Enter. That's pretty much how the menu system works. When you choose a menu option, one or more of Vim commands are executed, they just type the commands for you. If those Vim commands are easily displayed they'll be shown in the menu as in our Save example of :W. For complex operations that require Vim functions for example, you won't see the commands in the menu. However, you can actually look at what each menu item does by checking out the menu.vim file. Anyway, before we get too far off the path here let's look at some more menu examples. If you wanted to undo something you can use the Mac convention Command + Z, or click on this menu item. What really happens is the U command is executed. Again, it's the same thing as you typing U for Undo, which you already know how to do. The Redo menu option is an alias more or less for Control + R. For repeat, there really is no Mac equivalent for this and thus no Mac keyboard shortcut. This menu option simply types the dot command for you. This menu item is pretty silly if you already know how to use the dot command. Typing a period is way more efficient than moving your hand to the mouse navigating to this menu option, and then clicking on it. This type of thing is one of the reasons I teach the textual version of Vim first. Can you imagine clicking on that option 100 times versus typing 100? Even settings are nothing more than Vim commands you already learned in this course. For example, let's go down to the Global settings here, and then let's look at the Toggle Pattern Highlight. It's the same as typing :sethls! If you click on it, it shows the option in the bottom of your screen, showing that it's turned on. Let me click it again. Let's go to the Edit menu, Global Settings, Toggle Pattern Highlight, and now it displays nohlsearch at the bottom of the screen because it was toggled off. Let's go back and click on Edit, Global Settings, Context lines, and five. What this is does is execute the command :set so equals 5. So as you might remember, this is the scroll off option. It's the minimal number of screen lines to keep above and below the cursor. So if I were to go to line 8 with 8gg, and then type Z and Enter, the text is moved to the top of the screen with five lines of context before. We've gone over the scroll off option before, so that's not what we're focusing on here. What's important to understand is that most of these menu items just execute Vim commands in the background for you. Before we move on, I wanna show you what this looks like on a Linux system. The GVim versions for Linux and Windows look very similar. In this screenshot, you can see the Vim commands on the right hand side of the menu option. You don't have to hover over the menu item to see this information like you do on a Mac. You might notice another difference which is a toolbar. Let's turn the toolbar on for MacVim. We'll go to Edit, Global settings, Toggle toolbar. This toolbar has pretty much what you would expect a toolbar to have on it. You can open a file, save a file, perform the undo and redo operations and so on. If you look to the right hand side of the screen you'll see a scroll bar. I'm just gonna click on it and drag it up and down here. Also, you can use the mouse scroll wheel to move about the file as well. So I'll just scroll down and scroll up, and down and up, et cetera. There's nothing groundbreaking here, but again it's just something you can only do with a graphical version of Vim. With a graphical version of Vim, you have access to the systems clipboard. You already know about Vim's clipboard like system registers. You also learned about some special registers along the way. Well, there are two more special Vim registers that are used in graphical mode. They are the plus register and the asterisk register. The plus register is the clipboard register, it contains the contents of the clipboard. For example, if you were to highlight some texts in a Mac application and press Command + C, then that text would be placed into the system clipboard and be available to Vim in the plus register. I'm just going to copy this bit of text here. I'll just highlight it and hit Command + C on my Mac, and then I'll go back to MacVim. Let's go to the bottom of the file here with g, and I'm just gonna move over one character here with l and I'm gonna use the P put command. So I'm gonna use "+P. Now let's look at the menu here, edit and paste. The shortcuts in the edit menu here is actually "+gP, which is the plus register followed by the gP command. The only difference between the P command and the gP command is that the gP command places the cursor just after the pasted text, if there's room on the line. The P command just places the cursor on the last character of the pasted text. See how cursor is on the d, which is the last character of the word copied. Let's undo this, put operation with U. Now we can type "+gP. Now the cursor is just to the right of the pasted text. Let me undo this again. And let's use the menu option here, edit and paste. So that's the plus register. I also mentioned the star or asterisk register. Well on Mac and Windows systems, the asterisk register is the same thing as the plus register. As a matter of fact, if we look at the registers you'll only see the plus or asterisk register, but not both. Let's look at the registers with :reg and press Enter. Here you see the asterisk register, but not a plus register. Let's ask the register command for the contents of the plus register with :reg+ and hit Enter. Notice that the asterisk register is displayed, so let's undo our last paste operation with U. Now let's use "*P, you'll see that the text from the clipboard is pasted. So again, if you're on a Mac or Windows system the asterisk and plus registers are interchangeable. If you're working on a Linux or Unix system and using X windows, then the plus register contains text that uses the traditional clipboard, and the asterisk register contains text that is highlighted or selected with the mouse. If you've used X Windows before you'll know that you can highlight something with your mouse and then paste it with a middle click, that is the asterisk register in Vim. Also in X Windows, you can copy something with Control + C. This is the plus register in Vim. Let me show you, I'm gonna copy the text copied by highlighting it and pressing Control + C. Now I'm going to switch over to Vim and type reg and press Enter. So you see that text that we copied with Control + C is in the plus register. Now, let me just select the word highlighted here with my mouse. Let me switch back over, and let me highlight that with my mouse back over to Vim, and let's type reg again. Now, when we look at the registers you see the highlighted text in the asterisk register while the copied text remains in the plus register. By the way this clipboard functionality isn't just in one direction, you can also place content into the system clipboard just like you can with any other register. For example, let's yank the first line of text into the system clipboard here with gg, and now we can use "+ to specify the clipboard register and yy to yank the text. Now, if I go to another application on my Mac I'll switch over here, and press Command + V, then that text is pasted into the application. It's really great that Vim has these special registers that we can use to access the systems clipboard. But if you're looking to make this a more seamless experience, you can set Vim's clipboard option to unnamed. Let's do that now. Here with :set, clipboard equals unnamed and press Enter. What this does is to tell Vim to use the clipboard register for all yank delete change and put operations, which would normally go to the unnamed register. Now, by default, you don't have to specify the asterisk register. Let's yank a word with yw, notice we didn't supply a register. Now let's switch to a different application and type Command + V to paste that text we just yanked. Command + V, and that word we yanked is pasted. Let's do it the other way. Let's copy something from here with Command + C, go back to Vim and paste it with P. Now Vim acts more like other applications by using the systems clipboard by default instead of its own clipboard system. By the way, if you want to do this in Linux instead of using unnamed use unnamed plus. So here I'll type set clipboard equals unnamedplus and hit Enter. And so now let me go to another application and copy something here, Control + C, and then Shift + P, and we're back there. Let's do this. I'll yank this line with yy, go back to my other application and hit Ctrl + V and it's pasted from there. So again, if you're using Linux or a Unix system then unnamed plus is what you need to set the clipboard to get this type of functionality. If you like this option, it makes sense to save it, so you don't have to set it every time you start Vim. This brings me to the last topic I wanna cover in this lesson, and that is the gvimrc file. You already know about the Vimrc file. Well, gvimrc acts in the same way but it's only used by the graphical version of Vim. So if you have some specific gvim only settings this is where you should put them. Like the vimrc file, the actual name of the file is .gvimrc for Mac, Linux and Unix users, and _gvimrc for Windows users. In either case your gvimrc file should go in your home directory. Also be aware that the gvimrc file is read and executed after the normal start-up. This means your vimrc file is read before your gvimrc file. You can use the version command to see the location and name of the gvimrc file that Vim is looking for. You'll also notice the name and location of the menu file that I briefly mentioned at the beginning of this lesson. If you wanna change the font, use the GUI font option or gfn for short. To use our operating systems font selector type setgfn equals asterisks and press Enter. You could have also used a menu option to start the font selector as well. Now you can just pick your favorite font. I'm gonna use Courier New here, and size 24, I'll leave it at this. Now we can view our fonts selection here with setgfn? And press enter. If you wanna use this font each time you start Gvim, place it in your gvimrc file. By the way, if your font name has spaces in it you'll have to escape those spaces with a backslash. For example, if you want to use Courier New h14 then you would have to do this, setgfn equals Courier, and then a backslash before the space to escape it. New h14 and press Enter. Let me reset my font size here to something more legible. Typically you can surround values with quotes but it doesn't work with a GUI font option. Again, just escape any spaces in your font name. For more information on GUI specifics including settings, you can read up on the documentation by typing :hgui and pressing Enter.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
3453
Courses
61
Learning Paths
8

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.