Welcome to tui.ninja!


expand_more
It is a common task to search and replace all occurrences of a word within the current buffer. For example, given the following buffer: Initial Conditions B eautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:1 Suppose one wants to change all occurrences of is better to is way better . The first step in the example if to move the cursor to the word we want to replace: w B eautiful i s better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:11 then press & to select all occurrences of the word under the cursor. In addition to selecting all occurrences of is , the cursor moves to the next occurrence. * Beautiful i s better than ugly. Explicit i s better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL 25% 2:10 The general template for searching and replacing is: :[range]s/{pattern}/{replacement}/[flags] where Template Value Description : Puts Vim into Command Mode [range] % Applies this command to the entire file s/ Indicates that this is a search and replace operation {pattern}/ Not needed, since the words to replace are already selected. {replacement}/ is way Specifies the text to replace the search pattern with. [flags] No flags are specified. Putting this all together and executing the command: Beautiful is way better than ugly. Explicit i s way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. S parse is way better than dense. Readability counts. NORMAL 75% 6:1 6 substitutions on 6 lines As expected, each occurrence of is has been replaced with is way .
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 y ellow,#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 Method gg V G y r ed,#f00 green,#0f0 blue,#00f y ellow,#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 r ed,#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 Method gg y G r ed,#f00 green,#0f0 blue,#00f y ellow,#ff0 cyan,#0ff magenta,#f0f NORMAL Top 1:1 6 lines yanked Let's check the unnamed register to confirm it worked: Shorter Method r ed,#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 y ellow,#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 y ellow,#ff0 cyan,#0ff magenta,#f0f NORMAL 57% 4:1 :reg " Type Name Content l  ""   red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f