1

After committing I realised that indents on GitHub show differently from what I see in Neovim.

I also noticed that all formatting (including indentation) is reset when an error occurs when loading the lazy package manager, the code in this case looks the same as in GitHub

I want my editor not to lie me so it looks exactly the same in all editors.

What am I missing? :)

Neovim:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  -- AUTOPAIRS
  {
    'windwp/nvim-autopairs',
    event = "InsertEnter",
    opts = {}
  },
  -- TELESCOPE
  {
    'nvim-telescope/telescope.nvim', tag = '0.1.5',
    dependencies = { 'nvim-lua/plenary.nvim' },

    config = function()
      local actions = require("telescope.actions")
      require("telescope").setup{
        defaults = {
          mappings = {
            i = {
              ["<C-k>"] = actions.move_selection_previous, -- move to prev result
              ["<C-j>"] = actions.move_selection_next, -- move to next result
            },
          },
        },
      }
      local builtin = require('telescope.builtin')
      vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
      vim.keymap.set('n', '<leader>gf', builtin.git_files, {})

    end
  },
  -- TREE-SITTER
  {
    "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate",
    config = function () 
      local configs = require("nvim-treesitter.configs")

      configs.setup({
        ensure_installed = { "html", "css", "javascript", "typescript", "lua", "vim" },
        sync_install = false,
        highlight = { enable = true },
        indent = { enable = true },  
      })
    end
  },
  -- COLORSCHEME
  {
    "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
    opts = {},
    config = function()
      require("tokyonight").setup({
        transparent = true
      })
      vim.cmd[[colorscheme tokyonight-moon]]
    end
  }
})

GitHub:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    -- AUTOPAIRS
    {
        'windwp/nvim-autopairs',
    event = "InsertEnter",
        opts = {}
    },
    -- TELESCOPE
    {
        'nvim-telescope/telescope.nvim', tag = '0.1.5',
        dependencies = { 'nvim-lua/plenary.nvim' },

        config = function()
            local actions = require("telescope.actions")
            require("telescope").setup{
                defaults = {
                    mappings = {
                        i = {
                            ["<C-k>"] = actions.move_selection_previous, -- move to prev result
                            ["<C-j>"] = actions.move_selection_next, -- move to next result
                        },
                    },
                },
            }
            local builtin = require('telescope.builtin')
            vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
            vim.keymap.set('n', '<leader>gf', builtin.git_files, {})

        end
    },
    -- TREE-SITTER
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        config = function () 
            local configs = require("nvim-treesitter.configs")

            configs.setup({
                ensure_installed = { "html", "css", "javascript", "typescript", "lua", "vim" },
                sync_install = false,
                highlight = { enable = true },
                indent = { enable = true },  
            })
        end
    },
    -- COLORSCHEME
    {
        "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
        opts = {},
        config = function()
            require("tokyonight").setup({
                transparent = true
            })
            vim.cmd[[colorscheme tokyonight-moon]]
        end
    }
})

options.lua:

vim.g.mapleader = " "

vim.wo.number = true
vim.o.mouse = 'a'
vim.o.termguicolors = true

vim.o.expandtab = false        
vim.o.shiftwidth = 2         
vim.o.tabstop = 2             
vim.o.smartindent = true
4
  • 2
    Tabs are always subject to interpretation (2, 4, 8 or a variable number of spaces?). If you want indentation to be the same everywhere, use spaces instead of tabs (by setting'expandtab').
    – Friedrich
    Jan 6 at 16:53
  • 1
    @Friedrich So i changed expandtab to "true" (my code doesn't changed at all) then i highlight whole file and pressed "=" (visually nothing changed, again) but git saw changes, after commit my github repo looks as my neovim code, thank you! What resources you can recommend to read about this stuff, i still kinda don't understand whole process, i always thought it's just about key preference, like making Tab = 2 Spaces and use Tab since it's just faster than clicking 2 times space Jan 6 at 17:49
  • 6
    Please don’t post images of text: the screenshots are useful to compare indents, but having the exact text would be helpful, too. Also, having the exact code in your configuration is far more useful than a screenshot. Please edit.
    – D. Ben Knoble
    Jan 6 at 21:28
  • 1
    @Glub-tubusWepple That's exactly how I have my editor set up. I hit the TAB key on the keyboard and it basically just hits space twice (or 4 times, depending on programming language). But some editors still insert a literal tab character (\t, ASCII code 0x09), which different tools display as different widths. The only place where tabs are required is in Makefiles (for extremely historical reasons). Outside of that, I second Luc's answer: I recommend always coding with spaces and configuring your editor to just insert spaces when you hit TAB. Jan 7 at 4:25

3 Answers 3

5

You indent your code with tab (expandtab = false). You have configured Neovim to show tab as 2 spaces (tabstop = 2, shiftwidth = 2).

It seems that GitHub display tab as 8 spaces.

If you want to align both you can adapt your configuration to match GitHub and replace 2 by 8.

vim.o.tabstop = 8
vim.o.shiftwidth = 8

If you want to convert your text to use space instead you can:

Change your configuration:

vim.o.expandtab = true

Convert tab into spaces by running the following Vim command:

:retab
5

Note that almost every tool that renders text with tabs has a way to configure tab size. It's the case with neo/vim, but also with github.

See the current official documentation and its screenshots. You could also append ?ts=2 to the url of the file you're consulting via github interface -- see this Q/A on SO.

I still prefer working without tabulations -- as some tools are really cumbersome to configure on this topic. See Vivian's answer for the how.

0

Instead of just telling Vim directly to use 2-tab spaces, use an EditorConfig file to tell many editors—including Neovim and GitHub—your preferences. Create a file named .editorconfig at the root of your repository and put inside it:

root = true

[*]
indent_style = tab
tab_width = 2
1
  • Vim also ships with an editorconfig plugin now (:help editorconfig-install)
    – D. Ben Knoble
    Jan 8 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.