What's that Vim trick that blew your mind the first time you learned about it? A feature that has a big (or small) impact on your workflow, or just a command you use a lot. Anything goes! đ€Ż
I'll start with my go-to trick: I usually only notice a match should be replaced after searching for it with /
(/foo
). After learning that substitutions with empty search patterns (%:s//bar/
) replace the previously found matches, I've never had to re-type a pattern again.
Top comments (31)
In NeoVim (not really Vim but humor me :P), I can create panes with terminals in it. So I don't need Tmux anymore. Also, I can use Vim bindings for scrolling through the terminal panes and copy stuff. That feature made my workflow way more fluid.
Don't worry, Iâm a neovim user myself. â
Iâm actually the other way around in regards to neovimâs
:terminal
, though. I am using tmux, and prefer splitting the tmux window instead of starting a terminal session in Vim.I must say Iâve never taken the time to set
:terminal
up properly, as Iâm still getting a couple of âcommand not foundâ errors when starting it.Fun fact: Iâve actually tried using tmux splits instead of Vimâs built-in ones a while back, but that didnât really work because I didnât find a way to share sessions between Vim instances.
So, what does your workflow look like? Do you open up Vim and do everything from there, in one instance?
Yes, I do everything inside Nvim now. When I was using Vim+Tmux, I was using a plugin for moving between Vim and Tmux panes transparently. I also needed special config for getting access to the system's clipboard. That wasn't necessary when I got rid of Tmux. Are you using those plugins?
The only thing I had to do for getting my Nvim's terminal splits just the same as iTerm's was to source my
.bashrc
file inside.bash_profile
. I also added some useful mappings like:also removed the number lines and got terminal buffers to automatically enter into insert mode with
I'm attaching a screenshot of my current setup. Also here's my dot file repo if you want to take a look đ€ github.com/jesusabarca/.dotfiles
Current setup:
I use vim-tmux-navigator to switch between Vim and tmux splits, configure Vim to use the unnamed paste buffer, and use reattach-to-user-namespace to do the same in tmux (although that doesnât seem to be required anymore).
Iâll try the built-in terminal again sometime soon, though!
Terminal panes have been in Vim since 8.1 I think. This is still commonly touted as a reason to use neovim, but it's not really an issue nowadays.
Cool, I didn't know that. I went from Vim 7.x directly to Nvim.
I personally prefer just using iTerm panes.
Here are a number of tricks in core Vim I've found useful over the years:
CTRL-p
completion - it just completes identifiers in the current file (well, you can enable it to complete more things via thecomplete
option), but often that's good enough!CTRL-x
- I useCTRL-x CTRL-f
to complete filenames all the time, and I useCTRL-x CTRL-l
to complete lines way more often than I probably should =):normal
to run Vim commands across an entire file or a selection is really nice - it provides a nice alternative to macros (which are also quite handy):%!command
all the time for arbitrary text processing - especially:%!perl
.CTRL-r
, which inserts a register in insert mode:+
/*
- clipboard registers"
- last yanked text (useful if you want to type some text, paste what you yanked, and then type a little more).
- last inserted text (useful if you want to replace different motions with the same text)=
- expression register (useful for things like inserting the current directory withCTRL-r =getcwd()
)CTRL-w
- current word under the cursor (I use this with:Pydoc CTRL-w
to look up the documentation for the word under my cursor)/
- current search (I will often do a search, and then do/\C<C-r>/
to enable case sensitivity for the current search, or wrap the current search via/\C\<<C-r>\>
to search for only the target word and not words containing the search as a substring)CTRL-f
in the command line to open a command line editor, which allows you to fix previous command lines using Vim commandsg-
/g+
/:earlier
/:later
to traverse history by time, rather than undo ordervim filename +line_num
to seek toline_num
from the start, andvim filename +/pattern
to start searching forpattern
from the get-go>']
to indent the last paste (the[
and]
registers contain the start and end locations of the last changed or yanked text)And it's not technically a Vim trick, per se, but I like running my shell in Vi mode!
My favorite so far is setting macros in command-line mode.
This post basically blew my mind. I don't actually use macros that often and when I do I always get stage fright whenever I start recording. But as the article says, macros are just registers that you can set and edit.
If I wanted to convert this.
into this.
I would start typing a command.
Inside the quotes I'll write the keys that the macro would execute. The cool thing about this is that if you get something wrong you can undo, go back in the commands history, edit the macro and try again.
The final result would be this.
There is just no way I would get that right on the first try.
You'll have to be careful with special characters like "Escape". The way you get then in the command is by pressing
ctrl+v
first. For the escape character you would press ctrl+v first and then the escape key.Macros have been near the top of the list Iâve been planning to properly figure out for years now, I just never get around to it. I always have a hard time blindly recording macros with
q<letter><commands>q
.I love the idea of writing macros in command mode, and having them available in the command history. That article looks great, too. Great suggestion!
Brace yourself... you can still use your
q...q
command to record them as you go. Then, find a blank line somewhere and"qp
and boom - your macro is right there. Tweak it, then highlight it and"qy
and now@q
will replay your new one.Blew my mind when I figured out there was nothing special about
qq
.. it's just putting things into a buffer to replay.Using double quotes you can use a better notation to indicate keystrokes, something like:
In order to test the above macro paste this on the firest line and run
10@a
The operations you can do with the built-in file explorer (aka
netrw
):d
%
D
X
(upper-case):help netrw-quickhelp
There are a couple of tricks in Vim that I love.
about your 1. there's another more awesome trick:
when renaming variables, you can search for it using
/
or*
(reverse with?
or#
) and usecgn
(reversecgN
) to modify it, using.
to change the next match.The
-gn
movement is working on the next match. So you can spare one key (n
orN
) when bulk renaming variables.tl;dr : Instead of
you can do:
cheers đ»
NeoVim has a "preview" functionality when you're doing substitutions.
First:
set inccommand=split
Then, enjoy incremental highlighting of the text that will be impacted by any substitutions until you hit [ENTER]:
:%s/foo/bar/g
I have many tricks on at my "init.vim"
I also like "Ctrl-6" to jump to alternate buffer ":%y+" to copy the entire buffer to the clipboard. "gx" to open links. "gv" to reselect. "gi" to start insert at the last inserting point.
On my ~/.zshrc and ~/.bashrc I have
Currently I have an autoloaded zsh function that goes like this:
Say you've changed a function signature, like:
and you want to update all the references across all files of a project, you can do this with quickfix +
:cdo
:src/
directory.This will load all results into a quickfix list.
You can see this list by typing:
This is a simple subsitituion which replaces all references to
add3(
toadd(3,
and saves the changes.For more information about quickfix lists and what you can do with them, check out the vim docs and feel free to search "vim quickfix" online (lots of good blog posts & youtube videos dive into this feature of vim).
Column selection and insertion in multiple rows. Ctrl + V to select vertically and then I to insert in multiple rows.
I set up a shortcut to print in my .vimrc.
I run this command to see available printers.
I set a default printer.
In my .vimrc I have this line to print the current file.