6

This is a feature in Eclipse that I would love to find a solution for in Vim.

I have Vim set up with tw=80 so that the lines wrap at 80 columns. When I'm in the middle of a string, and the line breaks, I have to change some stuff around so that the once single-lined string is formatted to be a multiline string. I'd like that when the line wraps and I'm in the middle of the string, this "conversion" is done automatically.

Example in Python:

(^ represents the cursor)

my_string = "Hello my name is John Smith and...^

I press return:

my_string = """Hello my name is John Smith and...
            ^

Or in C:

my_string = "Hello my name is John Smith and...^

I press return:

my_string = "Hello my name is John Smith and ..." + 
            "^

Please let me know if there is anything I can clarify. If anyone can point me in the right direction that would be great!

  • play with set autoindent or set noautoindent pre typing. Works for me in non python or C indenting – Steve Jan 7 '16 at 8:05
  • @mMontu thanks for the link. It seems that this can be fixed using formatexpr but as a Vim newbie, I'm not sure exactly what to do / what to add to my .vimrc. Any insight/advice would be greatly appreciated. – Zach Kirsch Jan 7 '16 at 15:48
  • The change isn't straightforward. Following that link you can see a similar "duplicate" comment on that question. And notice that none of those questions have accepted answers. Achieving what you described will require reading through those questions and the documentation on the referenced Vim options along a lot of trial and error. If you go through this effort please post your answer here, I'd like to have this in C :) – mMontu Jan 7 '16 at 16:41
  • I've just remembered I once had a similar need, but never had the chance to find a solution. – mMontu Jan 7 '16 at 16:45