I would like to indent everything in vim with tabs, except a particular case. For example I have this c++ code(where <tab> is a tab character series and <s> is a space character series):
<tab>if(true &&
<tab><s>true)
<tab>{
<tab><tab>//code here
<tab>}
I would like after writing '&&' and press 'o' to jump on the next line and start writing to make vim put a tab and the number of spaces till '(' from the line before.
Is it possible to define this coding style in vim?
Thanks!
I think what you're looking for is the (N option for cinoptions. Try set cinoptions+=(0. According to the documentation, this looks like the alignment that you seek.
More information can be found by using help command: :help cinoptions-values or looking at the online version of the help for cinoptions-values.
As far as tabs go, you'll want to disable expandtab with :set noexpandtab, and you'll want to make sure your tabstops, soft tabstops, and shiftwidth are all set accordingly. As an example, the Linux source code uses a style like you mention above, and I have this in my vimrc:
setlocal ts=8 sts=8 sw=8 tw=80
" Don't expand tabs to spaces.
setlocal noexpandtab
" Enable automatic C program indenting.
setlocal cindent
" Don't outdent function return types.
setlocal cinoptions+=t0
" No extra indentation for case labels.
setlocal cinoptions+=:0
" No extra indentation for "public", "protected", "private" labels.
setlocal cinoptions+=g0
" Line up function args.
setlocal cinoptions+=(0
" Setup formatoptions:
" c - auto-wrap comments to textwidth.
" r - automatically insert comment leader when pressing <Enter>.
" o - automatically insert comment leader after 'o' or 'O'.
" q - allow formatting of comments with 'gq'.
" l - long lines are not broken in insert mode.
" n - recognize numbered lists.
" t - autowrap using textwidth,
setlocal formatoptions=croqlnt
Add following in your .vimrc
set tabstop=2
set expandtab
set shiftwidth=2
set smarttab
set linebreak
set smartindent
set cindent
set autoindent
This is all you need to roll out awesomeness in vim. :)
Related
I need a tip, tip or suggestion followed by some example of how I can add an extension in .txt format after the last character of a variable's output line.
For example:
set txt " ONLINE ENGLISH COURSE - LESSON 5 "
set result [concat "$txt" .txt]
Print:
Note that there is space in the start, means and fin of the variable phrase (txt). What must be maintained are the spaces of the start and means. But replace the last space after the end of the sentence, with the format of the extension [.txt].
With the built-in concat method of Tcl, it does not achieve the desired effect.
The expected result was something like this:
ONLINE ENGLISH COURSE - LESSON 5.txt
I know I could remove spaces with string map but I don't know how to remove just the last occurrence on the line.
And otherwise I don’t know how to remove the last space to add the text [.txt]
If anyone can point me to one or more solutions, thank you in advance.
set result "[string trimright $txt].txt"
or
set result [regsub {\s*$} $txt ".txt"]
I want to be able to automatically format code for the following rules using vim:
Rule 1): If expressions which are must be indeneted with 3 spaces. Example:
if(a &&
b)
(Note: b has three space-indent relative to the parent if, note that current vim behavior is 4)
Rule 2): parameters separated by space. Example:
function_call(a, b, c);
Rule 3): No space between assignment operators. Example:
int a=x;
Rule 4): Reference/dereference operator is attached to variable name not type. Example:
int &x = b;
Where possible, I want vim to do this stuff automatically as I am typing, however if this not possible, identifying formatting that is counter to the above rules (by marking them as errors) will also be helpful.
You can set auto-indentation rules in a custom indent file. Check out examples in the "indent" directory, somewhere like /usr/share/vim/vim74/indent, or in the Vim source code distribution.
You can set error highlighting rules in a custom syntax file. Find examples in the "syntax" directory, somewhere like /usr/share/vim/vim74/syntax, or again in the Vim source code distribution. Here's an example for JSON files:
" Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
syn match jsonNumError "\:\#<=[[:blank:]\r\n]*\zs\.\d\+"
If you want to actually re-format code automatically as you go you might need a special plugin like vim-autoformat and/or an external tool like ClangFormat.
Regarding indenting, and so on, check the options :h 'sw', :h 'cindent', :h 'cinoptions'...
Regarding where spaces and newlines shall be inserted,
For code already typed, clang-format is indeed the best way to go to reformat code. There is a plugin for vim.
For snippets, brackets and so on, lately I've worked on a plugin aimed at formatting text inserted by other plugins. Excesivelly inspired, I'm named the core plugin lh-style. It's used by mu-template (my snippet/templating plugin), and lh-brackets.
For other stuff you'll want to reformat on the fly, it'll be a little bit more complex. May be lh-style could help, I don't know, I haven't given much though on the subject yet.
For instance, outside comments and strings, = shall be expanded into :
itself after a [ (lamdbas),
<BS>=<space>, after =, >, <, ! followed by a space
<space>=<space> otherwise
EDIT: I got it all wrong, it does exactly the contrary of what you're looking for.
It'd be something like:
" ftplugin/c/mymappings.vim
function! s:InsertExpr(char) abort
let col = col('.')
let line = getline('.')
let syn = synIDattr(synID(line('.'),col-1,1),'name')
if syn =~? 'comment\|string\|character\|doxygen'
return a:key
endif
let lcut = getline('.')[: col-2]
let before =
\ lcut =~ '[=<>!] $' ? "\<bs>"
\ : lcut =~ "[=<>![ \t\n]$" ? ''
\ : ' '
let after = line[col-1] =~ "[ \t\n\\]]" ? '' : ' '
return before.a:char.after
endfunction
inoremap <buffer> <expr> = <sid>InsertExpr('=')
inoremap <buffer> <expr> < <sid>InsertExpr('<')
inoremap <buffer> <expr> > <sid>InsertExpr('>')
I recorded a macro to type curly braces {} using a given shortcut
Can I record a macro that takes any given highlighted text (word(s)), and adds the curly braces around it?
So I can highlight the words:
test here
and click the shortcut created with a macro, and turn the text to:
{test here}
This will work with one or many words:
1) Mark text
2) Goto Macro -> start recording
3) Cut highlighted text (CTRL+X)
4) Type: "{" then do CTRL-V then Type "}"
5) goto Macro -> Stop Macro
6) Save Macro as a shortcut
this should do the trick
Might be this should help:
TextFX > TextFX Edit > Indent and surround { text line }
The place I work has mixed-indents all over. Unfortunately, that is the style the company has decided on (yuck.) I would rather not see the "mixed-indent" errors produced by syntastic in vim, but I don't want to turn it off altogether. Is there a way to disable whitespace checks or something? Or can I switch to a better checker somehow? My vimrc automatically removes trailing whitespace and fixes indents, so I really don't need those checks anyway.
I suppose it's vim-airline.
Reading the help:
* customize the type of mixed indent checking to perform.
" must be all spaces or all tabs before the first non-whitespace character
let g:airline#extensions#whitespace#mixed_indent_algo = 0 (default)
" certain number of spaces are allowed after tabs, but not in between
" this algorithm works well for /** */ style comments in a tab-indented file
let g:airline#extensions#whitespace#mixed_indent_algo = 1
" spaces are allowed after tabs, but not in between
" this algorithm works well with programming styles that use tabs for
" indentation and spaces for alignment
let g:airline#extensions#whitespace#mixed_indent_algo = 2
Also:
* configure which whitespace checks to enable.
" indent: mixed indent within a line
" long: overlong lines
" trailing: trailing whitespace
" mixed-indent-file: different indentation in different lines
let g:airline#extensions#whitespace#checks = [ 'indent', 'trailing', 'long', 'mixed-indent-file' ]
I am using vim to manage a to-do list, and would like to set a hotkey to do the following:
Find all lines which contain the word under the cursor. The world is always a tag formatted as: #mytaghere
Sort all of these lines alphabetically by another tag contained within them. Again the tag is in #mytaghere format. For example, all lines with the tag #priorityA would be alphabetized and placed above all lines with the tag #priorityB, and so on.
Put all of these lines into Vim's quickfix window.
I have managed to achieve a limited version of 1 and 3 by putting the following into my .vimrc file:
map <f2> :vimgrep <cword> % <bar> copen <enter>
set iskeyword=#,48-57,_,192-255,95,35 "lets cword include #s and _s
This setup fails whenever the word under the cursor contains a #, and it works whenever the character is something else (for example, an _ ) The error produced is: "E682: Invalid search pattern or delimiter" which seems like the # is interfering with vimgrep.
Also, I can't figure out how to sort the lines by priority tags.
Thanks in advance for any help. I am just learning Vim, and if I could just get this problem solved I'd be able to use it for all of my organizing.
The :vimgrep command comes in two forms:
:vim[grep][!] /{pattern}/[g][j] {file} ...
:vim[grep][!] {pattern} {file} ...
For /{pattern}/, actually any non-keyword delimiter can be used. When your <cword> starts with #, Vim interprets this as the start delimiter, then complains because there's no end delimiter.
Choose a delimiter and put it around <cword>; to make the search literal, use \V and escape the current word, which is now inserted though the expression register =:
:noremap <f2> :vimgrep /<C-r>='\V'.escape(expand('<cword>'), '/\')<CR>/ % <bar> copen <enter>
Oh, and you should use :noremap; it makes the mapping immune to remapping and recursion.
Thanks to Ingo Karkat I was able to come up with the following solution:
noremap <f2> :vimgrep /<C-r>='\V'.escape(expand('<cword>'), '/\')<CR>/ % <bar> copen <enter>
The above will take everything under the cursor and put it into the quickfix window.
:set modifiable
:sort /.*|.*|/
The above will sort everything in the quick fix window ignoring the file name and line/column information.
To do everything at once (find all tagged lines and sort them by the first character of those lines):
noremap <f2> :vimgrep /<C-r>='\V'.escape(expand('<cword>'), '/\')<CR>/ % <bar> copen <enter> <bar> :set modifiable <enter> <bar> :sort /.*\|.*\|/ <enter>