How can I line up HAML attributes in vim? - vim

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

Related

Is there extension or function to know bracket declaration when focued close bracket

I want to know declaration of open bracket when focused close bracket.(ex. if (...) ).
I know emacs, vscode, vim are has goto declaration function. But, they needs 1 action(type M-.(emacs),F12(vscode),%(vim)). I don't want to type some key each time. So, I want to know declaration of bracket with 0-action.
I don't care how displays in declaration(pop-up, mini buffer, status bar)
Background:
I'm in fixing legacy code. The code is too much nested with ifs and fors and whiles.
By much nested, end of code are many continus close bracket(}) like below.
for (var item in list){
if (cond1) {
...
while( cond2 ) {
...
if (cond3) {
...
} else {
...
}
}
}
list.append(item)
}
}
I usually mistake cond2 and cond3, created bugs, don't show log messages, and spent much time.
This question was translated by google translator. so, if you couldn't recognise this, please comment.
When your cursor is on a bracket, the other one is highlighted automatically if you have :help matchparen enabled.
When your cursor is on a bracket, you can jump to the opening one with :help %.
To quote Mass:
Yes- the plugin match-up has this feature:
https://github.com/andymass/vim-matchup
Using the option
let g:matchup_matchparen_offscreen = { 'method': 'popup' }
There is also the ability to show the match in the statusline (the
default):
let g:matchup_matchparen_offscreen = { 'method': 'status' }`

Best way to create substitution macros in vim

I'd like to set up some custom auto-complete macros in vim. I'm thinking something like this (| represents the cursor position):
it<TAB>|
immediately becomes:
it("|", function () {
});
Is this possible using straight vim, or would I need a plugin? If so, is there a preferred plugin out there?
Using an abbreviation you could write something like this:
inorea it it("", function () {<cr>});<c-o>k<c-o>f"
The purpose of <c-o>k<c-o>f" at the end is to reposition your cursor inside the double quotes, but it may not work all the time.
Using a mapping, you could try this:
ino <expr> <tab> <sid>expand_last_word()
let s:your_expansions = {
\ 'it': '\<c-w>it(\"\", function () {\<cr>});\<c-o>k\<right>',
\ }
fu! s:expand_last_word() abort
let last_word = matchstr(getline('.'), '\v<\k+%'.col('.').'c')
return has_key(s:your_expansions, last_word)
\ ? eval('"'.s:your_expansions[last_word].'"')
\ : "\<tab>"
endfu
You would have to add your abbreviations and their expansions inside the dictionary s:your_expansions.
Using the :read command, you could define larger snippets of code, and split them across several files:
ino <expr> <tab> <sid>expand_last_word()
fu! s:expand_last_word() abort
let last_word = matchstr(getline('.'), '\v<\k+%'.col('.').'c')
if last_word ==# 'it'
return "\<c-w>\<c-o>:r /path/to/some_file\<cr>\<c-o>f\"\<right>"
endif
return "\<tab>"
endfu
Here /path/to/some_file should contain your snippet:
it("", function () {
});
They are very simple solutions, if you want something more robust, you probably need a snippets plugin. One of them is UltiSnips, which requires that your Vim version has been compiled with Python support (:echo has('python') or :echo has('python3') returns 1).
With UltiSnips, you would write your snippet like this:
snippet it "your description" b
it("$0", function () {
});
endsnippet
Here the definition is included between the keywords snippet and endsnippet. On the 1st line, you can describe the purpose of your snippet, inside the string in double quotes. It will be displayed by UltiSnips inside a list, if you've defined several snippets with the same tab trigger, and there's an ambiguity.
The ending b is an option to prevent the tab trigger it from being expanded anywhere else than the beginning of a line.
$0 is a tabstop, it stands for the position in which you want the cursor to be, once the snippet has been fully expanded.
The readme page on github gives a quick start, and some links to videos.
If you want to have a look at the snippets written by other people, you can install vim-snippets.
There are other snippet managers but I don't know them well enough to describe the syntax of their snippets. If you want a comparison, here's one, and here are links for some of them:
snipmate
mu-template
neosnippet
xptemplate
Here is a abbreviation that you can use for your particular example
:inoreabbrev it it("", function () {<cr>});<esc>kf"a
Typing it followed by ctrl + ] in insert mode will render
it("|", function () {
});
and keep you in insert mode.
But I would definitely go for ultisnips and there is a screencast for creating snippets on the same page. That's why I am omitting the snippet here as you can do it yourself.

Is it possible to set a separate color scheme for the popup of CtrlP plugin?

Thought it is my first time ever to encounter vimscript, I've checked the plugin code. It has the following lines of code:
fu! ctrlp#init(type, ...)
if exists('s:init') || s:iscmdwin() | retu | en
let [s:ermsg, v:errmsg] = [v:errmsg, '']
let [s:matches, s:init] = [1, 1]
cal s:Reset(a:0 ? a:1 : {})
noa cal s:Open()
cal s:SetWD(a:0 ? a:1 : {})
gal s:MapNorms()
cal s:MapSpecs()
cal ctrlp#syntax()
cal ctrlp#setlines(s:settype(a:type))
cal s:SetDefTxt()
cal s:BuildPrompt(1)
if s:keyloop | cal s:KeyLoop() | en
endf
if I put colorscheme atom or something like that inside the function, it changes the color scheme of the whole buffer instead of just changing the color scheme of the popup. Is there any way to configure CtrlP to use a separate color scheme? Of course, I didn't intend to put configuration to the plugin code, so I'm also looking for a solution that enables me to put the setting into my .vimrc, for example something using autocmd.
+1 for looking at the source and actually trying something.
-1 for not looking at the plugin's documentation:
Highlighting:~
* For the CtrlP buffer:
CtrlPNoEntries : the message when no match is found (Error)
CtrlPMatch : the matched pattern (Identifier)
CtrlPLinePre : the line prefix '>' in the match window
CtrlPPrtBase : the prompt's base (Comment)
CtrlPPrtText : the prompt's text (|hl-Normal|)
CtrlPPrtCursor : the prompt's cursor when moving over the text (Constant)
* In extensions:
CtrlPTabExtra : the part of each line that's not matched against (Comment)
CtrlPBufName : the buffer name an entry belongs to (|hl-Directory|)
CtrlPTagKind : the kind of the tag in buffer-tag mode (|hl-Title|)
CtrlPqfLineCol : the line and column numbers in quickfix mode (Comment)
CtrlPUndoT : the elapsed time in undo mode (|hl-Directory|)
CtrlPUndoBr : the square brackets [] in undo mode (Comment)
CtrlPUndoNr : the undo number inside [] in undo mode (String)
CtrlPUndoSv : the point where the file was saved (Comment)
CtrlPUndoPo : the current position in the undo tree (|hl-Title|)
CtrlPBookmark : the name of the bookmark (Identifier)
Statuslines:~
* Highlight groups:
CtrlPMode1 : 'file' or 'path' or 'line', and the current mode (Character)
CtrlPMode2 : 'prt' or 'win', 'regex', the working directory (|hl-LineNr|)
CtrlPStats : the scanning status (Function)
Edit
Take a look at your favorite colorscheme, if it is competently written, you should find a bunch of lines looking like this one:
hi Whatever ctermbg=235 ctermfg=250 guibg=#262626 guifg=#bcbcbc cterm=NONE gui=NONE
What you need to do is simple:
copy one of those lines,
change the highlight group name from Whatever to CtrlPNoEntries,
adjust the color values to suit your taste,
repeat.
If you want your colorscheme to be standalone, you will need to define all those CtrlP highlights in a separate file that will be loaded like any other plugin but late in the startup process:
~/.vim/after/plugin/mycolorscheme.vim

Shortcut to select a file path text object

With vim text-objects I can copy some text like vib or vi". See :help object-select
I would be happy to have a similar shortcut for selecting a file path like "/my/path/to/file". In functions like gf or ^xf some kind of matching is done, but I could not find a matching shortcut.
Any ideas how to make a shortcuts like vif, yif or dif to work? Or any plugin suggestions?
You can define a text object called "file" quickly if vim-textobj-user is installed.
call textobj#user#plugin('file', {
\ 'file': {
\ 'pattern': '\f\+', 'select': ['af', 'if']
\ }
\ })
Thus vif, yif, etc. would work as expected.
This should be fairly easy to do with my CountJump plugin:
:call CountJump#TextObject#MakeWithCountSearch('', 'f', 'a', 'v', '\f\#<!\f', '\f\f\#!')
This creates an af text object, based on the 'isfname' setting (\f regexp atom).

How to automatically insert braces after starting a code block in vim?

It's really easy to insert a closing brace after typing the opening one:
inoremap { {<CR>}<Esc>ko
This way
if (true) {
converts to
if (true) {
|
}
But I'd like to save time and to type 1 character less:
if (true)<CR>
So I'd like to create the following rule: if return is pressed and the line starts with if/for/while, execute {<CR>}<Esc>ko
Is this doable?
Thanks
Building on your previous mapping, this should do what you want:
inoremap )<CR> ) {<CR>}<Esc>ko
However, you should try a snippet expansion plugin like SnipMate or Ultisnips. Both plugins allow you to define snippets with placeholders and miroring (lots of them are included by default) that are expanded when a <Tab> is pressed after a trigger.
For example, you have a snippet associated with the trigger if that expands into:
if ([condition]) {
}
condition is selected, ready for you to type, and, once you are done, you can hit <Tab> again to jump the cursor between the curly braces, ready to type:
if (myVar == 5) {
|
}
This is incredibly handy.

Resources