Search code examples
vimkeymapping

Vim function keys have suddenly changed their mappings


As of today my function keys have all changed their behaviour.

F1 for example no longer opens help but is equivalent to typing OP in normal mode. That is it opens a new line above the current one, enters insert mode and types 'P'. As you can see in my .vimrc below the only keymapping I have for a function key is map <f2> :set rnu!<cr> which has worked just fine up until today.

F2 now types <F2>, and enters replace mode, and pressing <esc> types another <F2> before quitting replace mode.

F3 now opens a new line above, enters insert mode and types 'P'

F5 changes the case of 5 consecutive characters at a time

F6 to F10 have a similar function to F5 but changing case of different numbers of characters.

edit: typing <c-v><f1> <c-v><f2>... etc. in insert mode shows that they are now mapped to ^[OP ^[OR ^[OQ etc.. but I still don't know how or why

The function keys work as expected in other programs (gedit, firefox etc.) and opening vim in a different terminal doesn't make a difference either, which leads me to believe it's purely a vim issue.

This is quite frustrating as I have no idea where to start with fixing this, and I can't find any similar problems online so far.

Restarting my computer and vim also has no effect..

"##############################################################################
"##                         d8b                                              ##
"##                         Y8P                                              ##
"##                                                                          ##
"##                888  888 888 88888b.d88b.  888d888 .d8888b                ##
"##                888  888 888 888  888  88b 888P"  d88P"                   ##
"##                Y88  88P 888 888  888  888 888    888                     ##
"##             d8b Y8bd8P  888 888  888  888 888    Y88b.                   ##
"##             Y8P  Y88P   888 888  888  888 888     "Y8888P                ##
"##                                                                          ##
"##############################################################################
"_____________________________________________________________________________

"#############################
"# General Settings
"#############################

"Disable Vi compatibility options
set nocompatible

"Set filetype detection on
filetype on

"Enable filetype plugins
filetype plugin on

"Show line numbers
set nu

"Show relative line numbers
set rnu

"Set tab character to → (unicode u2192)
"Set end of line character to ↲ (unicode u21b2)
set listchars=tab:→\ ,eol:↲

"Set tabstop, shift width, softtabstop
set ts=4 sw=4 sts=4

"Round indent (with > and < commands) to multiples of shiftwidth
set shiftround

"New split windows open on the right (vertical) and on the bottom (horizontal)
set splitbelow
set splitright

"Don't use swapfiles
set noswapfile

"No line wrapping
set nowrap

"Expand tab to spaces
set expandtab

"Set scroll offset
set scrolloff=8

"No search hilighting
set nohlsearch

"Display line and column number in the status bar
set ruler

"Fix fuzzy find:
set nocompatible      "Limit search to working directory
set path+=**          "Search all subdirectories recursively
set wildmenu          "Shows multiple search results on one line

"Remove characters from window separators
set fillchars+=vert:\ 

"##############################
"# Key Mappngs
"##############################

"Set <leader> to spacebar
let mapleader=" "

"Easy escape
map <c-[> <esc>

"Easy quit/write
map <leader>q :q
map <leader>w :w

"Window split navigation
map <leader>h :wincmd h<cr>
map <leader>j :wincmd j<cr>
map <leader>k :wincmd k<cr>
map <leader>l :wincmd l<cr>
map <leader>H :wincmd H<cr>
map <leader>J :wincmd J<cr>
map <leader>K :wincmd K<cr>
map <leader>L :wincmd L<cr>
map <leader>T :wincmd T<cr>
map <leader>< :16wincmd <<cr>
map <leader>> :16wincmd ><cr>
map <leader>- :16wincmd -<cr>
map <leader>+ :16wincmd +<cr>
map <leader>= :wincmd =<cr>

"Argument navigation
map <leader>] :n<cr>
map <leader>[ :N<cr>
map <leader>a :arg<cr>
map <leader>0 :fir<cr>
map <leader>9 :las<cr>

"Tab navigation
map <leader>t :tabnew

"Set relative line numbers in netrw
let g:netrw_bufsettings = 'noma nomod rnu nobl nowrap ro'

"Window split in netrw open at 80% of window size
let g:netrw_winsize=80

"F2 toggles relative line numbers
map <f2> :set rnu!<cr>

"##############################
"# Plugins
"##############################

"vim-plug setup
call plug#begin('~/.vim/plugged')

    "Plugins
    Plug 'ap/vim-css-color'
    Plug 'joshdick/onedark.vim'
    Plug 'haishanh/night-owl.vim'
    Plug 'tomasiser/vim-code-dark'
    Plug 'itchyny/lightline.vim'
    Plug 'itchyny/vim-gitbranch'
    Plug 'jasonccox/vim-wayland-clipboard'

call plug#end()


"#############################
"# lightline settings
"#############################

"Set lightline colour theme and add git branch name to status bar
let g:lightline = {
      \ 'colorscheme': 'onedark',
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'gitbranch', 'readonly', 'filename', 'modified', 'buffernumber' ] ],
      \   'right': [ [ 'lineinfo' ],
      \              [ 'percent' ],
      \              [ 'fileformat', 'fileencoding', 'filetype', 'argumentnumber' ] ],
      \ },
      \ 'component_function': {
      \   'gitbranch': 'gitbranch#name'
      \ },
      \ 'component' : {
      \ 'buffernumber': '%n',
      \ 'argumentnumber': '%a'
      \ },
      \ }

"Fix for lightline not appearing for single windows
set laststatus=2

"Don't display mode at the bottom of the screen (not necessary due to lightline)
set noshowmode

"#############################
"# Colour settings
"#############################
 
"Use terminal colours
if (has ("termguicolors"))
    set termguicolors
endif

"Fix for termguicolors making everything black and white
let &t_8f = "\<Esc>[38:2:%lu:%lu:%lum"
let &t_8b = "\<Esc>[48:2:%lu:%lu:%lum"

"Set syntax hilighting on
syntax on

"Set colour theme
colorscheme codedark

"Set transparent background (keep below colorscheme command)
hi Normal guibg=NONE ctermbg=NONE
hi EndOfBuffer guibg=NONE ctermbg=NONE
hi LineNr guibg=NONE ctermbg=NONE 
hi Comment guibg=NONE ctermbg=NONE 

"Set line numbers and comments colour to grey (keep below colorscheme command)
hi LineNr guifg=#999999 ctermfg=Grey
hi Comment guifg=#999999 ctermfg=Grey

Solution

  • Behviour was due to <esc> key mapping in my .vimrc , which it seems is not advisable. The mapping itself map <c-[> <esc> was also redundant as <c-[> works as <esc> by default, at least on US/GB keyboards.

    Opening vim without plugins or .vimrc by running vim -u NONE helped identify the issue.