I use (Vim IMproved - not vi) vim all the time. I find it interesting to see what features other people tend to use in vim, there seems to be some minor differences in their workflows, but it's not clear who "wins" the productivity battle.
I only use a very small number of it's features:
* `:tabe` Create a new tab
* `:e .` Start browsing files in that tab
* `:w` Write the file
* `:q` Quit the file (`:q!`, seriously, please do quit the file)
* CTRIL+SHIFT+UP/DOWN To switch tab (doesn't reliably work on every machine)
* `/WORD` To search for something
* `:NUM` To go to a specific line
* `i` To insert
* SHIFT+V to select lines
* `d` To delete lines
* `x` To extract lines
* `y` To "yank" (copy) lines
* `p` To paste lines
>Here are a few things I wish I could do better:
I don't have a great memory, so memorizing more than what I use every day is a little too much. One thing that I'm working on remembering is the ability to replace strings (although I don't really like regex).
>Would I learn Vim again?
I'm yet to experience anything compelling enough to stray me away. The advanced features are there if I ever need them, but for the most part I am free to have a lightweight editor.
That's a solid set and will take you far. I did eventually start peppering in a few pattern matching command things that don't require much regex.
* `:g` Global command, executes an Ex command on every line that matches via the pattern `:[range]g/pattern/cmd`
One common use I have for using `:g` is to delete all lines that match or do not match a pattern, like when I've removed an attribute from some data structure and I want to quickly fix all my mock objects to remove the attribute:
`:g/pattern/d`
To delete patterns that do not match, useful for debugging:
`:g!/pattern/d`
* `:norm` this thing motivated me to use more of the movement commands like `A`, `o`, `D`, `f`, `cw`, and `ci`. Used `:[range]norm cmd`, it acts like each key in the command is run on every line in range. I usually use it with a selection, for example to add a comma to the end of every line in range with `A`, or insert at the end of line:
`:norm A,`
Or to delete everything after the first period in a range using `f` to move to character, `l` to move after that character, and `D` to delete everything after cursor:
`:norm f.lD`
The best part about learning these two is that they can be used together. If you want to insert `fmt.Println(err)` after every line (using `o`), but only on the lines matching `err != nil`:
`:g/err != nil/norm ofmt.Println(err)`
Or two write `time.Now(),` only on lines which have `"created_at":` and only after the colon:
`:g/created_at/norm f:lDA time.now(),`
And if you've got hashes with string keys and string values i.e. `"key": "value"`, you can upper case the string values matching `"pattern"` with:
`g/pattern/norm 3f"lvi"~`
On lines it matches it finds the third double quote with `3f"` then moves a character over with `l`, visual selects inside the double quote with `vi"` and uppercases the selection with `~`.
One regex trick that I end up using with `:g` pattern matching is `^\s*`. When you put it before any pattern then the pattern matches only if it has some amount of whitespace preceding it, and that whitespace extends from the beginning of the line to the start of the pattern. It is good for targeting code indented with whitespace.
Final thought, when you're typing your commands in norm and you've entered insert mode, you can issue the escape command with <Ctrl-v><Esc> which when done right should look like `^[` but will not actually be those characters.
>To delete patterns that do not match, useful for debugging: `:g!/pattern/d`
This is something I would normally use something like grep to achieve, nice to know it exists in vim as well.
>* `:norm` this thing motivated me to use more of the movement commands like `A`, `o`, `D`, `f`, `cw`, and `ci`. Used `:[range]norm cmd`, it acts like each key in the command is run on every line in range. I usually use it with a selection, for example to add a comma to the end of every line in range with `A`, or insert at the end of line: `:norm A,`
In this specific case, I would tend to (maybe in gvim) run a replace on `\n` -> `,\n` to achieve this. That's kind of what I'm looking for in terms of string replacement.
The rest is certainly interesting, but for my work flow I like to generally keep things as simple as possible (bad memory). I've never found myself thinking "I wish there were more movement commands" for example.
If you are used to sed it should be easy, as it is pretty much the same. I keep using this pattern over and over again:
:%s/search/replace/gc
The '%' is some sort of reference to the file you are currently editing and therefore you are doing a replace on the whole file. You can see the difference in combination with some visual selection. When you select some text and press ':' it opens as ":'<,'>"
:% # replace in the whole file
:'<,'> # replace in the current selection
The 'gc' flags at the end are:
g=global to replace multiple times per line
c=confirm to let vim ask you about every replacement
Sometimes its just easier to let vim ask you a few times than to spend 5 minutes to figure out the 100% correct regex.
Thank you for explaining this! I will have to practice, replace is something I don't do very often enough to remember but do it often enough that it annoys me each time.
I need to use Emacs rather than vim for some things (Proof General, mostly), and I was surprised how well evil-mode works. It's only undo that I don't get. From vim I'm used to be able to undo as many editing steps as I want. In evil-mode, I find that if I type "u" too many times, it runs out of undos to do and starts undoing the undos (i.e., it starts redoing things I want to go away).
Also, search uses different word separators from vim: In vim "foo_bar" is a single word that will be skipped by pressing "w". But in evil-mode the "_" is a word separator, so the above is three words. That breaks my flow every time.
Some of this might not be due to vanilla evil but due to the fact that I'm using it through Spacemacs.
This is my two cents. If by any chance anyone would know off the top of their head how to fix these, I'd be greatful.
I'm guessing you mean "dd" and "yy" to delete/yank lines. "x" deletes a single character, what you mean extract a line?
If you learn "w", which just means advance by a word, you can combine them with your existing knowledge for much better use. So "dw" becomes delete a word, or "d3w" means delete next 3 words. "v" is also useful in conjunction with "w" / "b".
SHIFT+V + 'd' is a longer way of doing 'dd'. SHIFT+V + 'y' is a longer way of doing 'yy'.
You can also delete and yank multiple lines by using 'd2d' (delete two lines) or similar. You can also delete lines upwards by using a movement command like 'dk' (or 'd2k' if you want to delete the two lines above).
I'm also a big fan of `ci[(|'|"]` to change all the text between a pair of delimiters. I can type `ci"` while my cursor's at the start of the line, and it'll jump to the first set of double quotes, remove the text that's currently between them, and enter edit mode.
All of my machines have the vim-tiny package to stay as close to stock vi as possible. I open and close the editor dozens of times a day to make simple changes and I don't tolerate any lag.
I can see that vim would be great for long-form programming but plain old vi was just fine for most admin tasks.
Use Plug to load the plugins that you only need for a specific file. For example, this only load this plugins if I'm working wiht LESS/SCSS/CSS or Vue files :
vim-tiny differs from regular vim in how much stuff is compiled into it. Regular vim is actually quite hefty since it includes e.g. scripting bindings to Python, Perl, Lua, etc.; or support for X11 (which is useful even in a terminal for working with the X clipboard), and so on. If you have both vim and vim-tiny installed, you can use the command ":version" to compare which features have been compiled in.
I only use a very small number of it's features:
* `:tabe` Create a new tab
* `:e .` Start browsing files in that tab
* `:w` Write the file
* `:q` Quit the file (`:q!`, seriously, please do quit the file)
* CTRIL+SHIFT+UP/DOWN To switch tab (doesn't reliably work on every machine)
* `/WORD` To search for something
* `:NUM` To go to a specific line
* `i` To insert
* SHIFT+V to select lines
* `d` To delete lines
* `x` To extract lines
* `y` To "yank" (copy) lines
* `p` To paste lines
>Here are a few things I wish I could do better:
I don't have a great memory, so memorizing more than what I use every day is a little too much. One thing that I'm working on remembering is the ability to replace strings (although I don't really like regex).
>Would I learn Vim again?
I'm yet to experience anything compelling enough to stray me away. The advanced features are there if I ever need them, but for the most part I am free to have a lightweight editor.