What does ".=" in vim scripts mean?
I've often seen assignments to variables of the form "let s.='something'" Here's the specific piece of code in a vim script that I've been struggling to understand:
let s .= '%' . i . 'T'
let s .= (i == t ? '%1*' : '%2*')
let s .= ' '
let s .= i . ':'
let s .= winnr . '/' . tabpagewinnr(i,'$')
let s .= ' %*'
let s .= (i == t ? '%#TabLineSel#' : '%#TabLine#')
The code adds the tab number (i
) and viewport number (winnr
of tabpagewinnr(i,'$')
) to the tab name, so that it looks something like "1: 2/4 Buffer name". From 开发者_如何学Gothe looks of it, the .=
operation seems to be appending stuff to s
. But then, I don't understand what the first two lines do. Any help is appreciated.
vim's online help is your friend:
:h .=
:let {var} .= {expr1} Like ":let {var} = {var} . {expr1}".
:h expr-.
expr6 . expr6 .. String concatenation
:h expr1
(well - this is a little hard to find):
expr2 ? expr1 : expr1
The expression before the '?' is evaluated to a number. If it evaluates to TRUE, the result is the value of the expression between the '?' and ':', otherwise the result is the value of the expression after the ':'.
Example:
:echo lnum == 1 ? "top" : lnum
Since the first expression is an "expr2", it cannot contain another ?:. The
other two expressions can, thus allow for recursive use of ?:.
Example:
:echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
To keep this readable, using |line-continuation| is suggested:
:echo lnum == 1
:\ ? "top"
:\ : lnum == 1000
:\ ? "last"
:\ : lnum
You should always put a space before the ':', otherwise it can be mistaken for
use in a variable such as "a:1".
One at a time:
let s .= '%' . i . 'T'
Assuming i=9 and s="bleah", s will now be "bleah%9T"
let s .= (i == t ? '%1*' : '%2*')
This is the familiar ternary operator from C. If t==9, then s is now "bleah%9T%1*". If t is anything but 9, then s is now "bleah%9T%2*"
精彩评论