Skip to content

Instantly share code, notes, and snippets.

@siduck
Last active November 9, 2024 04:58
Show Gist options
  • Save siduck/048bed2e7570569e6b327b35d1715404 to your computer and use it in GitHub Desktop.
Save siduck/048bed2e7570569e6b327b35d1715404 to your computer and use it in GitHub Desktop.
Migrate from v2.0 to v2.5 ( Unix Only )
#!/bin/bash
# Run the script from home dir
cd ~
nvim_config="$HOME/.config/nvim"
nvim_data="$HOME/.local/share/nvim"
cp -r "$nvim_config" nvim_bak # backup nvim dir
cp -r "$nvim_config/lua/custom" .
# Remove prefix 'custom.' from all files
find custom -type f -exec sed -i 's/custom\.//g' {} +
# replace all plugins.configs -> nvchad.configs
find custom -type f -exec sed -i 's/plugins.configs/nvchad.configs/g' {} +
cd custom
# we will load this in main init.lua
mv init.lua myinit.lua
# disable these fields in chadrc
sed -Ei 's/M.mappings|M.plugins/-- &/g' chadrc.lua
# add new module paths
[ -e options.lua ] && sed -i '1s/^/require "nvchad.options"\n/' options.lua
[ -e autocmds.lua ] && sed -i '1s/^/require "nvchad.autocmds"\n/' autocmds.lua
[ -e mappings.lua ] && sed -i '1s/^/require "nvchad.mappings"\n/' mappings.lua
cd ..
rm -rf "$nvim_config" "$nvim_data"
# setup new config
git clone https://github.com/NvChad/starter "$nvim_config"
if [ -e custom/configs ]; then
mv custom/configs/* "$nvim_config/lua/configs/"
rm -rf custom/configs
fi
mv custom/* "$nvim_config/lua"
cd "$nvim_config"
# load custom.init.lua if it exists
[ -e lua/myinit.lua ] && echo "require 'myinit'" >> init.lua
# Some users have plugins.lua instead of plugins dir/ so move it in the plugins dir
[ -e lua/plugins.lua ] && mv lua/plugins.lua lua/plugins/myplugins.lua
nvim
@Maritsu
Copy link

Maritsu commented Mar 10, 2024

Is the branch supposed to be test on line 34, or should it be v2.5?

@siduck
Copy link
Author

siduck commented Mar 10, 2024

@Maritsu thanks for correcting, it should be starter. Starter config will pull Nvchad main repo's v2.5 branch!

@h0pes
Copy link

h0pes commented Mar 10, 2024

From which dir is the script expected to be run? (i.e. line 7 recursively copying to current dir). Thanks

@siduck
Copy link
Author

siduck commented Mar 10, 2024

@h0pes home dir

@h0pes
Copy link

h0pes commented Mar 10, 2024

Do you mean ~? Might be useful to add a simple cd ~ then. Thanks for the code comment though

@siduck
Copy link
Author

siduck commented Mar 10, 2024

Do you mean ~? Might be useful to add a simple cd ~ then. Thanks for the code comment though

yes

@LeonardoMor
Copy link

Lines 23 and 24 can be replaced with:

sed -Ei 's/M.mappings|M.plugins/-- &/g' chadrc.lua

This will comment these lines as opposed to alter them.

@siduck
Copy link
Author

siduck commented Mar 11, 2024

@LeonardoMor
Copy link

Also a small bug, line 48 should be replaced with:

[ -e lua/myinit.lua ] && echo "require 'myinit'" >>init.lua

@siduck
Copy link
Author

siduck commented Mar 11, 2024

Also a small bug, line 48 should be replaced with:

[ -e lua/myinit.lua ] && echo "require 'myinit'" >>init.lua

done

@Andrew15-5
Copy link

rm -rf $nvim_config $nvim_data

Line 32 suddenly lacks the double quotes.

https://sipb.mit.edu/doc/safe-shell/

You can throw in

set -eu

for good measure.

@igordertigor
Copy link

Unfortunately, this didn't work for me. It seems that the plugin installation part works without issues, but then I get

Error detected while processing /Users/xxx/.config/nvim/init.lua:
E5113: Error while calling lua chunk: cannot open /Users/xxx/.local/share/nvim/nvchad/base46/defaults: No such file or directory
stack traceback:
        [C]: in function 'dofile'
        /Users/xxx/.config/nvim/init.lua:32: in main chunk

When I press enter (to skip the error message) I get the next error message:

Failed to run `config` for ui
/Users/xxx/.local/share/nvim/lazy/ui/lua/nvchad/init.lua:2: loop or previous error loading module 'nvconfig'

# stacktrace:
  - /ui/lua/nvchad/init.lua:2
  - //NvChad/lua/nvchad/plugins.ui.lua:16 _in_ **config**
  - .config/nvim/init.lua:17

and a large message for base46 "build failed" then with red background:

/Users/xxx/.config.nvim/lua/chadrc.lua:14: module 'custom.mappings' not found:
  no field package.preload['custom.mappings']
cache_loader: module custom.mappings not found
cache_loader_lib: module custom.mappings not found
  no file './custom/mappings.lua'
  no file '/opt/homebrew/share/luajit-2.1/custom/mappings.lua'
  no file '/usr/local/share/lua/5.1/custom/mappings.lua'
  no file '/usr/local/share/lua/5.1/custom/mappings/init.lua'
  no file '/opt/homebrew/share/lua/5.1/custom/mappings.lua'
  no file '/opt/homebrew/share/lua/5.1/custom/mappings/init.lua'
  no file './custom/mappings.so'
  no file '/usr/local/lib/lua/5.1/custom/mappings.so'
  no file '/opt/homebrew/lib/lua/5.1/custom/mappings.so'
  no file '/usr/local/lib/lua/5.1/loadall.so'
  no file './custom.so'
  no file '/usr/local/lib/lua/5.1/custom.so'
  no file '/opt/homebrew/lib/lua/5.1/custom.so'
  no file '/usr/local/lib/lua/5.1/loadall.so'

The last error seems to suggest that there was some custom file that wasn't properly renamed. Any ideas how to fix this?

@siduck
Copy link
Author

siduck commented Apr 4, 2024

@igordertigor now you check the manual migration, check v2.5 release news article. you should understand how the new config structure works, and is same as general nvim configs unlike custom config abstraction in v2.0 . Man up!

So first of all check your chadrc, M.mappings and M.plugins in it are no longer needed

@bendavis78
Copy link

bendavis78 commented Apr 12, 2024

This script needs set -e at the top (or && or || exit 1 on each step), so it won't continue if there are previous errors. I ran it, got a bunch of errors, and couldn't read the errors before nvim started. In hindsight I should have just done each step manually.

@Andrew15-5
Copy link

I haven't done the migration yet, but the code is pretty big and has a lot going on. I already posted feedback about this (here), but I'll probably try to understand what it exactly does anyway and then see if I want to run it manually or not.

@siduck
Copy link
Author

siduck commented Apr 13, 2024

This script needs set -e at the top (or && or || exit 1 on each step), so it won't continue if there are previous errors. I ran it, got a bunch of errors, and couldn't read the errors before nvim started. In hindsight I should have just done each step manually.

feel free to send an updated version of the script, i'll add it in the migration docs on nvchad.com itself

@baiwfg2
Copy link

baiwfg2 commented Apr 14, 2024

hi. I'm configure nvim-dap. In 2.0, it can be configured like (copied from here https://github.com/dreamsofcode-io/neovim-cpp/blob/main/plugins.lua#L33):

 {
    "mfussenegger/nvim-dap",
    config = function(_, _)
      require("core.utils").load_mappings("dap")
    end
  },

Migrate it into lua/plugins/xxx.lua in v2.5, it reports error:

Failed to run `config` for nvim-dap

/root/.config/nvim/lua/plugins/myplugins.lua:35: module 'core.utils' not found:
^Ino field package.preload['core.utils']
cache_loader: module core.utils not found
cache_loader_lib: module core.utils not found
^Ino file './core/utils.lua'
^Ino file '/__w/neovim/neovim/.deps/usr/share/luajit-2.1/core/utils.lua'
^Ino file '/usr/local/share/lua/5.1/core/utils.lua'
^Ino file '/usr/local/share/lua/5.1/core/utils/init.lua'
^Ino file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/core/utils.lua'
^Ino file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/core/utils/init.lua'
^Ino file './core/utils.so'
^Ino file '/usr/local/lib/lua/5.1/core/utils.so'
^Ino file '/__w/neovim/neovim/.deps/usr/lib/lua/5.1/core/utils.so'
^Ino file '/usr/local/lib/lua/5.1/loadall.so'
^Ino file './core.so'
^Ino file '/usr/local/lib/lua/5.1/core.so'
^Ino file '/__w/neovim/neovim/.deps/usr/lib/lua/5.1/core.so'
^Ino file '/usr/local/lib/lua/5.1/loadall.so'

# stacktrace:
  - lua/plugins/myplugins.lua:35 _in_ **config**
Press ENTER or type command to continue
Failed to run `config` for nvim-dap-ui

/root/.local/share/nvim/lazy/nvim-dap-ui/lua/dapui/init.lua:36: nvim-dap-ui requires nvim-nio to be installed. Install from https://github.com/nvim-neotest/nvim-nio

How can I solve this ?

BTW, I install neovim 0.9.5 in customized path:

# ls /data/opt/nvim/
bin  lib  man  share

@siduck
Copy link
Author

siduck commented Apr 14, 2024

@baiwfg2 the mapping part isnt automatic, so please remove the core.utils** stuff and convert your mappings to new ones

@tariks
Copy link

tariks commented Apr 15, 2024

Works on macos with gsed in lieu of sed

@fishBone000
Copy link

fishBone000 commented May 23, 2024

The script will fail if there's a network error, like failed git clone e.t.c.
EDIT: nvm, I just saw the nvim_bak backup directory.

@siduck
Copy link
Author

siduck commented May 23, 2024

@fishBone000 such users must check the network first! like you're running a script that clones stuff then you should obviously have a stable connection

@EricDuminil
Copy link

rm -rf "$nvim_config" "$nvim_data"

Oh well. I had my precious undofiles in $nvim_data. A warning or automatic backup would have been nice in the above script.

@Andrew15-5
Copy link

This is why it is always advised to read the (shellscript) code that you are downloading/running from the Internet. You can't make shell scripts 100% foolproof (or rather, you would spend a huge amount of time just to make it safe). For a simple migration helper script, I don't think this is worth doing. Making backups just in case is also a good thing to remember.

I still haven't migrated, but I did goof up a few times in the past when playing around with shell commands and scripts. I did learn on my mistakes.

But I agree that a healthy reminder about reading the code before executing it and making backups would be nice to have at the top of the script (and maybe here too).

@siduck
Copy link
Author

siduck commented Sep 23, 2024

you are right @Andrew15-5 , i didnt think users might preserve ~/.local/share/nvim, fyi @EricDuminil undo stuff is in ~/.local/state/nvim , not share* one i think.

this page is a gist, the code is visible in front of your eyes + with comments so Ig users can read it quickly

@EricDuminil
Copy link

@Andrew15-5 @siduck I totally agree it was mostly a PEBKAC error.

My nvim config is a weird mix of potentially decade old lines. let &undodir = $HOME.'/.local/share/nvim/undo2', among others, and I honestly cannot remember why.

@Andrew15-5
Copy link

and I honestly cannot remember why.

That's why it's better to always document your configs. I now try doing that (didn't document anything in the past), and it will probably save me the headache of figuring out what does what and why. But I think when using Lua API in Lua, the code is more self-documented than the old Vimscript stuff.

@sananand007
Copy link

The scripts works well . But a word of caution to folks trying this, Make sure to back Up your original configurations.

@siduck
Copy link
Author

siduck commented Nov 9, 2024

@sananand007 doesnt the script already do that 😑
image

@sananand007
Copy link

my bad, yes it does @siduck .
I have all my custom plugins settings needed to be re-done after the update, so wanted to put a word of caution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment