Introduction and initial settings of Neovim, relationship with sudoedit and points to note#

The VSCode development style is sufficient on Windows, but if you dare to develop in a Linux environment, one option is to master the vi-based editor. In this article, we will introduce Neovim and organize its initial settings. We will also summarize the sudo nvim command and sudoedit that should be avoided.

1. Installing Neovim#

You can install it with the command below:

# Ubuntu / Debian 系

sudo apt install neovim

# macOS (Homebrew)

brew install neovim

# インストール確認

nvim --version

2. Preparing the settings directory and configuration files#

The location of the nvim configuration file is shown below. Initially create folders and files manually:

mkdir -p ~/.config/nvim
touch ~/.config/nvim/init.vim

If you already have init.vim, copy it with the following command:

mkdir -p ~/.config/nvim
cp init.vim ~/.config/nvim/

3. Initial init.vim sample#

Here is an example of init.vim with minimal usability:

set number              " 行番号を表示
set relativenumber      " 相対行番号
set tabstop=4           " タブ幅を4に
set shiftwidth=4
set expandtab           " タブの代わりにスペース
set smartindent         " スマートインデント
set clipboard=unnamedplus  " クリップボード連携
set hidden              " 編集中でもバッファ切り替え可

" --- 検索 ---
set ignorecase          " 大文字小文字を無視
set smartcase           " ただし大文字が含まれていたら区別
set incsearch           " インクリメンタルサーチ
set hlsearch            " 検索結果をハイライト

" --- カラースキーム ---
syntax on
colorscheme default

" --- ファイル保存時の自動処理(例:トレーリングスペース削除) ---
autocmd BufWritePre * :%s/\\s\\+$//e

4. Alias#

Recommended settings for aliases#

# ~/.bashrc または ~/.zshrc に追加
alias vim='nvim'
alias view='nvim -R'
alias vimdiff='nvim -d'

Note

You can use nvim more comfortably by setting an alias for vim or view. However, for now, we will leave vi as the original. There are times when you just want to use vi.

5. sudo nvim should be avoided#

sudo nvim is deprecated#

nvim stores temporary and session files in the following locations:

~/.local/state/nvim
~/.local/share/nvim

If you start nevim for the first time as a logged-in user with sudo nvim, these files will be owned by root and an error will occur on subsequent normal startups.

Example: Error that occurs#

E886: System error while opening ShaDa file /home/user/.local/state/nvim/shada/main.shada for reading: permission denied
E303: Unable to create directory "/home/user/.local/state/nvim" for swap file, recovery impossible: permission denied
E303: Unable to open swap file for "test", recovery impossible

Solution: Use sudoedit#

How to safely edit files with root privileges

export SUDO_EDITOR=nvim
sudoedit /etc/your-config.conf

This way, nvim edits the temporary file with user privileges, and root overwrites it when saving.

When ownership needs to be restored#

sudo chown -R $USER:$USER ~/.local/state/nvim ~/.local/share/nvim

6. Summary#

We have summarized the introduction and initial settings of neovim, and its relationship with sudoedit. In the future, we plan to continue installing and configuring plugins.

Article information

Post date:

2025-05-06

author:

Mr. Takagi