This chapter compiles many of the best tips and tricks that the Neovim community has developed over the years to turbo-charge your Neovim workflow.
Filed In:misc
Sometimes it can be handy to get a quick diff of the changes that have been made to the current buffer since the previous save. This can be achieved with a simple command-line mode command. To demonstrate, let's start with the following buffer: Initial Conditions a = [ "one", "two", "three", "five", ] COMMAND Top 1:1 :%s/two/changed Now lets edit the buffer. To keep it simple we will make a single edit, changing "two" to "changed": Edit the Buffer a = [ "one", "changed", "three", "five", ] NORMAL 43% 3:5 Now, we can generate a simple diff with the command: :write !diff % - Let's review what this does. To start, this uses the :write command with the following call signature: :w[rite] !{cmd} Which passes the entire contents of the current buffer to the specified command via stdin . In this example we will use the diff shell command, which has the call signature: diff file1 file2 where our command defines file1 and file2 as: file1 = %, which contains the filename of the current file, and file2 = -, which tells diff to read stdin So, putting this all together, we pass the current buffer contents to the diff command, and generate the diff between the saved version of the current file (%) and the current buffer contents. Finally, the diff is passed back to Neovim: Generate the Diff a = [ "one", "changed", "three", "five", ] NORMAL 43% 3:5 :write !diff % - 3c3 < "two", --- > "changed", shell returned 1 The output window shows that the line containing "two" has been changed to "changed". diff also returns a status code if 1, meaning that the two files were different. This is definitely not the prettiest diff in the world, but is useful when a quick diff is required. Take a look at: diff --help to see what options are available to suit the output to your needs. For example, by leveraging diff's options one can format the same output as a side-by-side diff: :write !diff -yt -W 60 % - Alternate Output a = [ "one", "changed", "three", "five", ] NORMAL 43% 3:5 :write !diff -yt -W 60 % - a = [ a = [ "one", "one", "two", | "changed", "three", "three", "five", "five", ] ] shell returned 1
Filed In:misc
When editing a document one often needs to yank the entire buffer content. As with most operations, Neovim provides several different ways to accomplish this task. For each of the examples below, we will start with the following buffer content and cursor position: Initial Conditions red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f NORMAL 57% 4:1 Common Method Perhaps the most common method to achieve this task is the following sequence: gg to move the cursor to the top of the buffer. v to enter visual mode . G to move the cursor to the bottom of the buffer. y to yank the selected content. Common MethodggVGy red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f NORMAL Top 1:1 6 lines yanked Let's check the unnamed register to confirm that the full buffer content was yanked: Common Method red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f COMMAND Top 1:1 :reg " :reg " Type Name Content l "" red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f This method works well, and is probably so common because it is consistent with yanking smaller portions of the buffer. Besides requiring the most keystrokes, this method also has the disadvantage of moving the cursor to the top of the screen. Shorter Method A slightly shorter method leverages motions a bit more directly to save a keystroke: gg to move the cursor to the top of the buffer. y to yank the selected content. G to move the cursor to the bottom of the buffer. Shorter MethodggyG red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f NORMAL Top 1:1 6 lines yanked Let's check the unnamed register to confirm it worked: Shorter Method red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f NORMAL Top 1:1 :reg " Type Name Content l "" red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f This method is slightly better than than the previous method, but also moves the cursor to the top of the screen. Shortest Method The third, and shortest, method of yanking all buffer content leverages the %range to yank the entire buffer with only 3 keystrokes: Upon hitting <CR> the entire buffer is yanked: Shortest Method red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f COMMAND 57% 4:1 :%y Not only is this the shortest method, it has the additional advantage of keeping the cursor in the same position. Shortest Method red,#f00 green,#0f0 blue,#00f yellow,#ff0 cyan,#0ff magenta,#f0f NORMAL 57% 4:1 :reg " Type Name Content l "" red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f