I am currently thrown into a new project where the indentation style is a bit special. The basic rule is to use 'keyword+blank' spaces to indent the next line.
For example:
if () {
// indent 3 spaces here
}
while () {
// indent 6 spaces here
}
There are some (or a lot) of exeptions:
else if: use same number of spaces as if (3)
case in switch/case (2 spaces)
...
1) Is there already a plugin available that can do it for me? According to one of the developers this is called 'smart identation'. Unfortunately VIM's smartindent does something different.
2) If the answer to 1 is no. Is there an easy way to configure vim to respect these rules?
I'm not aware of any such plugin, and IMHO this scheme is anything but smart.
However, it is entirely possible to write a custom indent plugin that implements the exact requirements that you have. See :help 'indentexpr'; also, Vim ships with several indent plugins in $VIMRUNTIME/indent/*.vim that can serve as inspiration.
Basically, the algorithm would be like this:
Check the previous line for one of the keywords (if, while, and so on).
If there's a match, calculate the offset and add that to the previous line's indent (indent(v:lnum - 1)); else, use the previous line's indent as is.
If the line contains a }, find the line with the matching {, and use the indent from that line.
Related
Does anyone know a way to color highlight function calls in Vim?
I know that some plugins could do something like that by keeping record of tags, but with what I've found online, I could not figure out how to make it work.
I've tried using easy tags (which, by the way, doesn't seem to be maintained anymore) and gutentags, but to be quite honest, I haven't come much close to make any of them to work.
On the other hand, I imagine that it would be quite simple to implement a script to highlight anything that lies between a dot and a left parentheses or a blank space and a left parentheses (as in .anyCodeAtAll(), anotherCode()), but I have no idea how to do it. It would be a incomplete solution of course, but it would be good enough for my purposes at the moment.
Does anyone know how to make that work?
I have something like that in my configuration, but it's quite language specific. For example for Golang, I have a ~/.vim/after/go.vim which contains:
syntax match goCustomParen "(" contains=cParen
syntax match goCustomFuncDef "func\s\+\w\+\s*(" contains=goDeclaration,goCustomParen
" Exclude import as function name, for multi-line imports
syntax match goCustomFunc "import\s\+(\|\(\w\+\s*\)(" contains=goCustomParen,goImport
syntax match goCustomScope "\."
syntax match goCustomAttribute "\.\w\+" contains=goCustomScope
syntax match goCustomMethod "\.\w\+\s*(" contains=goCustomScope,goCustomParen
highlight def link goCustomMethod Function
highlight def link goCustomAttribute Identifier
highlight goCustomFuncDef ctermfg=13
highlight goCustomFunc ctermfg=43
highlight goCustomAttribute ctermfg=247
highlight goCustomMethod ctermfg=33
And for Python, I have a ~/.vim/after/python.vim:
syntax match pyCustomParen "(" contains=cParen
syntax match pyCustomFunc "\w\+\s*(" contains=pyCustomParen
syntax match pyCustomScope "\."
syntax match pyCustomAttribute "\.\w\+" contains=pyCustomScope
syntax match pyCustomMethod "\.\w\+\s*(" contains=pyCustomScope,pyCustomParen
highlight def link pyCustomFunc Function
highlight def link pyCustomMethod Function
highlight def link pyCustomAttribute Identifier
highlight pyCustomFunc ctermfg=43
highlight pyCustomAttribute ctermfg=247
highlight pyCustomMethod ctermfg=33
In each case, the first block defines what is a function, a method, an attribute, and so on, the second block link these custom definition to the generic classes "Function, Identifier..." and the 3rd block defines the colors.
The files need to be in the after directory to be executed after the colorscheme and highlights definitions.
Here's a side by side comparison of with and without these settings (look on the last 3 lines):
Unless someone has a better solution, you could adapt the above for the language you need it.
It might be that you have not installed your plug-in correctly.
Try following these steps (or retrace your steps) and see if it works / missed any steps out:
cd ~ Go to home directory.
vim .vimrc open .vimrc
Insert:
call plug#begin()
Plug 'xolox/vim-easytags'
call plug#end()
easytags#Options states easytags should work out of the box, but you could add option here in the
.vimrc file now or later,
e.g. put:
let g:easytags_syntax_keyword = 'always'
afer the call plug block.
Anyway.
:wq write quit the .vimrc
source ~/.vimrc unsure if required as will do later
vim test.js open vim with test.whatever language you know.
:PlugInstall in vim
Here might take a bit of time.
Then :q out of that window.
:source ~/.vimrc this is needed
Then test out to see if you have syntax highlighting.
I'm pretty sure this is along the right lines. Might be misspelled plugin name.
I was wondering what is the meaning of the orange part in the status bar?
☲ [573]trailing [515]mixed-indent [515:514]mix-indent-file
I am using a .vimrc downloaded from the web, so I am not sure what configuration it has. Could anyone kindly explain?
The existing answers have explained how to disable it and how to find information, but here's some more specific information.
Assuming it is vim-airline (since it does look identical to my vim-airline), this is its whitespace extension that checks for whitespace inconsistencies. Details can be found by running :h airline-whitespace.
Now, for the specific messages.
[573]trailing means that you have trailing whitespace on line 573, meaning that there are spaces/tabs/other whitespace following the last non-whitespace character on that line.
[515]mixed-indent means that line 515 has indentation with both tabs and spaces. For example, writing spaces as + and tabs as | >, line 515 might begin with | >| >++++++. This looks like a C source file where it is common to have indentation with tabs followed by spaces to align, like
// how it looks // with whitespace displayed
while (1) { // while (1) {
some_func_call(arg1, // | >some_func_call(arg1,
arg2); // | >+++++++++++++++arg2);
} // }
By default vim-airline detects this an error, but has a mode that allows this (let g:airline#extensions#whitespace#mixed_indent_algo = 2). More details can be found at :h airline-whitespace.
[515:514]mix-indent-file means that different lines (in this case 515 and 514) have different styles of indentation. Line 515 might be indented with tabs while 514 is indented with spaces, for example.
Every project/setup/language has different ideas of what kind of whitespace use is acceptable and what kind is not, and the whitespace extension is well customizable to suit specific needs. These options are once again found in :h vim-airline.
Foreword: this type of question is why most vimmers recommending crafting your own configuration piece-by-piece. Then you know all the pieces (and it’s 10x easier to debug).
Now, the meat: that looks like airline (since you don’t know, I can only guess). If so, running :help airline or checking out their GitHub page should explain that it has to do with trailing whitespace and mixed-tabs-and-spaces indents.
Just put let g:airline#extensions#whitespace#enabled = 0 in your .vimrc to disable it.
For the detail you can see the help pages with ":h airline-whitespace".
I'm a Fortran programmer who uses both free-form and fixed form. Since I have to mix them, usually I write code in a common form between free and fixed format, so in this way I can tell to vim that all my files are in the free format.
Vim is great in doing things like autoindentation, but I would like to type and let vim automatically wrap my code, and placing the Fortran continuation character & at column 73 (or greater), and at column 6 in the new line. Is it possible, or does it exist a plugin for this?
Currently I'm using textwidth=72 in fortran files to hard wrap the lines.
Thanks in advance.
One way to make vim insert text when going to a new line is to use formatexpr. Set it so to capture the line and replace it with itself with & and new line appended, when at/beyond a given column. In this case you are handling line breaks and textwidth does not apply. I didn't yet get to test some simple code for it, but here is a related example.
Another way would be to write general code so that when in a given column it inserts & and <CR>.
However, making any such approach respect Fortran-specific exclusions (comments, for one thing) will make it more complicated. The best solution would be to find suitable existing option(s) for Fortran, but I haven't so far.
This is a comment on indentation in general. It should allow you to directly set up a desired rule for a new line. Here is one standard set of files that set up a lot of indentation rules and features.
The usual entry point is this vim script, which requires another standard set of files. The link given on that page for the other files is broken though, so here is where to find them: unpack this zip file (found on this page), right into your ~/.vim/ directory. It will create subdirectories indent/, syntax/, and ftpplugin/, or put files into them if they exist, so be careful if you have stuff there already.
Then you can put the first script linked above into .vim/after/indent/. In this file, there are specific calculations of where to put the cursor when a new line is entered. Find the right place(s) and change to your desired indent, or preferably set up a snippet from it in another file (so not to change this file). In this case you also need to set things up so that it overrides settings from the first file.
A useful resource is indentexpr (or :help indentexpr).
Here is also a tutorial on that.
These are comments on syntax in general, posted initially. They contain items of help related to what you want and should be generally useful, but probably have not much to say about adding &.
There are plugins for fortran. Here is the syntax file, with many things to tweak.
This may already be on your system. (It was on mine.) Thus I would go through and pick and choose things to add to your .vimrc. Here are a few options that are directly related
syn match fortranContinueMark display "&"
syn sync linecont "&" minlines=20
Here are paragraphs that seem to me relevant in their entirety
if (b:fortran_fixed_source == 1)
if !exists("fortran_have_tabs")
"Flag items beyond column 72
syn match fortranSerialNumber excludenl "^.\{73,}$"lc=72
"Flag left margin errors
syn match fortranLabelError "^.\{-,4}[^0-9 ]" contains=fortranTab
syn match fortranLabelError "^.\{4}\d\S"
endif
syn match fortranComment excludenl "^[!c*].*$" contains=#fortranCommentGroup
syn match fortranLeftMargin transparent "^ \{5}"
syn match fortranContinueMark display "^.\{5}\S"lc=5
else
syn match fortranContinueMark display "&"
endif
if b:fortran_dialect != "f77"
syn match fortranComment excludenl "!.*$" contains=#fortranCommentGroup,#spell
endif
Then a block of syn match statements follow for common cpp-like settings, and then
"Synchronising limits assume that comment and continuation lines are not mixed
if exists("fortran_fold") || exists("fortran_more_precise")
syn sync fromstart
elseif (b:fortran_fixed_source == 0)
syn sync linecont "&" minlines=20
else
syn sync minlines=20
endif
By your question it appears that you know how to set up .vimrc but here are a few comments.
Syntax support need be enabled with appropriate enable and autogroup statements, for example
syntax enable
" au BufRead,BufNewFile *.f90 FileType=fortran
au FileType fortran setlocal ...
Here are some common formatting options that I have for fortran
autocmd FileType fortran setlocal formatoptions=croql comments=:/!/
There can also be a t among options, for textwidth
Here are some specific settings I have, which I see in this syntax file with far more sophistication
let fortran_free_source=1
" Said to need fortran.vim and/or fortran support packages (they work)
let fortran_do_enddo=1
let fortran_more_precise=1
Standard vim help is of course extensive, but try :help fortran -- it has a number of useful settings right up front and is not overwhelming at all. Also see ft-fortran-syntax from help.
See this post with some troubleshooting if things aren't working right. Here is another useful post, even as it appears unrelated by its title.
It appears, surprisingly, that more self-selected SO devs prefer to indent via tabs rather than spaces. Some people brought up the argument that you can use tabs to indent, and spaces to align. In theory this sounds cool, but in practice I suspect it would be more of a pain than anything, seeing as you can't see which character you have (unless you like turning on that sort of thing).
So I had an idea - why not the editors? Why shouldn't editors let you configure the number of spaces you're going to use to indent, but also the appearance of those spaces. That is:
Normal:
class MyClass:
____def myfun():
________somevariable = 42
________volts = 40000000 # If you're into that sort of thing.
________________________________# Not well-formatted Python, though.
Leading indent set to appear as 2 spaces:
class MyClass:
__def myfun():
____somevariable = 42
____volts = 400000000
Is it possible to do something like this with vim? I know it's totally possible to write a post-open/pre-save command to replace the contents, which might work the same... but I'm more curious if it's possible, in vim, to make it appear as though the leading spaces are less (or more) than they actually are?
Yes, you can, using the conceal feature. Demonstration (using the markup from your example text and a different replacement character instead of spaces for effect):
:syntax match Indent "\%(^\%(__\)*\)\#<=__" conceal cchar=#
:set conceallevel=2 concealcursor=nvic
The pattern matches every pair of __ at the beginning of the line, and replaces (conceals) each with a single #, effectively reducing the visible indent.
As a purely visual feature, I don't find it very useful though, and would prefer the post-open / pre-save solution you seem to be aware of.
I'm not sure this is possible, but I'm interesting in making this happen.
Ideally, I would like to map this feature to SHIFT+CTRL+3.
I'm looking for a way to have Vim enter a comment (single line) which corresponds to the syntax of the file I'm editing. If there are multiple single-line comment styles, Vim could either automatically pick one, or give me the choice. If the single-line comment has two parts (e.g. /* and */), then pressing SHIFT+CTRL+3 the first time will start the comment, and the second time will close the comment.
Examples:
Python: #
JavaScript: //
C, C++: /* with */ or //
I know there are scripts which will insert comments for you, but I haven't seen any that will do this based on the syntax of the file.
I highly recommend NERD Commenter.
Sort of! I don't believe vim will do this out of the box, but you can install plugins that will do fairly intelligent commenting (using movement keys, visual line highlighting, etc) that are specific to the filetype being edited. You can get these plugins off of vim.org, and you should be able to make your own key mappings in your .vimrc file if you don't like the ones they come with.
tComment is pretty well regarded, and has worked for me.
I've heard that EnhCommentify might be better, but I haven't used it myself.
Seems like a similar question to this:
How to comment in vim while respecting the indent?
Use the nerd commenter plugin:
http://www.vim.org/scripts/script.php?script_id=1218
See: this script which provides a function to commented a highlighted area in visual mode.
You want to start a comment in insert mode so your function would look more like:
fun CommentLines()
exe ":s#^#".g:Comment."#g"
endfun
Not quite what you're looking for, but efficient, and I suppose you know which comment to use.
(all this in command mode)
Put your cursor to the first line you want to comment. We willl then set a marker called a (valid names are a-z, single character) by typing
ma
put the cursor to the last line, then set a marker called b by typing
mb
Then comment the whole block (by searching for a newline and inserting the comment character (note the use of "#" as search delimiter because otherwise wee have to escape the "/")
:'a,'bs#^#//#
or for Python:
:'a,'bs/^/#/
To uncomment:
:'a,'bs#^//##
As we do line comments, it doesn't matter if we have other comments already in the file, they will be preserved.