How I Setup Neovim On My Mac To Make It Amazing - Complete Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys today we're gonna be taking neovim from absolute zero and turn it into something pretty amazing we're gonna be creating a pretty up-to-date and modern setup with a configuration written in Lua and making use of a lot of popular plugins to get a nice looking editor with proper syntax highlighting through neovins tree sitter intelligent code completions with the built-in LSP a file explorer git integration and an awesome fuzzy finder called telescope to easily find files and text throughout your projects there are some other easier options like using Vim as a plugin in a modern IDE or using some pretty cool pre-built neovim configurations like astrovim or lunar Vim which will get you up and running pretty quickly personally I find using Vim to be a lot of fun and I enjoy the process of making the editor my own I'm also by no means any of an expert I still have a lot to learn and there are some great resources out there like Chris hat machines YouTube series series called neovim from scratch which I learned a lot from with that said let's Dive Right In if you want to follow along with me I'll have a link in the description to a GitHub repo with everything I'm going to be showing you and make sure you're using a true color terminal like item 2 for the color scheme to work properly and also a nerd font I'm personally using meslow nerd font for specifics on my terminal setup I would check out the previous video in this series so the first thing we have to do is install neovim I'm going to be using Homebrew to do this you need to make sure that you're using the current stable release version which at the time of recording is 0.8 let's go ahead and install it by running Brew install neovan now that's done to check if neovim installed correctly we can do nbim dash dash version and as you can see here we have 0.8 with a build type of release which is exactly what we want I want to first take a step back and give you a general overview of what the file structure will look like at the end of this project as you can see here everything will be within the dot config folder located in our home directory inside of that we'll have another folder called nvim and inside of that a file called init.lua this file will be our most important one as neovim will look at it every time it starts to load our configuration its job will primarily be just to import all of our other Lua files located in the Lua folder inside of this folder we'll have another folder with our name or whatever else you want to call it and inside of that a folder called core this will have a luo file for color scheme configuration another one for our basic options and settings and another one for our custom keybinds or keyboard shortcuts we'll also have a plugins folder which will have luo files for configuring and setting up our plugins and finally we're going to have a plug-ins Dash setup.lua file which will be responsible for setting up our plugin manager which we're going to be using to install our plugins in this case we're going to be using a tool called Packer it will also list all of the plugins we want to install let's start setting this up make sure you're located in your home folder indicated by the tilde first you want to make sure that you have a DOT config folder here you can create it if you don't by doing make directory.config I already have it though so I'm just going to move into it then we want to create the nvim directory so let's do make directory and then now let's move into that in here let's create the init.lua file by running touch init.lua and the Lua folder by running main directory Lua now let's move into the Lua folder I'm going to clear the terminal window now let's create the folder with our name by running make directory and then our name you can use your name here or whatever else you want to use let's move into that and then let's create our core and plugins directories let's do make directory core and make directory plugins let's also create the plugins Dash setup.lua file by running touch plugins setup Dot now that we've created all of that we can start working with our core configuration so let's move into the core folder and here we want to create three files so touch color scheme dot Lua for our color scheme touch keymaps.lua for keybinds and our keyboard shortcuts and touch options.lua for our basic settings and options let's actually move a couple levels up back to the nvim folder because we want to import these in the init.lua file so let's go CD level up to joseon another level up to Lua and another level up to the nbim folder clear this if we do LS here you can see we have the init.lua file here let's open it up with neovim so let's do end them init.lua in here we need to import the three little files within our core folder which include the options file the key maps file and the color scheme file to do this we can use a require statement and then the path to the file ignoring the Lua folder so we can use halcyon.core.options and then do the same for our keymaps file and then do the same for our color scheme file awesome so let's save those changes and quit now let's go back to the core folder so that we can edit the options file so let's do CD into the Lua folder then the folder with our name and then the color folder let's now open the options.lua file with neovan in here we have access to the Vim Global variable and we're going to be using vim.op to set our options I'm actually going to save it into a local variable called opt which we're going to be using for conciseness the first thing I want to do here is configure my line numbers by the way you can use double dashes for comments I want to set relative number to true and I also want to set number to true to show you what this does I'm going to save these changes and quit neovim and then I'm going to open the file again as you can see here I get absolute line numbers wherever my cursor is and anywhere else I get relative line numbers so if I want to move to op.relative number I know it's three lines below me so I can do three J and then whenever I move my cursor I also know what absolute line number I'm in the next thing I want to configure is tabbing and indentation I'm going to set up that tab stop to two I'm going to do shift width to 2 I'm going to do expand tab to true and auto indent it's true what you need to know here is that this sets us up pretty nicely to use two spaces for tabbing if you want to use four spaces or something else you can change these twos to fours and that'll set it up for you next up we're going to configure line wrapping here we're going to set wrap to false this will prevent text from wrapping which is ideal when you're writing code let's now configure our search settings here we're going to set ignore case true and smart case to true to show you what this does let's save and quit and let's open the file again I'm going to add here a lowercase hello and an uppercase hello and now if I use a forward slash to search and type out in lowercase it's going to assume that I want a case insensitive search and it's going to match both of these but if I type out hello with uppercase it's going to assume that I want a case sensitive search and match the Hello with the uppercase only next we're going to set up our cursor line by doing cursor line equals to true to show you this let's save and quit and then open up the file again as you can see here now we have a line wherever our cursor is now let's work a little bit with the appearance of neovim we're gonna set term GUI colors to true so that our color scheme can work properly remember that you need to be using a true color terminal like item 2 for this to work we're also going to set the background too dark this will make light and dark color schemes default to dark and we're also going to set sign column equal to yes you don't have to worry about this one too much to show you what happens once we do this let's save and quit and open up again as you can see here things are starting to look pretty different now we're going to configure our backspace key we're going to set this backspace equal to indent EOL start don't worry about this one too much it just makes your backspace key work properly now we're going to configure our clipboard and this is pretty important we're going to do off dot clipboard append unnamed plus basically what this will do is force neovim to use the system clipboard when you paste anything with the yank operator or delete or anything else this really comes in handy when you want to copy something from within neovim to somewhere else on your computer now I'm going to configure some settings concerning split windows when I split vertically I want to split to the right so I'm going to do split right equals to true and when I split horizontally I want to I want to split below so let's do a split below is equal to true final thing I'm going to set is this thing right here I'm going to show you what it does in a second essentially it's going to change what the dash means within neogen let's save these changes and quit and open this up again now if I do hello Dash Hello go to the beginning and then do DW it now considers the Dash as a part of the word with that we're pretty much done with our basic settings and options let's save and quit let's clear this the next thing I want to do is configure my preferred color scheme to make things look a bit nicer but this is a plug-in so we're going to have to configure Packer to be able to install it if you remember from earlier we're going to configure packer in the plugins Dash setup.lua file located in the folder with our name so let's move up to that folder as you can see and here we have the plugin stash setup.lua file now let's open it up with neovim plugins Dash setup Dot we're going to be using a tool called Packer to install our plugins I would check out the documentation to learn more about setting it up from the documentation I'm going to copy some lines of code they have in there and I'm going to paste them in here I wouldn't worry about it too much just copy this code from my repo essentially what this does is that if Packer is not installed it's going to go ahead and install it when we open up neovim and what this does is that whenever we save this specific file Packer is going to install any missing plugins update any old ones and remove any of the ones we're not using anymore the next thing we want to do here is import Packer into this file we're going to use a protected call to do this it's a pattern we're going to see often let's go ahead and import it we're going to do local status Packer is equal to protected call require packer next we're going to check if not status then just return essentially this will try to import Packer and then if it fails we'll just return and not do anything else now we want to configure our plugins to do this we're going to do return Packer dot startup and then function use end according to the documentation we also want to add this in here don't worry too much about it just copy it from my repo or the documentation and add it in there now we can start listing all of the plugins we want to install the first one is actually Packer itself so we're going to do this right here to install Packer as Packer can manage itself and then as I mentioned we want to install my preferred color scheme to do this I'm going to do use plus 71 slash Bim night fly GUI colors this is literally just a git repository most of the time you can look up the repository and it'll tell tell you how to install it with a plugin manager or you can just look at the name of the repo and who it belongs to and that should be all you need for a packer to install it properly all right let's save these changes and quit now remember we want to import this file from init.lua that's really important so let's go back up two levels to the nvim folder let's open init.lua and then let's require it we're going to do require closing dot plugins Dash setup which is the name of the file let's save and quit go back to the folder with our name so CD lewisian and now let's open the plugins dots Dash setup file again with neovan as you can see here Packer automatically recognized the color scheme we wanted to install and it went ahead and installed it let's press Q to close this out you can see now that whenever I save this file Packer is going to check if it needs to install any new plugins update them or remove them let's close that again by pressing Q now let's exit now that the color scheme is installed we can properly configure it let's move into the core folder and let's open the color scheme.lua file now we want to set up the color scheme we can do this by doing bim.cmd color scheme Knight fly this actually allows us to run vimscript within Lua but we want to do this in a protected way in case the color scheme is not installed so instead of doing this we're going to do local status underscore is equal to P call CMD color scheme Knight fly so we're using a protected call here to make sure that we handle the case where the color scheme is not installed so if not status then print color scheme not found and we'll just return and let's delete that Top Line save and quit and if we open up that file again with neovim you're going to see that the color scheme has been set up properly and things are looking a lot nicer all right that's quit neovim the next thing I want to show you is how I configure my key maps we're going to open up keymaps.lua and then the first thing we want to configure here is our leader key I'm going to set this to space for those of you that don't know the leader key allows us to set up a bunch of different custom keyboard shortcuts or keybinds that don't clash with the Vim defaults essentially all of these key maps we Define will be prefixed with the leader key in this case the space key which is really easy to access and it can be really powerful the default leader key in Vim is the backslash but I think space is a lot easier to access now to configure our key maps we want to use bim.keymap and again I'm going to save this in a local variable called keymap for conciseness now we're going to set up first our general key maps to set a key map we're going to do key map dot set then we're going to specify which mode this key map will apply to in this case I want to do insert mode then what the keys are going to be so I'm going to do JK and then what the action will be so this is going to be Escape essentially what this means is that in insert mode JK will be the same as hitting the Escape key which takes you back to normal mode to show you this I'm going to save and quit and then open the file again and as you can see here I'm going to do I to go into insert mode and JK to go out next I'm going to add a normal mode key map this means that in normal mode when I press leader and H it's going to clear search highlights let's save and quit open up again let's search for key math and then if I do leader NH it's gonna clear so I just pressed space NH next we're going to set this guy right here in normal mode when I press the X key which deletes A Single Character it's going to delete the character but it's not going to copy it into a register which is what it normally does next I'm going to add these two these mean that in normal mode I'm going to use leader plus and leader minus to increment and decrement numbers let's save and quit and open this file again I'm going to add a 2 here and then I'm going to do leader minus to increment and liter plus to increment next I'm going to add these key maps these are some of the key maps I really enjoy using SV is going to split window vertically sh will split window horizontally SE will make the split windows equal width and height and SX will close a split window let me show you this in action let's open it up again now if I do leader SV I'm going to split vertically leader sh I'm going to split horizontally leader SX I'm going to close leader SX I'm going to close note that if you have split windows in neovim the mouse is available so you can change the width with your mouse which I think is the best approach honestly instead of using key maps and then I can do leader SE to make the equal width again let's close with leader SX and finally I'm going to add some key maps for managing tabs leader to will open a new tab leader TX will close the current tab leader T and we'll go to the next Tab and leader TP will go to the previous tab let's save and quit so I can show you this then open it up again let's do leader to leader to so I'm opening new tabs leader TP to go back leader TN to go to the next one liter TX to close and leader TX to close cool let's save and quit because we were just setting up split windows I want to take this opportunity to add another plugin so let's go back up to the folder with our name and open up the plugins Dash setup.lua file let's go down here to our list of plugins all right so I want to add this plugin right here this is Chris to me slash Vim tmux Navigator essentially what it'll allow me to do is use control h j k and l to navigate between splits and also navigate to tmux the next video I'm going to be making is about combining Vim with tmux so this plugin is in preparation for that it's going to help us a lot save this and then you're gonna see it's in gonna install that plug-in let's press q and then quit neovim open up this file again and then let's create some splits so let's do leader SV leader sh and as you can see here now I can use Ctrl K and J to move between splits above and below each other and Ctrl H and Ctrl L to move between splits that are vertically separated let's close these I want to add another plugin that will help us with window management it's called Vim maximizer essentially what it allows us to do is maximize splits and restore them back to their original size let's save this so that Packer installs it as you can see here it installed it let's press q and right quit now we're going to modify our key maps to make use of this plugin let's move into the core folder and then let's open the keymaps.lua file go to the bottom we're going to add a new section or plug-in key maps and then we're going to add this key map what it does is that in normal mode we can use leader SM to maximize the split window and also restored back to its original size let's save this and quit and open up the file again now if I use leader SV to split the window in half and then I do leader SM I'm going to maximize the window and then leader SM again and I'm going to restore it to the original size let's quit let's go up a level again and let's reopen the plugins setup.lua file now I'm going to add two more plugins that I find essential to my Vim workflow one of them is called Vim surround and the other is called replace with register let's save this file that's how they get installed Q here and then let's quit and reopen the file let me show you what you can do now with something like Vim surround let's say I want to surround ensure underscore Packer with double quotes I can do y s a motion and then the character I wanna surround ensure Packer with so I can do Ys the motion would be W for word and then the character I want to surround it with is double quote let's say I want to delete these surrounding quotes I can do DS and then the character that is surrounding so double quote let's undo that let's say I want to change these double quotes to single quotes I can do Cs and then the character I want to change so double quotes and then the character I want to change it to so single quote if that seems confusing you can check out the documentation to learn more about it now with the replace with register plugin I can use gr to copy text and then replace something else with what I copied let's say I copy text one with y w let's then move to text to and then I'm going to do g r w to replace text 2 with what I copied this applies to a lot of different motions and it can be really powerful along the same line we're going to add this next plugin called comment.nvim which we're going to use to make common thing things out a lot easier let's save this file so that Packer installs it press Q then we're going to quit this is the first plugin we actually need to configure to enable it to do this we're going to move over to the plugins folder I'm going to clear this terminal window and in here we're going to create a file called comment.lua after we create it I'm going to open it up with neovim we now need to import the comment plugin into this file we're going to use a protected call to do this to make sure we're not using it if it's not installed to do this we're going to do local setup comment is equal to keycall require comment and then if not set up then return and end essentially this will import the comment plugin into this variable and it will return if it isn't installed if it is installed then we need to call comment back setup let's write and quit like our other Lua files we actually needed to import this file in init.lua so let's move up to the hosian folder and up to Lua and then up to neovim and then we're going to open init.ua with neovim now we're going to add a require statement here this is going to be joseon.plugins dot comment which is the path to the file that's right and quit let's go back to the folder with our name so CD lewisian and let's open the plugins setup.lua file again I'm going to show you now what we can do with this common plugin essentially we can use GC plus a motion to toggle comments on different things for example let's say I want to comment this line I'm currently in I can do GCC and to undo that I can do GCC again let's then say I want a comment from line one to line nine I can do gc9j and if I want to do that I can do GC 9j with that done we're ready to move on to some really cool plugins that will really take our neovim setup to the next level I'm gonna go down here to our plugins and near the top I'm going to add this plugin right here basically this plugin gives us a lot of Lua functions that many other plugins make use of so let's save a file so that we can install that and then press Q the next thing we're going to add is a file explorer so we're going to add this plugin right here it's called nvim tree we're going to save a file so that we can install it now that that's done we can press q and now this is another plugin that we need to configure to enable it so let's quit let's clear this terminal window let's move to the plugins folder and create a new file called endim-3 dot Lua let's open it up with neovim let's import the plugin with a protected call now the documentation recommends that we add these two lines and then to finish this up we're going to do NVM tree dot setup then we're going to write and quit and again we need to import this file in init.lua so we're going to move up to hosian up to Lua and then up to nvim and then we're going to open the init.ua file down here we're going to require for sand.plugins.nbin-3 which is the path to the file we just created we're going to write and quit and now I'm going to open the file again now that the plugin is available to us we can do colon nvim tree toggle and we'll have a file explorer to the left which I really enjoy using for managing my projects navigating adding files deleting files changing names all that sort of thing we can navigate the tree with our normal motions to open a folder I can do enter and then I can use J and K to move up and down I actually want to make some configurations to NVM tree to make it look and work a little bit better so I'm going to go down to joseon press enter and then down to plugins and then I'm going to press enter on nbim tree to open that file I'm going to run and Vim tree toggled again to close it soon we're going to be adding a key map to open and close the file explorer more quickly but before we do that I want to add some configuration changes here we're going to go to this setup function and we're going to add a table in Lua this is pretty similar to an object in JavaScript it has keys and values there are a lot of options you can configure here I would check out the endim tree documentation to learn more about that let's open up nvm3 by doing MVM tree toggle one of the things I don't like are these arrows I think they don't look very good and as you can see here the Arrow when the folder is closed is bigger than the Arrow when it is open I want to change that let's close the tree and in here I'm going to add these lines I got how to do this from the documentation essentially within the renderer we have icons glyphs folder and then in there we can specify the arrows that we're going to be using when the folder is closed and when the folder is open as I've mentioned I'm using mess load nerd font and that allows me to use these icons and see them on my terminal make sure you're using a nerd font for this to work on your machine let's save and quit and then let's open open up neovim again let's open the Explorer and as you can see here the arrows now have changed let's go back to the plugins directory open up mvim tree again close the Explorer next thing I'm gonna do is add these lines this makes it so that working with the file explorer and window splits works properly and finally I want to change the color of the arrows themselves to do that I'm going to go up here and I'm going to add this line in Vim highlight groups Define how things look and the colors that things will have this modifies the Highlight group that the arrows use to get their color if you want to use another color for the arrows you can change this value remember that the repository with all of my code will be in GitHub and you can reference it to copy things and add them to your own configuration let's save this and quit let's open neovim again let's open the file explorer as you can see here now they're blue instead of the dark gray that they had earlier finally as I mentioned we want to create a key map to open and close the Explorer quickly so let's go over to Lua ocean core and then we're gonna go here to key maps close the Explorer and then we're going to go to the bottom to our plugin key map section down here we're going to add nvim tree and we're gonna do key map dot set normal mode I'm gonna do leader e and that's going to run NVM tree toggle and CR which means enter let's save that let's quit let's open up neobim again and then I can do leader e and leader e awesome there's one more thing I want to add to the file explorer and that's vs code like icons for my files but to do that we have to add another plugin so let's do leader e to open the file explorer let's go to Lua bosian and then we're gonna go to plugins Dash setup let's close the Explorer with the leader e let's go to the bottom of the file with shift G then we're going to add here a plugin for our icons again we're going to add it right here it's called nvim Dash web Dash Dev icons I'm going to save this file it's going to install the plugin and press q and then I'm going to quit meowvin to reflect our changes and then open new them again and let's do leader e and as you can see here the Lua files now have this fancy icon for them and there will be a lot more for JavaScript files and a bunch of other different things the next thing I want to do is configure a nice looking status line so let's go to Lua Halcyon plugins Dash setup we're going to install another plugin go to the bottom of the file with shift G and then down here we're going to add the plugin for our status line This plugin is called luoline let's save this file it's gonna install it automatically press Q then let's press colon right quit let's open the oven again open the file explorer then we're going to go to Lua hosian plugins directory and we need to add another file here to configure and enable luoline to add a file with the Explorer we can use a then down here we have a prompt for us to write the name of the file we're going to do here luoline dot Lua and then press enter press enter again and the file has been created you can see it here in the file explorer let's press enter to open it let's close the Explorer with leader e and now we have to import the plugin again and now we can call luoline Dot setup to enable it that's right to the file remember that we need to import this file in init.lua so let's open the file explorer move up here to init.lua Let's close the file explorer then let's add require halcyon.plugins.lua line which is the path to the file that's right and then let's quit to reflect our changes and then let's open up neovim again and you can see now that down here we have a fancy status line pretty cool I want to make some changes to it to make it look a little bit nicer I'm going to open the file explorer go to the Lua directory joseon plugins and then Lula line and again we can add a table in here with our configuration changes the lure line plugin actually has a nightfly theme which is the same theme I'm using for neovim so we're going to use that we have to also require it it's found in luoline.themes.nightfly and then I'm going to change some of its colors this is a table with the new colors I'm going to use to change these colors I'm going to do luoline underscore nightfly dot normal so in normal mode we're gonna have a background of blue and then I'm gonna do a rule line fly dot insert so that in insert mode we're going to have a color of green then I'm going to do knightfly dot visual so that in visual mode we're going to have a violet color and then for some reason the nightfly theme doesn't have settings for command mode so we need to add all of this which basically sets command mode so that the text is Bold and the color is yellow and the text is black now that we've set all of those properties down here in the table we have to do options is equal to another table with theme equal to luoline magnify let's save that and quit let's reopen any of them as you can see here now we have our new colors in the status line if I change to insert mode that's what we get if I change to visual mode that's the Violet and then if I do colon for command mode the yellow for command and with that we're finished with configuring the status line note that low line as well as nvim tree will both make use of the icons plugin we installed earlier the next plugin we're going to install is called telescope it's a fuzzy finder that makes it really easy to find files and text throughout your projects one of those plugins I honestly can't really live without so let's get into installing it let's open up the file explorer and move over to Lua Halcyon plugins setup close the file explorer go to the bottom with shift G we're going to add two lines here the second line will install telescope itself and the first one is a dependency that will help telescope perform better these two lines actually make use of a table to specify first the name of the repository and then we have here some custom options that we add so this runs make after installing the plugin and then this we're specifying that we want to use the specific branch of telescope so let's save this and install those two plugins press Q remember that if you need to copy and paste these you can check out my repo for reference now we have two configure telescope as well so let's open up the file explorer with leader e let's go up to plugins and let's press a to add another file and it's going to be called telescope dot Lua press enter and then enter again to open it and then leader e to close the Explorer all right now we want to import it we're actually going to do two Imports here first import is to import the telescope plugin itself and the second one is to import the telescope actions so we can specify some custom key maps for telescope so down here we have to run telescope.setup and inside we're going to add a table where we can Define our custom key maps or telescope these guys we're going to be using control K to move to the previous selection Ctrl J to move to the next selection and control Q to send items to a quick fix list I'm going to show you this in a second we also have to add this line telescope dot load extension FCF to make use of the other plugin we installed which is the dependency that will make telescope perform better let's write to this file and now before we start using telescope let's define some keyboard commands to activate it let's open up the file explorer go up to four go to our key maps file close the Explorer go to the bottom of the file with shift G and then we're going to add a section here for telescope we're going to add these key maps so this first one leader FF will allow us to use telescope to find files in our project the second one leader FS will allow us to find text throughout our project this third one leader FC will allow us to find the current string the cursor is on throughout our project this other one leader FB will show us our active buffers and leader FH will show us help tags and now before we finish I almost forgot we have to import the telescope.lua file from init.lua so let's open the file explorer go to init.lua close the Explorer and then we're going to add here require close the end dot plugins dot telescope we're going to write and put open up new of him again so now we can use telescope I can use leader FF to find files as you can see here we have a list of the files within our project and I can use Ctrl K to go up and Ctrl J to go down let's say I want to find the keymaps.lua file I can type here keymaps.lua and it gives me that result I also have a file preview of it on the right hand side I can press enter and it'll take me to that file on the other hand let's say I want to find a specific string within my project I can use leader FS then let's say I'm looking for keymap.set and we have a lot of those we see a preview of how it looks like on the right hand side and we can just pick one let's press enter it'll take us to that file and to that specific line where it is those are the primary two functions that I use with telescope it really helps me with navigating in big projects it really is a lifesaver and I really enjoy using it there are a lot of other things you can do with telescope I'm not going to be going into them in this video you can check out other videos about it on YouTube and also read the documentation alright so the next thing we're going to do is set up basic code Auto completion with Snippets and after we have that ready we're going to add a smart code completions with LSP so we're here in our DOT config nvim directory let's open up neovim let's open up the file explorer with leader e then we're going to go down to Lua portion let's open plugins setup.lua close the file explorer go to the bottom and then we're going to add a couple of plugins here for auto completion so first we're going to add these three plugins nvmcmp is going to be the plugin that we use for auto completion cmp-buffer is actually a source what this means is that it will allow nvmcmp to recommend text from the current buffer in contrast CMP Dash path is another source but this one is for file paths so that nmcmp can recommend to us different directories or files when we're writing out a path I'm also going to add some plugins for Snippets we're going to be using Lewis nip as a snippet engine CMP underscore Lewis nip is another source this will allow nbim CMP to show us Snippets in our Auto completion and friendly Snippets is just a collection of a lot of use useful Snippets for a lot of different languages let's save this file and Packer will install all of those plugins for us awesome now we actually have to configure ndim CMP to enable it let's open the file explorer let's go to plugins let's press a to add a new file here it's going to be called nvim Dash cmp.lua press enter and enter again to open it up close the file explorer so now that we're in this file we have to import two things the first is nvmcmp the plugin for auto completion and Lewis nip which is our plugin for Snippets so let's import those two with protected calls as we've done earlier as you can see we're first importing nvmcmp into the CNP variable if it's not installed then return and then we're importing Lewis nip into the Lewis net variable and if it's not installed then return now we want to load these Snippets from the friendly Snippets plugin so let's add this line This Will load them for us then according to the documentation we want to add this line you can check the documentation for that or my repo to copy this into your config and then we're going to run cmp.setup to configure and enable nvim CMP this is going to have a table inside and then we're going to configure our Snippets by adding this you can find this in the documentation or also you can copy it from my repository and then what we want to do is set up our keyboard mappings for auto completion to do that we're going to add these lines essentially Ctrl K and control J will allow us to navigate through the suggestions Ctrl e will close the suggestion window and we can use enter to choose a selection then we have to configure the sources for auto completion as you can see here we're going to be using Snippets from Lewis snip the buffer plugin for text within the current buffer and also the path plugin for file system paths with that the basic functionality for auto completion is pretty much ready let's save this file we also have to import it from init.lua so let's do leader e and then let's go to init.lua close the file explorer by doing leader e again and then we're going to go down here add require pausing.plugins.nvim CMP let's save this file and let's close neovan now let's open neovim again let's go to our Lua halseyan plugins nvmcmp and let's see now what happens when we type something let's type out something that is found in this buffer like Lewis snip let's do Lua and as you can see here we have recommended Lewis nip and Lewis nip underscore status we can navigate the results with Ctrl J or Ctrl K and then we can choose a result by doing enter this will also work with file system paths so if we do dot slash we'll get recommended the files that are within the current directory and we have a preview of what those files look like we can also scroll through the file by doing Ctrl F to scroll down and Ctrl D to scroll up this will also work with Snippets so let's say I want to do an if statement in Lua I can do if I have a snippet recommendation here I can do control J I get a preview of the snippet and if I want to use it I press enter and it fills it out for me I can also automatically change the condition by just starting the type so it falls for example awesome so that's working really well now that we have Auto completion ready we can move on to configuring LSP LSP is built into neovim and it stands for language server protocol it'll give us the ability to have really smart auto code completions and also the ability to perform code actions to fix problems in our code find things like where a class is defined or where it is referenced and also being able to rename things in a smart way to get this configured let's open up the file explorer by doing leader e and then let's go down to plugins Dash setup let's close the Explorer with letter e again let's go to the bottom of the file with shift G we're going to have to add several different plugins to get this working properly the first plugin will be in charge of managing and installing LSP servers we're going to add it like so it's called mason.nvim it's basically going to be our primary source of Truth for managing and installing all of our LSP servers LSP is built into neovim but the LSP servers themselves need to be installed and then the other important plugin that we need will be used for configuring the LSP servers we're going to add it like so and this plugin is called nvim LSP config we also need to add the following plugin which will help us bridge the gap between Mason and nvim LSP config it's called Mason LSP config you'll see how all of this will tie in together soon let's save this file which will install those new plugins let's press Q to close that window and then we want to configure Mason first let's open the file explorer go to the plugins directory and we're going to add a new directory here for everything that will concern LSP let's press the a key and then we're going to add here LSP and finish it with a forward slash to make sure that it's created as a folder let's open that folder press a again and we're going to type out mason.lua which will be the name of the file for configuring Mason let's open that file now what we want to do is import Mason and Mason LSP config into this file by adding these lines as you can see here we're using protected calls again to make sure that the plugins are installed and we use the name of the plugin to import it if these aren't installed or if there's an error nothing else will happen next we want to configure Mason first it's important that we write this first so we'll do Mason dot setup that's all we need to do and then we want to configure Mason LSP config we'll do dot setup and then in here we're going to pass a table and here we can add ensure installed I'm going to add here the list of LSP servers that I'm going to be using for my projects you can check out the documentation for Mason LSP config to see all of these servers that it supports alright and with that let's save this file we have to again import this in the init.lua file so let's open the filex floor let's go to init.lua close the file explorer and then we're going to add here require close hand dot plugins dot LSP dot Mason and let's save that let's quit neovim now let's open up neovim again and we're going to do colon Mason press enter and you're going to see here that it's installing the LSP servers once it's done they're going to appear here and installed it installed all of the LSP servers that we had specified in the mason.lua file now that the language servers have been installed we can actually configure them so let's close this window by pressing q and then let's open the file explorer by doing leader e let's open this Lua folder go into joseon then we're going to go to plugins Dash setup because we want to install some additional plugins to make all of this work properly let's close the Explorer and go to the bottom of the file by pressing shift G we want to add the following plugins cmp-nvim-lsp will allow us to configure the LSP servers so that they appear in Auto completion LSP Saga will add enhanced uis to our LSP experience it's really cool and I'm also using the typescript.nvim plugin to add further functionality to the typescript server this plugin will allow me to do stuff like renaming a file and updating all of its Imports and finally we're adding this LSP kind.nvim plugin this will be used to add vs code like icons to our Auto completion window let's save this file so that the plugins get installed now that they're installed let's press q and then let's open the file explorer with leader e then let's move over to the plugins directory the LSP directory and then we want to add a file called LSP config.lua we can do this by pressing the a key and then typing out the name of the file now let's press enter and enter again to open that file in here we're going to configure all of our LSP servers we need to add these three Imports to import LSP config to configure our servers TMP and Vim LSP so that we can configure the smart code Auto completion with LSP and the typescript plugin so that we can have the enhanced functionality for the typescript LSP server next we're going to be defining some key maps so we're going to say vim.keymap in a local variable and then we want to define a function called on attach which will take a client and a buffer we'll be using this function to enable a specific set of keybinds for whenever we're editing a file in a specific language that we do have an LSP server for for example if I have the typescript LSP server configured and I'm editing a typescript file then the LSP server is available and then I'm going to have access to these keybinds let's add these keybinds in here you can find this code in my repository first we have an options variable which will Define a specific set of options that we want these key maps to have so you can see here that we're assigning it to each of the key maps and then all of these will be normal mode key maps we're going to be making use of LSP Saga for a lot of this functionality which brings us really nice uis with LSP I think the best way to show you how these key maps work is by finishing the configuration then showing you with actual code also note that down here we have a special key map that is only available for the typescript server this this makes use of the typescript plugin so that we can rename a file and update all of its Imports alright so the next thing we're going to do is Define this variable right here called capabilities we're going to be using it to enable auto completion for each of our LSP servers we have to add it to each of the configurations we're going to Define next so to give you an example of how you configure an LSP server let's do the HTML server by doing LSP config then you do bracket with the name of the server so this one is called HTML then we do dot setup and then in here we pass a table with our configuration and here you want to pass capabilities like so this will enable auto completion for the HTML server and you also want to do on attach so that we can have the key values we defined available whenever we're editing an HTML file the configuration for my typescript server is a bit different because I'm using the plugin it looks something like this so it's typescript.setup and then in here we're going to pass a table with server equal to capabilities and on attached slightly different but not much all right I'm going to quickly add the other servers that I'm using so that's CSS I also have one for Tailwind CSS remember all of these I installed with Mason finally we have the Lua server you can copy all of this from my repository the Lua server has some specific details that we needed to configure to make it work properly like making the server recognize the global Vim variable that is available to us when we're editing our neovim configuration don't worry too much about that alright so let's save this now like the rest of our Lua files we have to import this in init.lua so let's open the file explorer go to init.lua close the Explorer we're going to add here require closing Dot plugins.lsp.lspconfig awesome save that we also have to configure LSP Saga which is the other plug-in we're using for LSP so let's open the file explorer go to the LSP folder press a to add a new file and we're going to call it LSP Saga dot Lua let's open it up this one isn't very difficult we just have to import the LSP Saga plugin by doing something like this so we're just importing it with a protected call again with the name of the plugin and now we have to add Saga dot init underscore LSP underscore Saga and this takes a table inside we're going to add a couple of options here like so this one called moving Saga is for moving around we're going to be using Ctrl K and control J like in the rest of the stuff we've configured I'm going to show you the specifics of how these work very soon as we're almost done now let's import it in init.lua by doing require close hand dot plugins.lsp.lsp Saga I think we should require this one before LSP config so I'm going to put it above it save now the final thing we have to do is configure some nvim CMP specifics so let's open the file explorer let's go to nvmcmp.lua close the Explorer and now we're going to add another import to this file this is going to be importing LSP kind to enable the vs code like icons to our Auto completion and now we want to configure our sources let's go to the end of this file as you can see here this is where we configure our sources we want to add here the nvim underscore LSP source so that LSP is also a source for our Auto completion finally we have to add these lines right here this is to configure the vs code like icons with LSP kind again you can copy this from my repository let's save this file let's quit neovim all right now that we've finished our LSP configuration let me show you everything you can do I'm gonna be opening up a small project I started recently for my blog I'm in the root directory I'm going to open up neovin by doing nvim I'm going to open the file explorer as you can see here we have a bunch of these fancy icons from one of the plugins we installed that's pretty cool let's go to the pages in this project this is a next JS project written in typescript it doesn't have that much right now let's go to the underscore app.tsx file let's close the file explorer now let's go down here and start typing I wrote n a and I can see here that I have a bunch of different recommendations from my LSP got variables keywords constants methods functions and we can see the information about them on the right hand side which is pretty cool note that I'm using Ctrl J and K to move through these if I press enter I'm going to use it and it also Imports it automatically to my file which is really nice let's undo that the other thing I want to show you is our keyboard shortcuts so if I go over here to nav and I want to see its definition I can do DD and LSP Saga will show me the definition of this nav function and I can actually make changes to it in this pop-up window which is pretty cool if I want to open this I can press enter let's jump back by doing control o Pro tip control o and control I are amazing and Vim so you can do control I to jump forward and Ctrl o to jump backwards it's really nice now if I do GF I can see the definition and the implementation the references and have a preview of it on the right hand side if I press enter I'll go into it the answer key works because of the key maps we defined it's jump back by doing Ctrl o I can also do GI to go to the implementation let's go back let's say I remove this import and we have an error down here which is the LSP working by the way I can jump to that error by doing closing bracket D and the light bulb indicates that I can do a code action to find a solution to it let's do leader CA which is our keybind 2C code actions and then here we have a recommendation of adding an import for component slash nav which is the import we deleted so let's press enter and we get the import back and the problem is fixed really nice note that if you have more than one error so let's remove those two Imports you can do closing bracket D to go forward and then opening bracket D to go backwards all right let's undo all of that let's remove the import for the nav again and let's go down here if I want to see the error once my cursor is above the map I can do later D note that for a lot of these so for example leader GD you can close it by pressing the q key if you do uppercase K then you're gonna do a hover Dock and get more documentation on wherever your cursor is some of these yet you just have to move around for them to go away let's try doing a rename so let's do leader GI to go to the implementation of this nav and then I want to change this name let's do leader RN to rename and then let's say I'm going to call it nav with two v's let's press enter it gets renamed and let's jump back to the other file so Ctrl o as you can see here this import gets updated with the two v's and this reference down here in line eight also gets the two v's really nice note that this change is not saved across your files when you do this and you want to save all your changed files you can do colon w a I want to also show you the typescript file rename functionality so if I go to the file explorer and go to nav and I want to rename the file I can do leader RF and then let's do nav with two v's here and then if I go back to the pages underscore app.tsx you're gonna see here that in the import we get the nav with 2vs which is awesome all right now that we're done with LSP the next thing we want to configure is code formatting and linting which is also pretty important I heavily rely on prettier for formatting my JavaScript and typescript code and eslint for linting and Diagnostics we're currently located in the dot config nvim directory let's open up neovim and open the file explorer let's go to the Lua directory hosian and then plugins Dash setup as we need to install some more plugins for this let's go to the end of the file what we're going to use for formatting and linting is the following plugin called null Dash LS we're going to use this to configure the formatters and linters like prettier and eslint but to install them we're going to use Mason for this as well so we're going to add this other plugin called Mason Dash null LS it is very similar to Mason Dash LSP config but this one is for the linters and formatters we're going to configure with null LS so up here in line 68 it's actually LSP servers linters and formatters that Mason will install let's save this file so that we can install the plugins and then once that's done we can press q and now we can go ahead and configure this let's open the file explorer let's go up to plugins and then LSP as this stuff is LSP related we're going to press the a key to add a new file and we're going to add null Dash LS dot Lua press enter and for again to open it now we have to import the null LS plugin by adding this protected call to import it like we've done with the other plugins notice that as we've configured LSP for Lua we're getting LSP functionality in here this says that we have an unused local nonetheless we're going to use it soon all right and now we're going to have two different variables one is formatting which will have a value of null underscore LS dot build ends dot formatting and the other one is going to be called Diagnostics with a value of null underscore ls.built ends dot Diagnostics and then we can call null underscore ls.setup in here we're gonna have a table we're going to have a set of sources these sources will be linters and formatters that Mason will install for us so before we add them let's actually go ahead and install them let's save this file open the file explorer go to the mason.lua file in here we want to import the Mason null LS plugin as you can see we're just using a protected call again to import this plugin and then down here we're going to use it much like with Mason underscore LSP config we're going to do Mason underscore null LS dot setup and then in here we're going to have insure underscore installed much like with the LSP servers we're going to add here the linters and formatters that we want Mason to install for us we're gonna have here prettier Sky Lua which is a formatter for Lua code and eslint which is our linter for typescript and JavaScript code you can verify the lenses and formatters you can install here in the Mason nonetheless plugin documentation now that we've done this let's save this file and then let's go back to the nolaness file and let's add our sources here so we have prettier which is a formatter to add it we can do formatting dot prettier then we have a formatter for Lua with stylua and then we have the eslint linter which we can add by doing Diagnostics dot es lint underscore d we want the formatting to happen whenever we save a file to do this we're going to add the following code down here I took this from the Nola documentation you can find it there or in the repo Linked In My description additionally we also have to define the following variable up here again this is directly from the nullest documentation you can find it easily in my repo now that we have that ready what's missing is we need to import this file in the init.lua file so let's go to file explorer init.lua and then down here we're going to do require ocn.plugins.msp.no LS and say let's quit I'm going to show you this in action again in my typescript blog I'm in the root directory I'm going to open neovim and because we have it installed the lenser than formatters before I'm going to open Mason by doing colon Mason and you'll see that in the background it's been installing these we have stylua for Lua code we have eslint for our Diagnostics and then we have prettier I forgot to mention that you can also open up Mason and you can use the number keys to go see your LSPs your debuggers actually linters and formatters see what you have installed and you can also browse this list and if you see something that you want to install you can just press the I key and it's going to start installing it so as you can see here I don't know what this is but it installed it you can uninstall it by doing uppercase X pretty cool now that we have prettier installed let's open the file explorer let's go to pages and then let's go to underscoreact.tsx Let's close the file explorer and let's change some of this these things up I'm going to add a bunch of spaces here um here as well and then I'm going to save this file and as you can see now that we saved it prettier does the formatting and it fixes everything for us which is exactly what we wanted to achieve additionally this blog doesn't have an eslint config file so up here we're getting an eslint error nonetheless Works through the LSP so I can use the same keybinds that I used before to see this es lens error I can do space d and it tells me that there is no eslan configuration found in my project great so that means that we have formatting and linting working properly one of the final important things we should configure is using free Setter for proper attacks highlighting in our projects as well as some plugins for Auto closing brackets parentheses quotes and tags with the help of tree sitter so I'm back here in the dot config NVM directory we're going to open up neovim and then we're going to go to our Lua halseyan plugin slash setup.lua let's go to the end of the file with shift G and now we need to install the plugins for configuring pre-sitter we're going to add the following to install nvim tree sitter which will help us configure things and this run function I took directly from the documentation so you can check this out there and also you can copy this from the repository Linked In the description of this video additionally we want to add the following two lines to install nvim auto Pairs and nvim TS auto tag to help us with closing stuff like parentheses brackets quotes and also tags let's save this to install these plugins I'm honestly not sure why but I got an error when I first saved this and the second time I saved it it worked fine I think everything is good to go I'm not really sure why that happened let me know in the comments if you know what I should change here all right now that we have these things installed let's open the file explorer and in the plugins directory we're going to add a new file by pressing the a key called autopairs.lua we're going to open that file up close the Explorer and then we're going to import the plugin like so same as always with a protected call then we can configure Auto pairs by doing Auto pairs dot setup in here we want to add the following lines to enable tree sitter compatibility with auto pairs again you can find this in the documentation or my repository we also want to configure Auto pair so that it works nicely with our Auto completion let's add the following lines to configure this all of this is from the auto pairs documentation you can find this stuff there or also my repository and then let's import this file back in init.lua like so I'll see I'm done plugins dot Auto pairs now let's go ahead and configure tree sitter as well so let's add another file inside the plugins directory by pressing the a key and typing out the name of the file is going to be called tree sitter.lua I'm going to open it up and then I'm going to import the endim tree seter plugin here and then I'm going to do tree sitter dot setup and in here I'm going to add the curly braces we're going to enable highlight so this means that we're going to get better syntax highlighting in our projects we're also going to enable indent for better indentation through tree sitter and also we're going to add Auto tagging note that to enable this you also need to have the auto tagging plugin installed after doing that we want to add an Ensure installed field here to add all of the languages that we want presetter to work with down here we're also going to add Auto install is equal to true this means that if these language parsers are not installed they'll be installed automatically when we open up neovin these are languages that are mostly relevant to my work you can remove from here or add things you can check out the NVM tree sitter documentation to look at a list of all of the languages that tree sitter supports awesome let's save this finally let's go back to the file explorer go to Anita Lua close the Explorer and then down here let's import policyon.plugins.tracesetter and save that's quick neopin all right I've opened this typescript file from my blog to show you the difference that tree sitter syntax highlighting makes it's pretty subtle here but you'll find yourself in other situations where the difference might be more noticeable right now tree sitter is enabled let's disabled it so I can do colon TS disable highlight and that's going to turn it off and then I can do TS enable highlight and that's on I also want to show you Auto tagging working so let's add here A P tag once I write the first tag it's gonna automatically add the closing tag which is really nice additionally we now have Auto closing of parentheses opening brace it auto closes it opening quote same thing that sort of deal now I'm back in the dark config slash NVM folder let's open up neovim this one final thing I want to add this is going to be really quick it's some git integration let's open up the file explorer go into Lua Halcyon plugins Dash setup let's go to the end of the file and down here we're going to add the following plugin called git signs let's save this to install it nice cue to close that then let's open up the file explorer go to the plugins directory press a to add a new file get signs dot Lua let's go in there let's import the plugin with a protected column like so and then the only thing we have to do is call git signs dot setup save this file now let's open up the file explorer again with leader e then go down to init.lua go to the end of the file and add require portion dot plugins dot get signs and save that now that's done let's quit neovim now I'm back in the directory for my blog this is a git repo so I can show you how this integration Works let's open up neovim let's open up the file explorer go to Pages then underscore app.tsx close the Explorer then let's add some new lines as you can see here we're getting green lines on the left hand side which is the git integration working that's blipp days let's delete line four and as you can see here we have a red Dash indicating that we deleted that line everything looks good let's quit out of here all right guys so that's it we're done I know that was a lot this is a really jam-packed video with a lot of information thanks for making it to the end if you made it this far this video was a lot of work to make so if you enjoyed it and found it helpful make sure to leave a like down below it really helps me out the next video will be about tmux and how we can combine neovan and tmux to create a really awesome workflow that one should be a lot more straightforward leave a comment down below if you have any questions or feedback for me thank you so much for all the support and don't forget to subscribe to the channel to see more content like this from me see you guys in the next one peace foreign
Info
Channel: Josean Martinez
Views: 408,823
Rating: undefined out of 5
Keywords: how i setup neovim on my mac to amke it amazing, text editor, vim plugins, configure vim, web development, how to configure vim, how to configure neovim
Id: vdn_pKJUda8
Channel Id: undefined
Length: 74min 57sec (4497 seconds)
Published: Tue Oct 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.