I'd like to refactor this condition in my vimrc:
if &term =~ "xterm" || &term =~ "screen" || &term =~ "builtin_gui"
" do some stuff...
endif
In Ruby I'd probably do something like:
names = ["xterm", "screen", "builtin_gui"]
if names.any? { |n| &term =~ n }
" do some stuff...
endif
I know Vimscript doesn't have anything like Ruby blocks, but is there any builtin function that would let me do something along these lines?
=~ a regexp match. So you can use full power of regexp. Either of these should work:
&term =~ 'xterm\|screen\|builtin_gui'
&term =~ join(names, '\|')`
Not really
let names = ["xterm", "screen", "builtin_gui"]
if len(filter(names, '&term =~ v:val')) > 0
endif
Related
Here I want to minify my code:
let s:next_col = getline('.')[col('.') - 1]
if s:next_col is# "'" || s:next_col is# '"' || s:next_col is# '`' || ')' || s:next_col is# ']' || s:next_col is# '}' || s:next_col is# '>'
return "\<right>"
endif
The above code work as expected. But when I try convert it to this:
let s:next_col = getline('.')[col('.') - 1]
" I also try to add / before \V, not sure if its correct but that also won't work
if s:next_col is# '\V\("\|`\|)\|]\|}\|>\)' || s:next_col is# "'"
return "\<right>"
endif
Now, the only it work will be if the next column is '.
I wonder how I can grouping these character ' " ) ] } > and check if one of those matching with my variable?
is and its variants are only useful for comparing lists or dictionaries. If you want to compare strings, then the right operators would be ==, !=, =~, !~ and their case variants.
Here, you can simply use =~ to compare with a regular expression pattern and, since punctuation marks don't have casing, there is no need for # or ?:
let s:next_col = getline('.')[col('.') - 1]
if s:next_col =~ "['\"`)\\]}>]"
return "\<right>"
endif
[...] is a collection. See :help /[].
', `, ), }, and > are all used as-is because they have no special meaning in Vim's regex dialect.
" is escaped with a \ because double quotes are used for quoting the pattern.
] is escaped once because ] closes the collection, which makes it special in Vim's regex dialect, and a second time because \ is special in double quotes. See :help expr-".
I have a file in which, below is the content.
A
A
A
A
A
A
A
A
A
.
.
.
A
Piece of code...
A
A
A
.
.
A
I want something like this using folding in vim
A
Piece of code
A
2 folds are created by folding repeated lines.
This should happen automatically as I open the file. Is it possible by doing it in vimrc?
How about this :help fold-expr:
setlocal foldenable foldmethod=expr
let &l:foldtext = 'printf("+-%s %d times: %s", v:folddashes, (v:foldend - v:foldstart + 1), getline(v:foldstart))'
let &l:foldexpr = 'getline(v:lnum) ==# getline(v:lnum + 1) && v:lnum < line("$") ? 1 : (getline(v:lnum - 1) ==# getline(v:lnum) ? "<1" : 0)'
It should be possible by using setlocal foldmethod=expr where you can write your own function:
setlocal foldmethod=expr
setlocal foldexpr=CustomFold(v:lnum)
function! CustomFold(lnum)
if getline(a:lnum) == getline(a:lnum-1) || getline(a:lnum) == getline(a:lnum+1)
return '1'
endif
return '0'
endfunction
However this is untested and you wouldn't want to do it for all files. But it should point you in the right direction. It also would not 100% match your criteria, but once you have a specific problem, you can always ask again
I want to set the &titlestring in my .vimrc to contain the short hostname. When I have this, it works fine:
let &titlestring = $USER . "#" . hostname() . " | " . expand("%:~")
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
set title
endif
But it prints the full hostname. In order to get the short hostname, I tried this:
let hostname=system('hostname -s')
let &titlestring = hostname . " | " . expand("%:~")
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
set title
endif
But then I get | ~/.vimrc echoed to the input line and Thanks for flying Vim in the titlebar. How do I get the short hostname in the titlebar?
I wouldn't launch an external command for that; the system() call in your .vimrc may explain the strange symptoms.
Why don't you extract the short hostname (first part, up to .) via substitute()?!
let &titlestring = $USER . "#" . substitute(hostname(), '\..*$', '', '') . " | " . expand("%:~")
I currently have the following in my .vimrc
let g:Myvar="noisy"
function Myfirstfunction()
if g:Myvar=="noisy"
echo "noisy"
else
echo "quiet"
endif
endfunction
I would like to be able to change (or toggle) g:Myvar using :set
:set g:Myvar=quiet
Of course, the current set up doesn't work, hence the question: how can I toggle a custom option using :set? I'm not convinced that my approach so far is valid, so I am very open to it being overhauled.
let g:Variable = "noisy"
function! MyFirstFunction()
if g:Variable == "noisy"
echo "noisy"
else
echo "quiet"
endif
endfunction
I don't see the problem with the above. "It works on my machine" :) But, I'm not quite sure what exactly are you asking next - let g:Variable="quiet" works also.
Are you asking how to make function that will toggle one variable between two different values?
let g:Variable = 1
function! TogglingVariable()
if g:Variable == 1
let g:Variable = 0
echo "Variable is now 0"
else
let g:Variable = 1
echo "Variable is now 1"
endif
endfunction
If neither of these is what you want, you'll have to explain it a bit more then. This is all I could conclude from the question.
I downloaded Scala 2.8, installed the vim scripts included and tried to type in some Scala code. When I typed in val x = 1 + 2 and hit ENTER, the indentation goes to below the v. When I type in val x = (1 + 2), the indentation is below the x!
If VIM is used by anyone at all for Scala, this bug should've been seen long ago. Or am I the only one seeing this?
With the indent/scala.vim from the current 2.8.0.final release I have the same outcome... But I know, that it worked in a earlier release, because I have one file here where it works. Here it is:
" Vim indent file
" Language : Scala (http://scala-lang.org/)
" Maintainer : Stefan Matthias Aust
" Last Change: 2006 Apr 13
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal indentexpr=GetScalaIndent()
setlocal indentkeys=0{,0},0),!^F,<>>,<CR>
setlocal autoindent sw=2 et
if exists("*GetScalaIndent")
finish
endif
function! CountParens(line)
let line = substitute(a:line, '"\(.\|\\"\)*"', '', 'g')
let open = substitute(line, '[^(]', '', 'g')
let close = substitute(line, '[^)]', '', 'g')
return strlen(open) - strlen(close)
endfunction
function! GetScalaIndent()
" Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1)
" Hit the start of the file, use zero indent.
if lnum == 0
return 0
endif
let ind = indent(lnum)
let prevline = getline(lnum)
"Indent html literals
if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$'
return ind + &shiftwidth
endif
"### Taken from mail on scala mailing list
"### -------------------------------------
" Add a 'shiftwidth' after lines that start a block
" If if, for or while end with ), this is a one-line block
" If val, var, def end with =, this is a one-line block
"if prevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
"\ || prevline =~ '^\s*\<else\>\s*$'
"\ || prevline =~ '{\s*$'
"let ind = ind + &shiftwidth
"endif
" Add a 'shiftwidth' after lines that start a block
" If if, for or while end with ), this is a one-line block
" If val, var, def end with =, this is a one-line block
if prevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\)\>.*[)]\s*$'
\ || prevline =~ '^\s*\<\(\(va[lr]\|def\)\>.*[=]\s*$'
\ || prevline =~ '^\s*\<else\>\s*$'
\ || prevline =~ '{\s*$'
let ind = ind + &shiftwidth
endif
" If parenthesis are unbalanced, indent or dedent
let c = CountParens(prevline)
echo c
if c > 0
let ind = ind + &shiftwidth
elseif c < 0
let ind = ind - &shiftwidth
endif
"### Taken from mail on scala mailing list
"### -------------------------------------
" Dedent after if, for, while and val, var, def without block
"let pprevline = getline(prevnonblank(lnum - 1))
"if pprevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
"\ || pprevline =~ '^\s*\<else\>\s*$'
"let ind = ind - &shiftwidth
"endif
" Dedent after if, for, while and val, var, def without block
"let pprevline = getline(prevnonblank(lnum - 1))
if pprevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\)\>.*[)]\s*$'
\ || pprevline =~ '^\s*\<\(\va[lr]\|def\)\>.*[=]\s*$'
\ || pprevline =~ '^\s*\<else\>\s*$'
let ind = ind - &shiftwidth
endif
" Align 'for' clauses nicely
if prevline =~ '^\s*\<for\> (.*;\s*$'
let ind = ind - &shiftwidth + 5
endif
" Subtract a 'shiftwidth' on '}' or html
let thisline = getline(v:lnum)
if thisline =~ '^\s*[})]'
\ || thisline =~ '^\s*</[a-zA-Z][^>]*>'
let ind = ind - &shiftwidth
endif
return ind
endfunction
But I have no clue, where the change was introduced... Tried to find it in the SVN history at https://codereview.scala-lang.org/fisheye/browse/scala-svn/scala-tool-support/trunk/src/vim/indent/scala.vim