I have been trying to map {
in insert mode to the following if the line contains any character other than blank or tabs: <CR>{<CR>
and to {<CR>
if the line is blank (contains at most spaces or tabs), how would you do this? I am assuming vimscript is needed ?
I'm not sure this is what you want, but you could try this:
function! s:InsertBrace() abort
if getline('.') =~ '\S'
return "\n{\n"
else
return "{\n"
endif
endfunction
inoremap <expr> { <sid>InsertBrace()
Edit
If your vim version doesn't support vimscript functions, you could try the following:
inoremap <expr> { getline('.') =~ '\S' ? "^M{^M" : "{^M"
However, do not type ^M
literally, instead type <C-v>
(Ctrl+v) and then Enter. It will insert a carriage return character literally which vim displays with its caret notation ^M
.
-
ya this is what I wanted! It works in vim but I happen to use an emulation of vim that doesn't support vimscript functions (i just realized this) do happen to know any other way to do this? Thanks in any case. – fYre Dec 29 '15 at 16:57
-
@fYre I don't know if it will work with your vim version, but I've edited my answer to propose an alternative. I hope it will work. – saginaw Dec 29 '15 at 17:07
-
@fYre If it doesn't produce what you want, you should probably update your question, add a little more information (your vim version and which features it was compiled with) and refuse my answer as being a solution to your problem. Someone else will probably have another working solution for your vim version. – saginaw Dec 29 '15 at 17:28