تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating Keymaps Module0:00

Let's look at how we can map keys to make ourselves even faster and also fix a few little annoyances. Let's jump back into our terminal in our .files repo and we'll open up NeoVim. Let's now jump into our main NeoVim config entry point at nvim init.loa. Previously, we required an options module that we created, so now let's also require a keymaps module. I'm going to copy this with yy and then paste it with p. And then I'm going to use fo to come over to the first o on the line and then cw to change this to keymaps. We can now go ahead and create this module with :e nvim loa user keymaps.loa. Now, I've already gone ahead and created this file with a lot of the keymaps I want to talk about. The first item here is not actually a keymap, but it's changing what our leader key is. So the leader key is a key that we can use to prefix our custom keymaps, almost like a namespace,

Wrapped-Line Movement Mapping2:06

The next argument is the key that we're trying to map, so in this case I'm mapping the K and J keys, and you'll remember those go up and down. And then the next argument here is what we're mapping it to. Now this one's a bit of a special case, because if we look over at the fourth parameter here, I'm saying that this keymap is using an expression. And we can see that in this key mapping here we've got a little ternary going on. So to explain what this keymap does, I'm going to copy in some text. And then we'll come back to the start, and I'm just going to turn on line wrapping. So you'll notice that I'm on line 9 at the moment, but if I press the J key, I come straight down to line 10,

Insert Mode Quick Append9:08

And that's just the way I prefer things to work. All right, this one here is really cool. So let's uncomment this one. This one's a key binding for insert mode. So if I was to write something like echo test, and let's say my cursor is somewhere inside here and I'm in insert mode. Now let's say I want to put a ; on the end of this line. By default, I've got to press escape to come into normal mode. I could then use Shift A to append on the end of the line and then put my ; in.

Clear Search Highlight Mapping10:30

So if you remember when we searched for some text, so say I search for the word keymap and we end up with all these search mappings, that's great. We can see them all. We can jump between them. But when we've kind of finished with that search, we still have all this text highlighted that might be a little bit annoying. If you remember to clear this text, we'll type :nohls for no highlight search. And because this is something I do a lot, I've actually bound this to leader k. So let me save this file and resource it.

Open Current File Externally11:19

Basically, what this will do is it'll open the current file we're editing in the default operating system program for that file type. So if we come into command mode and we put an exclamation point in front of something, that basically is telling NeoVim to run this command via our shell instead. So this will run xdg-open via our shell and then the percentage sign, which is evaluated to the path to the current file. And then we're telling it to do a carriage return, which is the enter key, and we do that twice. So to demonstrate this, let's edit a file called test.html and we'll just say, hello laracasts.

So to demonstrate this, let's edit a file called test.html and we'll just say, hello laracasts. Let's save this file and let's just type and see what happens. So if I go :!xdg-open % and push enter once, then you'll see it's opened up my browser with that test.html file. This can be handy depending on the file types you're editing. You'll notice, though, that this has left us kind of in this little terminal thing here. It's run the command, but it's kind of showing us the output of that command in case we wanted to see it.

Move Lines with Alt13:21

So I'm just going to save this and resource this file. So these mappings are all brand new mappings. There's nothing really to demonstrate for the current behavior. So let's say I want to move this line here up a position and swap it with this line here. By default, I would do dd and then I'd probably move up a line, maybe up two lines and then push P to kind of paste that in there. So let's undo those ones. What this key mapping does is, and these two are for insert mode. In insert mode, I can come into insert mode and I can push Alt J or Alt K.

The "leader" keyConfiguring KeymapsAdd New Functionality

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟