Neovim: Difference between revisions

From NixOS Wiki
imported>AndASM
(Beginner-friendly link to Vim page, which contains most of the Neovim documentation.)
(import from old wiki)
(31 intermediate revisions by 13 users not shown)
Line 1: Line 1:
__TOC__
[https://neovim.io Neovim] is a fork of [[Vim]] aiming to improve the codebase, allowing for easier implementation of APIs, improved user experience and plugin implementation.


== Installation and configuration ==
Due to the large number of plugins, it is possible to extend and configure neovim to fit the exact needs of the user. Many users start working with neovim with a preconfigured neovim distribution (such as [https://www.lazyvim.org/ LazyVim], [https://astronvim.com/ AstroVim], [https://nvchad.com/ NVChad]) or use the preconfigured NixOS package for [[LunarVim]]. This way you can learn what is possible. Experienced users often advise to familiarize themselves with neovim and then create their own configuration from scratch.
Neovim shares most of it's configuration with [[Vim]]. See the [[Vim|Vim page]] for more details on the use of both.


== Configure Neovim as the default Editor ==
For people who like the modal text editors in a terminal but don't want to spend so much time configuring it, the [[Helix]] editor might be the right choice.
Add this to your <code>configuration.nix</code>


<code>environment.variables.EDITOR = "nvim";</code>
== Installation ==


== Set Neovim as <code>vi</code> and <code>vim</code> ==
=== System-wide ===
Add this to your <code>configuration.nix</code>


   nixpkgs.overlays = [
If you do not use Home Manager, you can use the following code in your NixOS configuration:
     (self: super: {
 
       neovim = super.neovim.override {
<syntaxhighlight lang="nix">
        viAlias = true;
programs.neovim = {
        vimAlias = true;
   enable = true;
      };
  defaultEditor = true;
    })
};
  ];
</syntaxhighlight>
 
You can also manually add Neovim to your packages. This should only be used if the two version above do not work for you.
 
<syntaxhighlight lang="nix">
environment.systemPackages = [ pkgs.neovim ];
</syntaxhighlight>
 
=== With Home Manager ===
 
[[Home Manager]] has a module for Neovim, which can be enabled via
 
<syntaxhighlight lang="nix">
programs.neovim = {
  enable = true;
  extraConfig = ''
     set number relativenumber
  '';
};
</syntaxhighlight>
 
More information about the module can be found here: [https://nix-community.github.io/home-manager/options.xhtml#opt-programs.neovim.enable Home Manager Manual].
 
== Configuration ==
 
Neovim shares most of its configuration with Vim. See the [[Vim|Vim page]] for more details on the use of both.
=== System-wide ===
 
The NixOS module does not have an <code>extraConfig</code> option as the Home Manager module does.
Instead, you can use the <code>programs.neovim.configure</code> option as described [https://search.nixos.org/options?show=programs.neovim.configure&type=packages&query=neovim here].
 
The following example configures RC commands and enables the plugin <code>ctrlp</code> to support fuzzy file search (see [https://github.com/ctrlpvim/ctrlp.vim homepage] on how to use it)
 
<syntaxhighlight lang="nix">
programs.neovim = {
  enable = true;
  configure = {
    customRC = ''
      set number
      set cc=80
      set list
      set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol:¶,precedes:«,extends:»
      if &diff
        colorscheme blue
      endif
    '';
    packages.myVimPackage = with pkgs.vimPlugins; {
       start = [ ctrlp ];
    };
  };
};
</syntaxhighlight>
 
To set Neovim as your default editor:
 
<syntaxhighlight lang="nix">
programs.neovim = {
  defaultEditor = true;
};
</syntaxhighlight>
 
Further, the NixOS module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
To use them, add the following to your NixOS configuration:
 
<syntaxhighlight lang="nix">
programs.neovim = {
  viAlias = true;
  vimAlias = true;
};
</syntaxhighlight>
=== With Home Manager ===
 
The Home Manager module does not expose many configuration options. Therefore, the easiest way to get started is to use the [https://nix-community.github.io/home-manager/options.html#opt-programs.neovim.extraConfig extraConfig] option.
You can copy your old config or directly load your default Neovim config via:
 
<syntaxhighlight lang="nix">
programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;
</syntaxhighlight>
 
To use Neovim as your default editor, you can set the <code>EDITOR</code> [https://search.nixos.org/options?show=environment.variables&type=packages&query=environment.variables environmental variable] to "nvim" by adding the following to your NixOS configuration:
 
<syntaxhighlight lang="nix">
environment.variables.EDITOR = "nvim";
</syntaxhighlight>


Alternatively you can use the neovim module (merged in september 2020).
The Home Manager module does also expose options to automatically add <code>vi</code> and <code>vim</code> aliases.
To use them, add the following to your Home Manager configuration:


  programs.neovim.enable = true;
<syntaxhighlight lang="nix">
   programs.neovim.viAlias = true;
programs.neovim = {
  viAlias = true;
   vimAlias = true;
};
</syntaxhighlight>


== Note on errors using default `packages` for plugins requiring Lua modules ==
==== Installing Plugins ====


[https://github.com/nanotee/nvim-lua-guide#a-note-about-packages Due to how the `runtimepath` for Lua modules is processed], your configuration may require `packadd! plugin-name` to require a module. A home-manager example:
Plugins can be installed using the <code>programs.neovim.plugins</code> option.
You can add only the plugin, or the plugin with its corresponding config:


  programs.neovim = {
<syntaxhighlight lang="nix">
    plugins = [
programs.neovim.plugins = [
      {
  pkgs.vimPlugins.nvim-tree-lua
        plugin = nvim-colorizer-lua
  {
        config = ''
    plugin = pkgs.vimPlugins.vim-startify;
          packadd! nvim-colorizer.lua
     config = "let g:startify_change_to_vcs_root = 0";
          lua require 'colorizer'.setup()
        '';
      }
     ];
   }
   }
];
</syntaxhighlight>
If you only add the plugin, you can add the configuration as described above.
An index of official packages can be found in on [https://search.nixos.org/packages?from=0&size=50&sort=relevance&type=packages&query=vimPlugins search.nixos.org].
In addition to the official packages, there are several user maintained repositories, such as [https://github.com/m15a/flake-awesome-neovim-plugins awesome-neovim-plugins] or [https://github.com/NixNeovim/NixNeovimPlugins NixNeovimPlugins]. Plugins which are
not available in any of these repositories may be integrated using the <code>vimUtils.buildVimPlugin</code> function from Nixpkgs:
<syntaxhighlight lang="nix">
pkgs.vimUtils.buildVimPlugin {
  pname = "whatever";
  version = "whatever";
  src = builtins.fetchGit {
    url = "https://github.com/example/whatever.git";
    ref = "whatever";
  };
  buildScript = ":";
}
</syntaxhighlight>
== Tips and tricks ==
=== Neovim Nightly ===


== To build neovim master ==
You can also use the [https://github.com/nix-community/neovim-nightly-overlay Neovim Nightly Overlay] to install the most recent current nightly version of neovim.


Use the official documentation:
You can run the master version via the following command:
https://github.com/neovim/neovim/wiki/Building-Neovim#nixos--nix
The neovim repository now contains a flake so you can run the master version via `nix run github:neovim/neovim?dir=contrib`


There is also an overlay available:
nix run "github:nix-community/neovim-nightly-overlay"
https://github.com/nix-community/neovim-nightly-overlay
 
==== Developing / Building ====
 
If you want to develop neovim, you may use
 
  nix develop "github:nix-community/neovim-nightly-overlay"
 
to acquire a suitable environment.
 
=== Note on Lua plugins  ===
 
Due to how the `runtimepath` for Lua modules is [https://github.com/nanotee/nvim-lua-guide#a-note-about-packages processed], your configuration may require <code>packadd! plugin-name</code> to require a module. A home-manager example:
 
<syntaxhighlight lang="nix">
programs.neovim = {
  plugins = [
    {
      plugin = pkgs.vimPlugins.nvim-colorizer-lua;
      config = ''
        packadd! nvim-colorizer.lua
        lua << END
require 'colorizer'.setup {
  '*'; -- Highlight all files, but customize some others.
  '!vim'; -- Exclude vim from highlighting.
}
END
      '';
    }
  ];
}
</syntaxhighlight>


== See Also ==
== See Also ==
[[Vim]]
* [[Vim]]
* [[Treesitter|Treesitter for Neovim]]


[[Category:Applications]]
[[Category:Applications]]

Revision as of 06:26, 27 May 2024

Neovim is a fork of Vim aiming to improve the codebase, allowing for easier implementation of APIs, improved user experience and plugin implementation.

Due to the large number of plugins, it is possible to extend and configure neovim to fit the exact needs of the user. Many users start working with neovim with a preconfigured neovim distribution (such as LazyVim, AstroVim, NVChad) or use the preconfigured NixOS package for LunarVim. This way you can learn what is possible. Experienced users often advise to familiarize themselves with neovim and then create their own configuration from scratch.

For people who like the modal text editors in a terminal but don't want to spend so much time configuring it, the Helix editor might be the right choice.

Installation

System-wide

If you do not use Home Manager, you can use the following code in your NixOS configuration:

programs.neovim = {
  enable = true;
  defaultEditor = true;
};

You can also manually add Neovim to your packages. This should only be used if the two version above do not work for you.

environment.systemPackages = [ pkgs.neovim ];

With Home Manager

Home Manager has a module for Neovim, which can be enabled via

programs.neovim = {
  enable = true;
  extraConfig = ''
    set number relativenumber
  '';
};

More information about the module can be found here: Home Manager Manual.

Configuration

Neovim shares most of its configuration with Vim. See the Vim page for more details on the use of both.

System-wide

The NixOS module does not have an extraConfig option as the Home Manager module does. Instead, you can use the programs.neovim.configure option as described here.

The following example configures RC commands and enables the plugin ctrlp to support fuzzy file search (see homepage on how to use it)

programs.neovim = {
  enable = true;
  configure = {
    customRC = ''
      set number
      set cc=80
      set list
      set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol:¶,precedes:«,extends:»
      if &diff
        colorscheme blue
      endif
    '';
    packages.myVimPackage = with pkgs.vimPlugins; {
      start = [ ctrlp ];
    };
  };
};

To set Neovim as your default editor:

programs.neovim = {
  defaultEditor = true;
};

Further, the NixOS module does also expose options to automatically add vi and vim aliases. To use them, add the following to your NixOS configuration:

programs.neovim = {
  viAlias = true;
  vimAlias = true;
};

With Home Manager

The Home Manager module does not expose many configuration options. Therefore, the easiest way to get started is to use the extraConfig option. You can copy your old config or directly load your default Neovim config via:

programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;

To use Neovim as your default editor, you can set the EDITOR environmental variable to "nvim" by adding the following to your NixOS configuration:

environment.variables.EDITOR = "nvim";

The Home Manager module does also expose options to automatically add vi and vim aliases. To use them, add the following to your Home Manager configuration:

programs.neovim = {
  viAlias = true;
  vimAlias = true;
};

Installing Plugins

Plugins can be installed using the programs.neovim.plugins option. You can add only the plugin, or the plugin with its corresponding config:

programs.neovim.plugins = [
  pkgs.vimPlugins.nvim-tree-lua
  {
    plugin = pkgs.vimPlugins.vim-startify;
    config = "let g:startify_change_to_vcs_root = 0";
  }
];

If you only add the plugin, you can add the configuration as described above.

An index of official packages can be found in on search.nixos.org. In addition to the official packages, there are several user maintained repositories, such as awesome-neovim-plugins or NixNeovimPlugins. Plugins which are not available in any of these repositories may be integrated using the vimUtils.buildVimPlugin function from Nixpkgs:

pkgs.vimUtils.buildVimPlugin {
  pname = "whatever";
  version = "whatever";
  src = builtins.fetchGit {
    url = "https://github.com/example/whatever.git";
    ref = "whatever";
  };
  buildScript = ":";
}


Tips and tricks

Neovim Nightly

You can also use the Neovim Nightly Overlay to install the most recent current nightly version of neovim.

You can run the master version via the following command:

nix run "github:nix-community/neovim-nightly-overlay"

Developing / Building

If you want to develop neovim, you may use

 nix develop "github:nix-community/neovim-nightly-overlay"

to acquire a suitable environment.

Note on Lua plugins

Due to how the `runtimepath` for Lua modules is processed, your configuration may require packadd! plugin-name to require a module. A home-manager example:

programs.neovim = {
  plugins = [
    {
      plugin = pkgs.vimPlugins.nvim-colorizer-lua;
      config = ''
        packadd! nvim-colorizer.lua
        lua << END
require 'colorizer'.setup {
  '*'; -- Highlight all files, but customize some others.
  '!vim'; -- Exclude vim from highlighting.
}
END
      '';
    }
  ];
}

See Also