Diagonal lemma in Godel Incompleteness Theorem In the proof, we start with a numbering of all formulas with one free variable $v$.
The formula $sub(x,x,y)$ used in the diagonal lemma says:
$y$ is the godel number of the formula obtained when the free variable $v$ in the formula whose number is $x$ is replaced by the numeral for $x$.
$sub(x,x,y)$ is a formula with the variable $x$ free and if $y$ is within the scope of a quantifier, it is the only variable free.
I am not clear about the following questions.
If $x$ is a free variable, what is the numeral for $x$? Do we take it as $S(S(S...S(0)...))$ (where the $S$'s are repeated $x$ times)?
If $y$ is the godel number of a formula ( formula $\phi(x)$ obtained by replacing the variable $v$ by the numeral for $x$ in the formula whose number is $x$) , then is $y$ a number or a function of $x$?
 A: Referring H.Enderton, A Mathematical Introduction to Logic (2nd ed - 2001), we must start with the general definition of the function $Sb(v_1,v_2,v_3)$ [pag.228].
The definition is :

let $T$ a system for arithmetic [e.g.first-order Peano Arithmetic or Robinson Arithmetic : they must contain the symbols $0$ (individual constant denoting the number zero) and $S$ (1-ary functional constant denoting the successor function)];
let $n$ the Godel-number of a (term or a) formula $\alpha$;
let $m$ the G-number of a variable $x_i$;
let $k$ the G-number of a term $t$;
then $Sb(n,m,k)$ is the G-number of the resulting expression obtained replacing with the term $t$ all the free occurences of $x_i$ into $\alpha$ (i.e.$\alpha(t/x_i)$).

Question 1) : If x is a free variable, what is the numeral for x?
You must start with the coding of all symbols of the language; then you encode all terms, starting with xariables : for example, the G-number of $x_i$ will be : $g(x_i) = 9+2i$.
Remember ! in the formal treatment the variables are ordered; you are using $x$, $y$, $z$ as meta-language variables standing for the variables of the object-language, that are : $x_1$, $x_2$, $x_3$, ...
Applying the definition, we can compute the G-number for $x_1$; it is $11$.
Same for the terms ; terms are expressions like : $S(S(S(0))$ or $S(x+S(0))$, and following the "build-up" of the term the encoding rules will produce univocally a G-number. Numerals are terms : 11 is a term (it is $S(S...S(0)...)$ i.e. $S$ 11-times $0$) and it is the name in formal arithmetic of the number $11$.
So , in the encoding of a (formal) expression, it is perfectly known which variable you are replacing with a (perfectly known) term.
Question 2) : If $y$ is the G-number of a formula, then is $y$ a number or a function of $v$ (where $v$ is the G-number of a variable $x_i$) ?
$v$ is a number; when you replace the (free occurrences of the) variable $x_i$ into formula $\alpha$ with a term $t$ you get a new expression : $\alpha(t/x_i)$. You must replace $x_i$ in $\alpha$ with the numeral "naming" the G-number of $t$, and you will get a new formula.
Applying again the encoding rules to the resulting expression, you will compute a new G-number : the value of $Sb(y,v,u)$.
Another basic concept is "representability" (seee pag.205-on) :

The formula $\rho$ (with one free variable) represents in T a 1-ary relation $R$ on $\mathbb N$ iff for every $a$ in $\mathbb N$ :
$a \in R \Rightarrow \vdash _T \rho(S^a(0))$, i.e. the (closed) formula obtained from $\rho(x)$ substituing in place of $x$ the numeral "naming" the number $a$ is provable in $T$, and
$a \notin R \Rightarrow \vdash_T \lnot \rho(S^a(0))$.

A very simple example of relation representable in $T$ is the equality relation in $\mathbb N$ (pag.206) : use the formula $x_1 = x_2$.
You can extend this notion also to functions (see pag.212), saying that a formula $\phi$ (with $m+1$ free variables) functionally represent a function $f$ (with $m$ arguments). The basic result is (pag.214-on) that a lot of useful functions and relations are representable in $T$.
All this result converges in the arithmetization of syntax (pag.224-on), where you can prove that the functions and relations involved with G-numbers are representable in the formal system $T$.
At page.228, item 5, you have that $Sb(y,u,v)$ is representable.
The "trick" of Fixed-Point Lemma (pag.235) is to exploit the above facts.
$\theta$ is a formula of the system $T$ : it does not represents $Sb$, but it is very "tightly" linked to $Sb$.

The formula $\theta(v_1,v_2,v_3)$ functionally represents the function that, given as arguments the G-number of a formula (with only the free variable $x$) $\alpha$ and the number $n$ (they are both numbers !), returns as ouput the G-number of $\alpha(x/S^n(0))$ (i.e. the closed formula obtaind replacing $x$ with the numeral "naming" $n$).

Call it $Sb_1$ : it is like $Sb$ but without the reference to the free variable (there is only one). The formula $\theta$ is defined as :

$\theta(x,y,z)$ is true iff $Sb_1(x,y)=z$.

In the prof of the Lemma we will use the formula $\theta$ that "speaks of" formulae of the system itself through their G-numbers.
Added Dec,23
Question 3) : The formula with godel number $v_1$ is a formula with 2 free variables, namely $x$ and $v_1$? And once we substitute (the numeral for) $v_1$ for $x$, we are left with a formula with $v_1$ as free variable and take the godel number of this formula? Or we first substitute a number for $v_1$ in $\theta$ , find the corresponding formula and substitute?
First, in order to avoid confusion, we will use only $v_1, v_2, ...$ as free variables in formulae. We will use $n, m, k, ...$ as numbers and $S^n(0), ...$ as numerals (i.e.closed term "naming" numbers).
At pag.235, you have the formula (1) : $\forall v_3 [\theta (v_1,v_1,v_3) \rightarrow \beta(v_3)]$.
This is a formula with only one free variable : $v_1$. It is a formula of the language; so you can compute its own G-number : say $q$ (if you review the encoding rules, you can check that G-numbers for variables cannot be equal to G-numbers for formulae).
Then put the numeral $S^q(0)$ in place of the free variable $v_1$ into (1) and you will get a closed formula : $\sigma$.
This new formula has a new G-number, different from $q$.
