I would like to indent my C++ code to have 1 shiftwidth after the scope specifier (private, public, protected) like the following code (assuming shiftwidth=3)
class blah
{
public:
blah();
private:
m_bl;
};
I'm currently using set cino=g0,h1.
However, vim always indents the lines after scope specifier to be as if gs was used, i.e, vim always adds 6 spaces instead of 3. The result looks like below:
class blah
{
public:
blah();
private:
m_b1;
};
I have tried several different options like:
set cino=g0,h0
set cino=g0,h-1
set cino=g0,h0.5s
None of them seem to be working. Vim always indents by 6 spaces (shiftwidth=3).
How do I achieve the indent results to get 1 shiftwidth after scope specifier?
I'm using Vim version 8.0.69 if that helps.
Related
i'm interested in configuring vim to indent haml attributes. right now they automatically indent to two spaces, but i want them to line up like this:
.some-div{ id: something,
data: { key: 'value' },
title: 'etc' }
.the-content
= whatever
this is my current sad state of affairs:
i want it to behave more like the way it indents ruby:
Use tabular to form vim:
https://github.com/godlygeek/tabular
There's a good vimcast on this:
https://www.youtube.com/watch?v=S33w7rcxbOk
I'm tending to rely on vim more than a full IDE for working on projects, and one of the things I find myself doing on a regular basis is creating a new file(s) with derived values.
For example, creating a new c++ class involves creating a .hpp file and a .cpp file, adding file comments, the license, the author, ctor/dtor, copy, assign, move, etc...
.hpp
class %Object% {
public:
explicit %Object%() = default;
~%Object%() = default;
%Object%(%Object%&& rhs) = default;
%Object%(const %Object%& rhs) = default;
%Object%& operator=(%Object%&& rhs) = default;
%Object%& operator=(const %Object%& rhs) = default;
protected:
private:
}
.cpp
#include "%Object%.hpp"
Another example would be a .h and a .c file in c.
I'm a little familiar with UltiSnips and muTemplate, which both seem to cut down on boilerplate a lot. However, I'm not clear if there's a way to use these, or something else, outside of a file scope. I wrote a really quick and dirty set of bash scripts to do it, and I'm getting ready to re-implement it in python, but I'd rather use an existing plugin.
Is there a way to do this with UltiSnips, muTemplate, or something else? If not, is there a good way to extend an existing plugin?
Discl. I'm the maintainer of mu-template and lh-cpp. Unfortunately I'm just seeing your question now -- I would say that you shouldn't have hesitated to drop me an email/an issue/... I'm not sure the question is still opened. I'm not even sure to have exactly grasped what you were looking for.
Since the version you've experimented with, I've added many templates/snippets/wizards in lh-cpp to generate classes according to their semantics. You can now either:
expand things like value-class, base-class, etc
or call a function to expand the same wizard/snippet/template and give it parameters. For instance, expanding a value class with a pointer-like parameter will trigger the generation of the copy constructor and assignment operator (otherwise, they would be defaulted, explicitly or implicitly depending on options and on the C++ flavour detected (C++98/03, C++11 or more -- rule of all or nothing still has to be enforced). Alas this approach is not very ergonomic at the moment. I have to find a way to simplify this task. You can find example of use in the test/spec directory of lh-cpp.
Note that the c++ template for C++ files are also highly customizable -- on a per project basis. Usual licence texts are ready to include. A new C++ file knows how to include its associated header file (if detected).
Add this to one of your start up file:
" Function to substitute the class names in a file
function! SubstituteClassName()
execute "1,$s/%Object%/" . expand("%:t:r") . "/g"
endfunction
" Function to create the skeleton of a header file
function! CreateHeaderFile()
1
insert
#pragma once
#ifndef %Object%_H
#define %Object%_H
class %Object% {
public:
explicit %Object%() = default;
~%Object%() = default;
%Object%(%Object%&& rhs) = default;
%Object%(const %Object%& rhs) = default;
%Object%& operator=(%Object%&& rhs) = default;
%Object%& operator=(const %Object%& rhs) = default;
protected:
private:
}
.
call SubstituteClassName()
endfunction
" Function to create the skeleton of a source file
function! CreateSourceFile()
1
insert
#include "%Object%.hpp"
.
call SubstituteClassName()
endfunction
function! CreateClassFiles(name)
" Open the header file.
execute "edit " . a:name . ".hpp"
" Create the skeleton of the header file
call CreateHeaderFile()
" Write the file
wa
" Open the source file.
execute "edit " . a:name . ".cpp"
" Create the skeleton of the header file
call CreateSourceFile()
" Write the file
wa
endfunction
Now you can create the skeleton .hpp and .cpp files using
call CreateClassFiles("myclassname")
I have a js file with following content:
function do_this(){
a = '{1}';
}
function do_that(a){
b = b + 1;
}
// vim: set fdm=marker fmr={,} :
When it folds it shows following:
function do_this(){
a = '{1}';
}
function do_that(a){ +-- 3 lines_____________
// vim: set fdm=marker fmr={,} :
I expect both functions to be folded. I guess "a = '{1}';" is getting in the way.
Is there a way to fix this using only the custom marker "{,}" within the modeline?
Unfortunately foldmarker does not allow regex matching, as specified by :h fmr. Therefore it will only match a literal string, so there's no way getting around the a = '{1}' in your example. However, it seems like what you really want is
// vim: set fdm=syntax fdls=1 :
with
let javaScript_fold=1
in your ~/.vimrc.
Here's my code:
public function setFileAvatar($fileAvatar)
{
$this->fileAvatar=$fileAvatar;
}
I select the whole code with "V" (note the upcase) then I type: ":'<,'>s/ileAvatar/ileNameAvatar" which is supposed to replace ALL "ileAvatar" by "ileNameAvatar"
But here's the result:
public function setFileNameAvatar($fileAvatar)
{
$this->fileNameAvatar=$fileAvatar;
}
The $fileAvatar are not replaced! How comes?
Your substitution is missing the /g flag; only the first occurrence in each line is replaced.
If you make this mistake frequently, you can consider changing the default via :set gdefault.
well, as the title says. the following is what looks like now in my c++ source code:
1
2 /*
3 * === FUNCTION =============================================================
4 * Name: sample_function
5 * Description:
6 * =============================================================================
7 */
8 void sample_function ( <+argument_list+> )
9 {
10 return <+return_value+>;
11 } /* ----- end of function sample_function ----- */
when i looked up the
c-support/templates/cpp.idioms.template
or
c-support/templates/c.idioms.template
everything is right.
it seems like when insert the template code ,vim format it again by itself.
can anybody help me?
The automatic indenting interferes with the way that the template is inserted. You should report this issue to the plugin's author.
You can temporarily work around the problem by turning off indenting; depending on your indent settings one of:
:set nocindent noautoindent nosmartindent indentexpr=