How to show the current location in a yaml hierarchy

Vi and Vim Asked by Benjamin Reitzammer on December 3, 2020

When working with larger or rather deeply nested yaml files (Hi Rails), it happens quickly that I loose sight of where I am with regards to the indentation hierarchy of the file.

A very simple example to illustrate my point:

1| en:
2|   activerecord:
3|     errors:
4|       messages:
5|         parse_error: is syntactically not correct

When the cursor is on the line 5, I would like to somehow get the info
en -> activerecord -> errors -> messages -> parse_error
via a command or the likes.

3 Answers

The right way to do it would be to use an external program if the markup language presents a complex structure or particular edge cases, like the bunch or tools you can find for JSON.

Though YAML seems to be pretty straightforward Even if YAML seems to be pretty complex, I started with a one liner working on your snippet to end up with this Vim script you could place in ftplugin/yaml.vim:

function! YAMLTree()
    let l:list = []
    let l:cur = getcurpos()[1]
    " Retrieve the current line indentation
    let l:indent = indent(l:cur) + 1
    " Loop from the cursor position to the top of the file
    for l:n in reverse(range(1, l:cur))
        let l:i = indent(l:n)
        let l:line = getline(l:n)
        let l:key = substitute(l:line, '^s*(<w+>):.*', "\1", '')
        " If the indentation decreased and the pattern matched
        if (l:i < l:indent && l:key !=# l:line)
            let l:list = add(l:list, l:key)
            let l:indent = l:i
    let l:list = reverse(l:list)
    echo join(l:list, ' -> ')

nnoremap <F5> :call YAMLTree()<CR>

It loops on each line from the cursor to the top looking for a word w+ between zero or more spaces s* and a colon :, followed by anything. It also checks for indentation to recognize the keys hierarchy.

P.S. My first thought was about indent highlighting plugins like indentLine or vim-indent-guides. That's something I initially missed on Vim, but I think the heavy slow-down it can cause cured me.

Correct answer by LEI on December 3, 2020

I wrote vim-localorie for this. It shows you the yaml key of the current line with localorie#expand_key().

(In a Rails view it can also show you translations of the current line's i18n key, although that's not the question here.)

Answered by Andy Stewart on December 3, 2020

I'm using the yaml plugin by Meijvogel and that has a YamlDisplayFullPath function which he recommends putting into autodisplay mode though I prefer it on InsertLeave like so:

autocmd! InsertLeave *.yml YamlDisplayFullPath  

Answered by Thomas R. Koll on December 3, 2020

Add your own answers!

Related Questions

How to get the line numbers for all matches?

2  Asked on November 7, 2021 by xiaodong-huan


Error when installing Vim

1  Asked on November 5, 2021


What does :s (by itself) do?

1  Asked on August 31, 2021


Adding Filetype != filetypes

1  Asked on August 31, 2021


“The ycmd server SHUT DOWN” with vim 8.1

1  Asked on August 31, 2021 by lus-de-sousa


PATH order different in vim terminal window

1  Asked on August 31, 2021 by froxset


Shell syntax highlighting

2  Asked on August 31, 2021


v$ without trailing space?

1  Asked on August 31, 2021 by tallboy


Modify the path of a file be opened

0  Asked on August 31, 2021 by run_the_race


Use pipe symbol as separator in statusline

1  Asked on August 31, 2021 by user419050


Undo macro instances

1  Asked on August 31, 2021


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir