Last active
January 15, 2025 13:07
-
-
Save karolba/23892b23d701173c64bf7e6512949f9b to your computer and use it in GitHub Desktop.
my new vimrc in Vim9 script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vim9script | |
# vim: sts=4 sw=4 sts=4 et | |
&compatible = false # It's not 1980 anymore, don't care about vi-compatibility | |
syntax on # This should be set by default, but just in case | |
filetype plugin indent on # This should be set by default, but just in case | |
&autowrite = true # Write the contents of the file automatically before many actions | |
&autoread = true # Detect when other programs modify a file we're editing | |
&backspace = 'indent,eol,start' # Allow backspacing over everything in insert mode. | |
&backup = true # Enable backups | |
&backupdir = expand('~/.vim/backup//') # ... and put them in ~/.vim | |
©indent = true # Make autoindent smarter a bit | |
&display = 'truncate' # Show @@@ in the last line if it is truncated. | |
&encoding = 'utf8' # Always use utf8 internally (not 'fileencoding') | |
&formatoptions ..= 'j' # Delete comment character when joining commented lines | |
&hidden = true # Store buffers in RAM | |
&history = 200 # Keep 200 lines of command line history | |
&iskeyword ..= ",-" # For me this is more often true than not | |
&list = true # Show listchars: | |
&listchars = "nbsp:▄,tab:→ " # these | |
&mouse = 'a' # It's not 1995 anymore | |
&ruler = true # Show the cursor position all the time | |
&scrolloff = 5 # Show a few lines of context around the cursor. | |
&shortmess ..= 'Ac' # todo: why lol | |
&showcmd = true # Display incomplete commands | |
&smartcase = true | &ignorecase = true # Make searches smarter by default | |
&smoothscroll = true # Nicer looking scrolling | |
&swapfile = true # Enable swapfiles | |
&directory = $'.,{expand("~/.vim/swp//")}' # ... and put them in ~/.vim if they can't be put in . | |
&t_EI ..= "\033[2 q" # NORMAL mode (ELSE) -> solid block | |
&t_SI ..= "\033[6 q" # INSERT mode -> solid vertical bar | |
&t_SR ..= "\033[4 q" # REPLACE mode -> solid underscore | |
&timeout = false # Don't timeout stuff | |
&ttimeout = true # But do timeout escape | |
&ttimeoutlen = 100 # so wait up to 100ms after Esc for special key | |
&ttyfast = true # This config is typically not gonna be used over ssh | |
&undofile = true # Save undo history | |
&undodir = expand('~/.vim/undo//') # ... to ~/.vim | |
&updatetime = 300 # Make swap files if inactive | |
&wildmenu = true # Display completion matches in a status line | |
&wildoptions = 'pum' # in a popup! | |
#&t_TI = "" | &t_TE = "" # disable modifyOtherKeys support - seems to be buggy on ghostty [:(] | |
set isfname-== # Make gf and <C-x><C-f> more useful by ignoring common delimeters close to it | |
set isfname-=' # cont | |
set isfname-=" # cont | |
set isfname-=: # cont | |
set isfname-=, # cont | |
set nrformats-=octal # Do not recognize octal numbers for Ctrl-A and Ctrl-X | |
if has('reltime') | set incsearch | endif # Do incremental searching if it's possible to timeout. | |
if exists('+langremap') | set nolangremap | endif # Prevent that the langmap option applies to characters that result from a mapping. | |
def g:GetVisualSelection(): string | |
var [line_start, column_start] = getpos("'<")[1 : 2] | |
var [line_end, column_end] = getpos("'>")[1 : 2] | |
var lines = getline(line_start, line_end) | |
if len(lines) == 0 | return '' | endif | |
lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] | |
lines[0] = lines[0][column_start - 1 :] | |
return lines->join("\n") | |
enddef | |
# When editing a file, always jump to the last known cursor position. | |
autocmd BufReadPost * { | |
if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' | |
execute 'normal! g`"' | |
endif | |
} | |
# Convenient command to see unsaved changes | |
function g:DiffOrig() | |
vert new | set bt=nofile | r ++edit # | 0d_ | diffthis | wincmd p | diffthis | |
endfunction | |
command DiffOrig :call DiffOrig() | |
# Bracketed paste inside tmux | |
if exists('$TMUX') | |
&t_BE = "\033[?2004h" | |
&t_BD = "\033[?2004l" | |
&t_PS = "\033[200~" | |
&t_PE = "\033[201~" | |
endif | |
# --- :Commands --- | |
def Spaces(to: number): void | |
&l:ts = to | |
&l:sw = to | |
&l:sts = to | |
&l:et = true | |
:retab | |
enddef | |
command -nargs=1 Spaces :call Spaces(<f-args>) | |
def SpacesNoExpandTab(to: number): void | |
&l:ts = to | |
&l:sw = to | |
&l:sts = to | |
&l:et = false | |
enddef | |
command -nargs=1 SpacesNoExpandTab :call SpacesNoExpandTab(<f-args>) | |
def Tabs(to: number): void | |
&l:sw = to | |
&l:ts = to | |
&l:sts = to | |
&l:et = false | |
enddef | |
command -nargs=1 Tabs call s:Tabs(<f-args>) | |
# Anti-fat-finger commands | |
command Q :q | |
command W :w | |
command Wq :wq | |
command WQ :wq | |
command WQA :wqa | |
command WQa :wqa | |
command Wqa :wqa | |
# --- plugins --- | |
plug#begin() | |
# support opening files with `vim main.c:123:4` | |
plug#('https://github.com/wsdjeg/vim-fetch') | |
# git | |
plug#('https://github.com/tpope/vim-fugitive') | |
# unix helpers | |
# what I use from this: | |
# - typing shebangs redetects filetypes | |
# - typing shebangs and saving makes the file executable | |
# - :SudoEdit | |
plug#('https://github.com/tpope/vim-eunuch') | |
# Keybinds | |
# what I use: | |
# quickfix: [q, ]q | |
# todo: read through this plugin | |
plug#('https://github.com/tpope/vim-unimpaired') | |
# netrw improvements: | |
# what I use: | |
# - press `-` to go open netrw where the current file is | |
# - getting rid of the big banner | |
plug#('https://github.com/tpope/vim-vinegar') | |
# do my own vim-surround mappings (avoiding the `y` prefix) | |
g:surround_no_mappings = true | |
plug#('https://github.com/tpope/vim-surround') | |
nmap ds <Plug>Dsurround | |
nmap cs <Plug>Csurround | |
nmap cS <Plug>CSurround | |
nmap s <Plug>Ysurround | |
nmap S <Plug>YSurround | |
nmap ss <Plug>Yssurround | |
nmap Ss <Plug>YSsurround | |
nmap SS <Plug>YSsurround | |
xmap s <Plug>VSurround | |
xmap S <Plug>VgSurround | |
imap <C-S> <Plug>Isurround | |
var languages_with_lsp = [ | |
'python', | |
'python3' | |
] | |
plug#('https://github.com/prabirshrestha/vim-lsp', { for: languages_with_lsp }) | |
plug#('https://github.com/mattn/vim-lsp-settings', { for: languages_with_lsp }) | |
g:lsp_diagnostics_enabled = 0 # No annoying diagnostics when writing | |
g:lsp_document_highlight_enabled = 0 # No annoying highlighting the same variable (I can do with vim thank you) | |
def OnLspBufferEnabled(): void | |
&l:omnifunc = lsp#complete | |
&l:tagfunc = lsp#tagfunc | |
nmap <buffer> K <plug>(lsp-hover) | |
enddef | |
autocmd User lsp_buffer_enabled call OnLspBufferEnabled() | |
# filetype plugins: | |
plug#('https://github.com/fatih/vim-go', { for: ['go', 'gomod'] }) | |
plug#('https://github.com/zah/nim.vim', { for: ['nim'] }) | |
plug#('https://github.com/hashivim/vim-terraform', { for: ['terraform'] }) | |
# closing tags on "</" in html | |
plug#('https://github.com/docunext/closetag.vim', { for: ['html'] }) | |
plug#end() | |
# optional plugins built in to vim | |
packadd comment # gcc, gc{motion}, {Visual}gc | |
packadd editorconfig # load editorconfig files automatically | |
packadd editexisting # if a vim instance is already editing a file, focus it instead of opening it twice (TODO: doesn't work) | |
# --- filetype-specific stuff --- | |
autocmd FileType go { | |
nnoremap <buffer> <CR> <cmd>wa<bar>GoRun<CR> | |
nnoremap <buffer> <S-CR> <cmd>wa<bar>GoBuild<CR> | |
} | |
autocmd FileType erlang { | |
setlocal sw=4 sts=4 ts=4 et | |
#call LoadLsp() | |
} | |
# --- mappings --- | |
# CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo | |
inoremap <C-u> <C-g>u<C-u> | |
# Better <C-l> | |
nnoremap <silent> <C-l> <C-l><cmd>nohlsearch<bar>match<bar>diffupdate<CR> | |
# Command-line mode | |
cnoremap <C-a> <Home> | |
cnoremap <C-e> <End> | |
# --- modify <C-g> to my liking --- | |
# original <C-g> - remap to <C-g><C-g> | |
nnoremap <C-g><C-g> <C-g> | |
nnoremap <C-g>g 1<C-g> | |
nnoremap <C-g>G 2<C-g> | |
# this requires fugitive: | |
nnoremap <expr> <C-g>* $"<Cmd>Glgrep {expand('<cword>')}<CR>" | |
vnoremap <expr> <C-g>* $"<Cmd>Glgrep {GetVisualSelection()}<CR>" | |
nnoremap <C-g><C-n> <Cmd>lnext<CR> | |
nnoremap <C-g><C-p> <Cmd>lprevious<CR> | |
# tig! | |
nnoremap <silent> <C-g>t <Cmd>!tig<CR> | |
nnoremap <silent> <C-g><C-t> <Cmd>!tig<CR> | |
# Invoke omnifunc easier | |
inoremap <C-o> <C-x><C-o> | |
# make it possible to scroll the completion popup | |
inoremap <expr> <ScrollWheelUp> pumvisible() ? "<C-p>" : "<ScrollWheelUp>" | |
cnoremap <expr> <ScrollWheelUp> pumvisible() ? "<C-p>" : "<ScrollWheelUp>" | |
inoremap <expr> <ScrollWheelDown> pumvisible() ? "<C-n>" : "<ScrollWheelDown>" | |
cnoremap <expr> <ScrollWheelDown> pumvisible() ? "<C-n>" : "<ScrollWheelDown>" | |
# machine-specific vimrc config | |
var vimrc_local = expand('~/.vimrc.local') | |
if filereadable(vimrc_local) | |
:source `=vimrc_local` | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment