title: Various Vim Tips tags: vim tips # Nir Lichtman youtube video tips ## Why vim See [this video](https://www.youtube.com/watch?v=UTtitAidw2k) on why Nir prefers vim to IDE's * more efficient in what you can do from the keyboard * autocomplete can use a language server * scrolling to line: `%` takes you to end of function; * `0`,`^`,`$` to go to start, first char, end of line; * `/` and `?`: search * 'f' and 'F': find in line ## 1 source: [this youtube video](https://www.youtube.com/watch?v=8vJKGRkiwnU) ```csv C-n,complete word C-x l,complete line ``` In a terminal ```csv sep=| C-w|to allow you to use a vim command, e.g. C-w :... ``` ## 2 This [youtube video](https://www.youtube.com/watch?v=0LM3Iw6dd-k) And [this youtube video](https://www.youtube.com/watch?v=jj7MZsN_g0w) ```csv sep=| gj,gk|move up/down within wrapped lines ``` ## copen ``` :grep hello *.txt :copen ``` ## 3 From [this youtube video](https://www.youtube.com/watch?v=qUZvdo_nAxM) ## 4 See [this youtube video](https://www.youtube.com/feed/history) * `:set ignorecase` to ignore case when searching * `:set smartcase` case sensitive if uppercase letters present (see `:help smartcase`) ## 5 See [this youtube video](https://www.youtube.com/watch?v=1028mlsQTJM) ``` $ ctags -R src :tag *argedupe # completion C-] ``` ## 6 [This vim doesn't need tabs video](https://www.youtube.com/watch?v=Fgb-m4q4v1Y) You can use splits. (I use both tabs and splits.) * Close a window with `C-w q` to close the window but not the buffer whereas `:q` closes window and closes the buffer if it is the last window with that buffer. * `:bufdo g/function/d` does this command on all open buffers ## 7 From [here](https://www.youtube.com/watch?v=cjNtDVmzr5A). How to delete without copying: * `:reg` shows registers * vim copies to default reg automatically when deleting * There is a black hole register: `"_d` to delete without copying * named registers: `"ay`, or `"qd`. # Opening files ```csv gf, open file under cursor C-w f,open file under cursor in new window ``` # Basic Stuff ## Regex Search Replace 1. Append to line: `:s/$/hello`; prepend to line: `:s/^/hello`; 1. Prepend to first non-whitespace *without* lookbehind: `s/\(\S\)/hello\1`; prepend to first non-whitespace char using lookahead: `:s/\ze\S/hello` (see [here for info on lookahead/lookbehind](https://vim.fandom.com/wiki/Regex_lookahead_and_lookbehind); using lookbehind to append append to last non-whitespace `:s/\s*\ze$/x` (note that `$` goes after the lookahead assertion) 1. Append to `hello` using lookbehind: `:s/hello\zs/ world` ## File Tree In neovim, `C-n` opens the file tree. (I'm not sure whether that is standard or my default config.) Selecting a file with enter sends it to one of the open windows: if one window it will use that, if more than one, it enumerates them `a,b,c` and you press a letter for the window you want. Use a nerdfont if you want file and folder icons to work properly. I use Hack Nerdfont in both Konsole and Windows Terminal. # Various ## Paste setting into buffer ``` :call setreg("+", getbufvar("%", "&guifont")) ``` and then `"+p` to paste. # Command Line History ``` :q -- open command line history C-c -- copy command to command line (C-c again to exit) C-c C-c -- exit command line history ``` # Accessing buffers from command line # Copying matched lines to a position ``` :g/pattern/t10 ``` will copy all matching lines to position starting at line 10