Enable smart indent on curly braces - go

I have to do this every time when I open curly braces
vim config for indent:
"" Fix backspace indent
set backspace=indent,eol,start
"" Tabs. May be overridden by autocmd rules
set autoindent
set smartindent
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set smarttab
What to do?

You could have a look at amix/vimrc, a powerful vim configuration.
I have installed the awesome version and it could implement what you want. Moreover, it has lots of useful configurations that could improve your efficiency.
If you write go, you could install fatih/vim-go, a powerful plugin adds go language support for Vim.

delimitMate does the thing. Set option delimitMate_expand_cr to 1 or 2 in your .vimrc:
let delimitMate_expand_cr = 2
See:
:h delimitMate_expand_cr
:h delimitMateExpansion
Also you can use simple mapping on {{ to do same:
inoremap {{ <C-o>o{<CR>}<C-o>O

Related

Upgraded my macOS to Monterrey 12.5.1 and now the vim colors are different. How to get them back to the way they were before?

Upgraded my macOS to Monterrey 12.5.1 and now the vim colors are different:
For reference, these are the default colors of my terminal:
The default font color is gray, and this was the case before for vim as well, before the update.
Is there an easy way for me to come back to the default syntax (before the update)?
According to https://vi.stackexchange.com/questions/37484/upgraded-my-mac-os-and-now-the-vim-colors-are-different-is-there-an-easy-way-fo I tried adding, set bg=light to my .vimrc but it didn't work.
I'm using now a plugin (Plug 'tribela/vim-transparent') to remove the black background, which is something that really helps; but the font colors are still "shifted". This is what it looks like for me now:
The yellow line numbers and the blue “~” symbols used to be both gray. The rest of the font colors seems to be “shifted” from their original colorscheme (elflord) and they are always shifted no matter what colorscheme I use.
this is my .vimrc for reference:
set backspace=2 " backspace in insert mode works like normal editor
syntax on " syntax highlighting
filetype indent on " activates indenting for files
set autoindent " auto indenting
set number relativenumber " turn hybrid line numbers on
colorscheme elflord " colorscheme desert
set nobackup " get rid of anoying ~file
set ignorecase " make search case insensitive
set smartcase " make search case insensitive
"set bg=light
"autocmd ColorScheme * highlight! Normal ctermbg=NONE guibg=NONE
"hi Normal guibg=NONE ctermbg=NONE
"autocmd SourcePost * highlight Normal ctermbg=NONE guibg=NONE
" \ | highlight LineNr ctermbg=NONE guibg=NONE
" \ | highlight SignColumn ctermbg=NONE guibg=NONE
" ========= Pluggins ========
call plug#begin('~/.vim/plugged')
" For solving color changed issues after mac update
Plug 'tribela/vim-transparent'
" For using fzf on vim
set rtp+=/usr/local/opt/fzf
" Installation of HardTime plug in
" Vim HardTime
Plug 'takac/vim-hardtime'
call plug#end()
Background variable
A strange thing about the background variable is that when I open a vim session at first, it gives these colors, and the value is dark:
then, I switch it to light:
and finally switch it back to dark, the colors are mostly gray:
The plugin Plug 'tribela/vim-transparent' also stops working after this change, so there seems to be an issue with this background variable...
Any suggestions? :)
We recently rewrote all the built-in color schemes except default.
Whenever possible, we based the rewrite on the highest definition information available, the GUI colors to achieve maximum consistency. In all cases, it means that the TUI experience is brought closer to the GUI experience but it also means that people used to the old, broken, look of their favorite colorscheme might get a shock.
What you get now is how elflord was supposed to look to begin with. If you want the old elflord, you can find it here.

I want to removing highlighted braces in vim

" Configuration file for vim
set modelines=0
" CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible
" Use Vim defaults instead of 100% vi compatibility
set backspace=2
" more powerful backspacing
" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup nobackup
syntax on
set ai
set shiftwidth=4
set tabstop=4
set ruler
set backspace=2
set ic
set hlsearch
set incsearch
set smartindent
set confirm
set history=200
set cursorline
set number
:nohl
:set nowrap
set mouse=a
colo google
hi Normal ctermbg=none
the above is my .vimrc
As you can see, my braces was highlighted at line 3 and line 5 although my cursor is not on them(so, the highlighting may not be controlled by MatchParen).
However, while I can't remove their highlighting by using MatchParen, I think there must be another way to deal with it.
How can I disable these highlighting???
thx :)
You need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. When you have the name of the offending syntax group, you can investigate where it comes from; (the last lines of) :scriptnames may help.

How to set line numbers by default in vim?

I know you can add line numbers in vim by using,
:set number
How do I set this to default behavior?
add this line to ~/.vimrc (if not exist, create a new file)
:set nu
and save the file
the settings in $HOME/.vimrc file would be loaded automatically.
Except for set number to show linenumber, I have this to toggle normal line number and relative line number: (by pressing <leader>nu)
"---------------------------------------------------------
"toggle relativeline number
"---------------------------------------------------------
function! ToggleRelativeNumber()
let &relativenumber = &relativenumber?0:1
"let &number = &relativenumber? 0:1
endfunction
nnoremap <silent> <Leader>nu :call ToggleRelativeNumber()<cr>
https://github.com/sk1418/myConf/blob/master/common/.vimrc#L704
Open the file /etc/vim/vimrc (in sudo mode) and add the following line: set number
By the way, you will also find other (highly recommended) interesting commands you can enable:
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)

Invalid Character in a SQL request with Vim

I'm facing a weird issue since yesterday. I'm coding a PHP webservice with request a Oracle DB.
I write a request in vim, say:
SELECT
RBK_ISBREF AS ISBREF,
RBK_KEY AS BL_KEY,
RBK_USERID AS USER_ID,
FROM RULBLACKLIST
WHERE
RBK_KEY = 'identiteClient'
AND RBK_VALUE = 'foo'
It returns ORA-00911 Invalid Character.
The weird part is, my request is OK in SQL developper (and returns values) and if i change my IDE from Vim to whatever it returns the correct values…
Is is a format/encoding missconfiguration in Vim ?
In my .vimrc i got these:
set fileformat=unix
set fileformats=unix,dos
" Indentation
"-----------------------
"-----------------------
set autoindent
set smartindent
set backspace=indent,eol,start
" Convert tabs to spaces
set tabstop =4
set shiftwidth =4
set softtabstop =4
set expandtab
Any tips?
Check the file's encoding; Vim might write in a format that the Oracle tool doesn't understand.
:setlocal fileencoding? bomb? fileformat?
Especially a byte order mark might be the culprit. Alternatively, you can also do a binary compare of otherwise identical good and bad files.

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt

Resources