0

enter image description here This is the leftmost my cursor goes in normal mode.

enter image description here This is how it looks in insert mode.

I have the following filetype settings

au BufNewFile,BufRead *.py
     \ set tabstop=4 |
     \ set softtabstop=4 |
     \ set shiftwidth=4 |
     \ set textwidth=79 |
     \ set expandtab |
     \ set autoindent |
     \ set fileformat=unix |

 au BufNewFile,BufRead *.js, *.html, *.css
     \ set tabstop=2 |
     \ set softtabstop=2 |
     \ set shiftwidth=2 |

 au BufNewFile,BufRead *.go
     \ set tabstop=4 |
     \ set softtabstop=4 |
     \ set shiftwidth=4 |
     \ set noexpandtab |
     \ set smarttab

This typically happens with .go files as I guess I have noexpandtab. But the cursor not going to column-0 in normal mode freaks me out.

Ishan Khare
  • 1,745
  • 4
  • 28
  • 59
  • sorry if that "freaks you out", but that's how vim works. Normal mode the cursor is shown _on_ the character, and in insert mode the curser is indexed _between_ the characters. – JimB Oct 08 '17 at 19:11
  • I understand, but i should still be able to navigate to `0th` index using arrow keys? Why does this only happen in case of `noexpandtab`. Why can I navigate to `0th` index when there are leading `4-space` and not when there is a leading tab? – Ishan Khare Oct 08 '17 at 19:26
  • 2
    Because your tabs are displayed as 4 spaces wide. You are on the 0th character, but the width is 4, so the cursor in shown in the 4th visual column. Inserting before it will move to the start of the line, and inserting after will move after the tab. – JimB Oct 08 '17 at 19:46
  • is there a way i can `display` the tabs as 4 spaces wide, but still don't convert to 4 spaces on write-out? – Ishan Khare Oct 09 '17 at 07:24
  • sure, that would maybe be another question, but the essence is, you can set and unset expandtab before/after saving with autocmds – Doktor OSwaldo Oct 09 '17 at 09:28
  • Possible duplicate of [gofmt producing mix-indent-file for vim](https://stackoverflow.com/questions/46608910/gofmt-producing-mix-indent-file-for-vim) – Peter Oct 09 '17 at 17:17

1 Answers1

2

What you see is normal Vim behavior. On a line that starts with a <Tab> character, the cursor is shown on the character; as a tab occupies multiply (default 8) screen cells, Vim puts the cursor at the end of the character (for double-width characters, Vim also double the character, but a 8-char wide cursor probably would look bad, and may not technically be possible in the terminal) in normal mode. In insert mode, the cursor is between characters, so you can position the cursor before (e.g. with gI) and after (I) the tab.

Remedies

It would be best to accept this behavior; if you really can't stand it, here are some options that change the behavior. Unfortunately, each comes with caveats and downsides:

  • With :set list, Vim puts the cursor at the front, as you want. But you'll see whitespace highlighted, too. You could fiddle with the 'listchars' option to reduce the visual noise, but then you'll lose that helpful feature.
  • With :set virtualedit=all, you can position the cursor anywhere inside a tab. Starting editing in the middle of it breaks the tab character into space before and after. Also, you can position the cursor anywhere behind physical text.
  • With :autocmds, you could convert tabs to spaces on reading the buffer, and back on writing. (See :help retab-example)
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324