Today's Featured Neovim Tips & Tricks:expand_moreFormatting an Entire BufferformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 The first step is to execute gg , which moves the cursor to the top of the buffer: Move to Top of Buffer g g < div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL Top 1:1 Next, we execute = , which tells Vim to format the lines traversed by the associated motion. We follow this with G , which indicates that we want to apply formatting from the current line to the bottom of the file: Format the Entire Buffer = G < div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL Top 1:1 5 lines indented The end result shows that the combination of gg and G includes all lines in the file. Formatting a SelectionformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Enter visual mode : Enter Visual Mode <Esc> <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Move down another row: Create the Selection v <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 50% 3:1 and finally format the selected lines: Format Selection j <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 67% 4:1 Note that Vim formatted only the lines contained within the selection (including lines containing the cursors), and didn't touch the other lines. Replace Word Under CursoreditingIt 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 . Search and Replace in Multiple FileseditingworkflowWe learned how to search and replace within a single file, this tip discussed how to extend that capability to multiple files. As a first step, let's define the pattern and replacements text we want to execute on a single file: Initial Conditions a = [ "one", "two", "three", "five", ] COMMAND Top 1:1 :%s/five/four/n As a simple example, let's change all occurrences of five to four . The command to do this is: :%s/five/four Let's test it on one file to be sure it works. We will add the n flag so that we can see the results but not execute the changes themselves: Test the Pattern a = [ "one", "two", "three", " five ", ] NORMAL Top 1:1 1 match on 1 line OK, that looks correct. Now that we have built the command we want to run, the next step is to define the list of files over which we will execute this command. This is done using the :args command. Suppose we want to execute this command over all python files in the current working directory. We can populate those paths in the arglist using: :args ./*.py we can verify the paths that we inserted into the arglist using: :args Now that the arglist has been populated, we can execute our command on each of the files in the list by calling :argdo and passing our search and replace command from above as an argument: :argdo %s/five/four Execute the Command a = [ "one", "two", "three", " four", ] NORMAL 71% 5:5
Formatting an Entire BufferformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 The first step is to execute gg , which moves the cursor to the top of the buffer: Move to Top of Buffer g g < div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL Top 1:1 Next, we execute = , which tells Vim to format the lines traversed by the associated motion. We follow this with G , which indicates that we want to apply formatting from the current line to the bottom of the file: Format the Entire Buffer = G < div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL Top 1:1 5 lines indented The end result shows that the combination of gg and G includes all lines in the file.
Formatting a SelectionformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Enter visual mode : Enter Visual Mode <Esc> <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Move down another row: Create the Selection v <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 50% 3:1 and finally format the selected lines: Format Selection j <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 67% 4:1 Note that Vim formatted only the lines contained within the selection (including lines containing the cursors), and didn't touch the other lines.
Replace Word Under CursoreditingIt 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 .
Search and Replace in Multiple FileseditingworkflowWe learned how to search and replace within a single file, this tip discussed how to extend that capability to multiple files. As a first step, let's define the pattern and replacements text we want to execute on a single file: Initial Conditions a = [ "one", "two", "three", "five", ] COMMAND Top 1:1 :%s/five/four/n As a simple example, let's change all occurrences of five to four . The command to do this is: :%s/five/four Let's test it on one file to be sure it works. We will add the n flag so that we can see the results but not execute the changes themselves: Test the Pattern a = [ "one", "two", "three", " five ", ] NORMAL Top 1:1 1 match on 1 line OK, that looks correct. Now that we have built the command we want to run, the next step is to define the list of files over which we will execute this command. This is done using the :args command. Suppose we want to execute this command over all python files in the current working directory. We can populate those paths in the arglist using: :args ./*.py we can verify the paths that we inserted into the arglist using: :args Now that the arglist has been populated, we can execute our command on each of the files in the list by calling :argdo and passing our search and replace command from above as an argument: :argdo %s/five/four Execute the Command a = [ "one", "two", "three", " four", ] NORMAL 71% 5:5