Welcome to tui.ninja!


Today's Featured Neovim Tips & Tricks:

expand_more

Normal mode commands can be used to apply an operation over each line in a range . Suppose you are creating a list in python by yanking and pasting some content from another buffer . After pasting, the items are in place but are missing the trailing , required by python: Initial Conditions a = [ "one" "two" "three" ] NORMAL 33% 2:1 These could be manually added in various ways, but Vim's :normal command provides a handy solution by allowing a normal mode command to be applied to each line in a range. To demonstrate, we first create a visual selection: Select Visual Rangev2j a = [ "one" "two" "three" ] VISUAL 67% 4:1 Note that the range could have also been entered manually. Next create the command to execute. We would like to move the cursor to the end of each line, enter insert mode after the line, then enter the , character. Inserting after the line can be achieve with the A command, so the command to execute for each line is: A, and the complete command is: :normal A, Executing this command in our buffer provides the desired result: Execute the Command a = [ "one", "two", "three", ] NORMAL 67% 4:12
Filed In:formatting

Starting 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 Selectionv <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 Selectionj <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.
Filed In:editingshell

A common task when editing files is to remove duplicate lines. While this can be done manually, it is often easier to leverage Vim's command-line mode to leverage the uniq command. Suppose we have the following file in a buffer and want to remove duplicates: Initial Conditions bananas apples apples oranges apples COMMAND Top 1:1 :%!uniq The command to execute is: :%!uniq Let's break this down to review what is happening: To start, this follows the pattern: :[range]!{cmd} {args} where :% specifies the range as the entire file, although this can be replaced with a range specifying a subset of lines, or a visual selection . Next, !{cmd} indicates that an external command is to be called with the specified range and arguments. In this case the external command to be called is: uniq with (initially) no arguments. By default uniq takes input from stdin, so there is no need to specify the input source. Executing this command produces the following output: Execute uniq bananas apples oranges apples NORMAL Top 1:1 5 lines filtered which is not exactly what we were looking for. It turns out that by default uniq only detects adjacent repeating lines, so apple still appears twice in the output. If we take a look at the help page for uniq we see that the -u option can be used to print only unique lines, which is what we were looking for. Lets undo to get back to the original buffer, then try again using the -u argument: Execute uniq -u bananas oranges apples NORMAL Top 1:1 5 lines filtered That is more like it. We should note that uniq also offers the -d option, which performs the opposite function and returns only the duplicate lines. Starting from the original buffer we can execute this command to see the result: Execute uniq -d apples NORMAL Top 1:1 5 lines filtered
Filed In:editing

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 Beautiful 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. COMMAND Top 1:1 :%s/is/is way/ Suppose one wants to change all occurrences of is better to is way better. 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}/ is Specifies the pattern to search for {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: Title Beautiful is way better than ugly. Explicit is way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. Sparse 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. Writing patterns can sometimes require a bit of trial and error, if the final text is not as expected, simply hit u to return to the original: Titleu Beautiful 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 6 changes; before #1 0 seconds ago