junegunn/fzf.vim

Insert mode completion in empty line and preserving indentation - question

Closed this issue · 1 comments

Checklist

  • I have fzf 0.54.0 or later
  • I have searched through the existing issues

Output of :echo system(fzf#exec() .. ' --version')

0.54.0 (9e92b6f)

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Problem / Steps to reproduce

The question is: "is it possible to use fzf completion for the first word of indented line without loosing this indentation"?

I'll take this one as an example:
imap <c-j> <plug>(fzf-complete-word)

Steps to reproduce

  • press o to open new line
  • press <c-t> to insert indetation (4 spaces)
  • press <c-j> to activate fzf and choose a word
  • word is inserted in column 1 anyway

If I use vim's insert mode completion like <c-n> / <c-p> it keeps indentation.

I suppose it's not fzf fault. Inserting indentation in new line wit <c-t> and then going to normal mode by pressing Esc usually removes indentation. At least that's my experience with vim's indent feature. I guess this also happens when FZF completion is activated - it exits insert mode and indentation is lost.

One obvious workaround I found is entering part of the word in insert mode which serves as a prefix for fzf. Then it's no longer an empty line.

Other is using space key directly to indent line or using space+backspace after <c-t>. This is probably related to

Note: If you change the current line's indentation manually, Vim ignores the
cindent settings for that line. This prevents vim from reindenting after you
have changed the indent by typing , , or in the indent or
used CTRL-T or CTRL-D.

(from :h indentkeys)

Another is not using autoindent / indentexpr or removing 'o' from 'indentkeys' - but this makes editing horrible.

Thanks,

I've incorporated space/backspace 'hack' into my mappings and that seem to do the trick.

imap <c-j> <space><backspace><plug>(fzf-complete-word)
inoremap <expr> <a-e> "<space><backspace>" . fzf#vim#complete('cat file.txt')

Other approach could be using mappings like
inoremap <c-t> <c-t><space><backspace>
nnoremap o o<space><backspace>

but I came to the conclusion that I prefer Vim's behaviour of stripping indentation in empty lines after going to normal mode.

Not yet sure if this solution has any side effects but it works for me.
Closing the issue/question.