Mutation rates may be influenced by the environment. Here, we demonstrate that insertion sequence IS5 in Escherichia coli inserts into the upstream region of the flhDC operon in a manner that depends on whether the environment permits motility; this operon encodes the master regulator of cell motility, FlhDC, and the IS5 insertion increases motility. IS5 inserts upstream of flhD þ when cells are grown on soft-agar plates that permit swimming motility, but does not insert upstream of this locus on hard-agar plates that do not permit swimming motility or in planktonic cultures. Furthermore, there was only one IS5 insertion event on soft-agar plates, indicating insertion of IS5 into flhDC is not due to general elevated IS5 transposition throughout the whole genome. We also show that the highly motile cells with IS5 upstream of flhD þ have greater biofilm formation, although there is a growth cost due to the energetic burden of the enhanced motility as these highly motile cells have a lower yield in rich medium and reduced growth rate. Functional flagella are required for IS5 insertion upstream of flhD þ as there was no IS5 insertion upstream of flhD þ for flhD, flgK and motA mutants, and the mutation is stable. Additionally, the IS5 mutation occurs during biofilm formation, which creates genetic and phenotypic diversity. Hence, the cells appear to 'sense' whether motility is feasible before a sub-population undergoes a mutation to become hypermotile; this sensing appears related to the master transcription regulator, FlhDC.