<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jovica</title>
    <description>The latest articles on Forem by jovica (@jovica).</description>
    <link>https://forem.com/jovica</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F71080%2Fea1d2c5b-1ad6-4047-9120-2ea762b108a7.jpeg</url>
      <title>Forem: jovica</title>
      <link>https://forem.com/jovica</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jovica"/>
    <language>en</language>
    <item>
      <title>Editing remote files with Vim</title>
      <dc:creator>jovica</dc:creator>
      <pubDate>Mon, 30 Mar 2020 09:31:30 +0000</pubDate>
      <link>https://forem.com/jovica/editing-remote-files-with-vim-4p9j</link>
      <guid>https://forem.com/jovica/editing-remote-files-with-vim-4p9j</guid>
      <description>&lt;p&gt;One of the lesser known features of Vim is the ability to edit files remotely, over the network.&lt;/p&gt;

&lt;p&gt;This feature comes with the netrw plugin. To achieve this, netrw uses the SSH protocol, and manages remote files via the &lt;code&gt;scp&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Here’s how to do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim scp://user@myserver[:port]//path/to/file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the double &lt;code&gt;/&lt;/code&gt; for the directory on the remote host, which is needed to correctly resolve the absolute path. &lt;code&gt;[:port]&lt;/code&gt; is optional.&lt;/p&gt;

&lt;p&gt;So with the command above you can open a file located on a remote host for editing.&lt;/p&gt;

&lt;p&gt;What actually happens in the background is that Vim uses &lt;code&gt;scp&lt;/code&gt; to download the requested file from a remote machine to a local &lt;code&gt;/tmp&lt;/code&gt; directory, and then opens it for editing. &lt;/p&gt;

&lt;p&gt;When you save your changes to the file, the changes are first applied to a local copy in &lt;code&gt;/tmp&lt;/code&gt; directory. After that, the file is uploaded via &lt;code&gt;scp&lt;/code&gt; to the remote host.&lt;/p&gt;

&lt;p&gt;If you open a directory on a remote host, you could also use netrw to browse through remote files and directories. The important thing is to always specify the directory path with &lt;code&gt;/&lt;/code&gt; at the end.&lt;/p&gt;

&lt;p&gt;Of course, it’s recommended that you use SSH keys for authentication. Otherwise, you might be asked for the SSH password too often.&lt;/p&gt;

&lt;p&gt;Beside SSH, there are other protocols supported such as sftp, ftp, dav, etc.&lt;/p&gt;

&lt;p&gt;For example, to open a file on a remote FTP server, you could run a command like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim ftp://hostname/path/to/file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Netrw offers lots of options and possibilities for remote editing, so for more information on this, take a look at &lt;code&gt;:help scp&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;What you've just read is an a section of &lt;em&gt;CHAPTER 6. WORKING WITH FILES&lt;/em&gt; from the book &lt;a href="https://jovicailic.org/mastering-vim-quickly/"&gt;Mastering Vim Quickly&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>ssh</category>
    </item>
    <item>
      <title>The "dot" Command in Vim</title>
      <dc:creator>jovica</dc:creator>
      <pubDate>Fri, 18 Oct 2019 09:59:41 +0000</pubDate>
      <link>https://forem.com/jovica/the-dot-command-in-vim-34k4</link>
      <guid>https://forem.com/jovica/the-dot-command-in-vim-34k4</guid>
      <description>&lt;p&gt;I believe you have already heard of the principle &lt;em&gt;Don’t Repeat Yourself&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In software engineering, this is a principle of software development where your focus is on reducing repetition of all kinds. As you'll see throughout the book, Vim has many ways and commands to automate different kinds of tasks, so you don't have to repeat your actions.&lt;/p&gt;

&lt;p&gt;One of the most powerful Vim command when it comes to avoiding repetition is the &lt;code&gt;.&lt;/code&gt; ("the dot") command.&lt;/p&gt;

&lt;p&gt;Hitting &lt;code&gt;.&lt;/code&gt; in Normal mode will repeat the last native Vim command you've executed.&lt;/p&gt;

&lt;p&gt;Let's say you want to delete 5 words from the cursor forward. As you already know, you could press &lt;code&gt;5dw&lt;/code&gt; and it's done. However, sometimes it's not convenient to mentally count the number of words.&lt;/p&gt;

&lt;p&gt;An alternative would be to use &lt;code&gt;dw&lt;/code&gt; to delete one word. And then press &lt;code&gt;....&lt;/code&gt; to call the dot command four times. In this case, you would repeat the latest, &lt;code&gt;dw&lt;/code&gt; command, four more times, and in this way achieve the same effect without counting the words.&lt;/p&gt;

&lt;p&gt;If you used dd to delete a line, and you want to delete 4 more lines, you could also execute &lt;code&gt;4.&lt;/code&gt; instead of pressing &lt;code&gt;....&lt;/code&gt; . That also works.&lt;/p&gt;

&lt;p&gt;It's very important to understand what is actually repeatable by the dot command. For example, if you have a sample code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my $i
my $learn
my $quickly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and your cursor is positioned on the first line. You want to append &lt;code&gt;;&lt;/code&gt; to all three lines. You could run a command like: &lt;code&gt;A;&amp;lt;Esc&amp;gt;j&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;• &lt;code&gt;A&lt;/code&gt; – would place your cursor at the end of the first line in Insert mode.&lt;br&gt;
• &lt;code&gt;;&lt;/code&gt; – you press to actually insert it, and then you press &lt;strong&gt;Esc&lt;/strong&gt; to get back to Normal mode.&lt;br&gt;
• &lt;code&gt;j&lt;/code&gt; – to move one line down&lt;/p&gt;

&lt;p&gt;Now, your cursor is at the second line. If you then press &lt;code&gt;.&lt;/code&gt; to repeat the change in next (second) line, this won't work. Here's what you'd get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my $i;
my $learn;
my $quickly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Your cursor will still be at the second line rather than on the third line, but &lt;code&gt;;&lt;/code&gt; will be appended. &lt;/p&gt;

&lt;p&gt;This brings us to conclusion that only this part of our original command was repeated: &lt;code&gt;A;&amp;lt;Esc&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, why is this the case? &lt;/p&gt;

&lt;p&gt;It’s important to remember that with the dot command, you can repeat the commands which change the contents of the buffer.&lt;/p&gt;

&lt;p&gt;A change is any command which you can use to modify your text. In our example, we had the case that command &lt;code&gt;j&lt;/code&gt; wasn't repeated, and our cursor wasn't moved to the third line.&lt;/p&gt;

&lt;p&gt;Commands like &lt;code&gt;j&lt;/code&gt; are called motions (or nouns)—and they don't affect the text itself. Command &lt;code&gt;j&lt;/code&gt; just moves the cursor, but doesn't change text anyhow, so it can't be repeated.&lt;/p&gt;

&lt;p&gt;Think in terms of the grammar of your native language: Not nouns, but verbs are used to express some sort of action. The same is true in Vim: nouns (or motions) can't affect the text, so they can't be repeated with the dot command.&lt;/p&gt;

&lt;p&gt;To see all the commands which can affect the text in a buffer, take a look at :help change.txt&lt;/p&gt;

&lt;p&gt;Of course, if you'd like to repeat multiple changes, or a combination of movements and changes, you can easily record those into a macro. &lt;/p&gt;

&lt;p&gt;You can learn all you need on macros from Macros chapter (free to download) of my book  &lt;a href="https://jovicailic.org/mastering-vim-quickly/"&gt;Mastering Vim Quickly: From WTF to OMG in no time&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Still reading? &lt;/p&gt;

&lt;p&gt;Every Tuesday I send awesome tips on Vim to 6K+ Vim fans, subscribers of my &lt;a href="https://masteringvim.com"&gt;Mastering Vim Quickly Newsletter&lt;/a&gt;. Join us for free.&lt;/p&gt;




&lt;p&gt;This post was originally published on &lt;a href="https://jovicailic.org/2018/03/vim-the-dot-command/"&gt;my blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>masteringvimquickly</category>
    </item>
    <item>
      <title>3 Little Known But Useful Vim Tips</title>
      <dc:creator>jovica</dc:creator>
      <pubDate>Mon, 26 Nov 2018 15:35:05 +0000</pubDate>
      <link>https://forem.com/jovica/3-little-known-but-useful-vim-tips-1pbg</link>
      <guid>https://forem.com/jovica/3-little-known-but-useful-vim-tips-1pbg</guid>
      <description>&lt;p&gt;I'll show you three tips which can be incredibly useful, yet, they are very little known.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tip #1 - Open/edit/save archives
&lt;/h2&gt;

&lt;p&gt;Running a command like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ vim archive.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;will open the &lt;code&gt;archive.tar.gz&lt;/code&gt;, and list the contents of the archive. From there, you can open, edit and save changes in these files, without the need to extract them first.&lt;/p&gt;

&lt;p&gt;How cool is that? :)&lt;/p&gt;

&lt;p&gt;Supported archives: tar.gz, tgz, zip, jar, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip #2 - Edit files remotely
&lt;/h2&gt;

&lt;p&gt;Vim has the ability to edit files remotely, over the network. This feature comes with the netrw plugin. To achieve this, &lt;code&gt;netrw&lt;/code&gt; uses the SSH protocol, and manages remote files via the &lt;code&gt;scp&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Here's how to do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim scp://user@myserver[:port]//path/to/file.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the double &lt;code&gt;/&lt;/code&gt; for the directory on the remote host, which is needed to correctly resolve the absolute path. &lt;code&gt;[:port]&lt;/code&gt; is optional.&lt;/p&gt;

&lt;p&gt;So with the command above you can open a file located on a remote host for editing.&lt;/p&gt;

&lt;p&gt;What actually happens in the background is that Vim uses &lt;code&gt;scp&lt;/code&gt; to download the requested file from a remote machine to a local &lt;code&gt;/tmp&lt;/code&gt; directory, and then opens it for editing. &lt;/p&gt;

&lt;p&gt;When you save your changes to the file, the changes are first applied to a local copy in &lt;code&gt;/tmp&lt;/code&gt; directory. After that, the file is uploaded via &lt;code&gt;scp&lt;/code&gt; to the remote host.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip #3 - Magic
&lt;/h2&gt;

&lt;p&gt;Did you know that you can open the last edited file on the machine with cursor on the latest location?&lt;/p&gt;

&lt;p&gt;Just start Vim, hit &lt;code&gt;Ctrl&lt;/code&gt;+&lt;code&gt;o&lt;/code&gt;+&lt;code&gt;o&lt;/code&gt; - and there you go.&lt;/p&gt;

&lt;p&gt;Keep hitting &lt;code&gt;o&lt;/code&gt; and see what happens ;)&lt;/p&gt;




&lt;p&gt;Now, if you wanna discover more Vim tips you've never heard of, take a look at my book &lt;a href="http://jovicailic.org/mastering-vim-quickly/"&gt;Mastering Vim Quickly: From WTF to OMG in no time&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vim</category>
      <category>masteringvimquickly</category>
    </item>
    <item>
      <title>The Vim Trick Which Will Save Your Time and Nerves</title>
      <dc:creator>jovica</dc:creator>
      <pubDate>Tue, 11 Sep 2018 13:31:45 +0000</pubDate>
      <link>https://forem.com/jovica/the-vim-trick-which-will-save-your-time-and-nerves-45pg</link>
      <guid>https://forem.com/jovica/the-vim-trick-which-will-save-your-time-and-nerves-45pg</guid>
      <description>&lt;p&gt;Whether you're beginner or advanced Vim user, I'm sure you've seen this at least once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E45: 'readonly' option is set (add ! to override)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It usually goes like this: &lt;/p&gt;

&lt;p&gt;You open a file with Vim and make some changes. When you try to save the file and see the message above. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Then&lt;/em&gt; you realize that you didn’t run Vim with &lt;code&gt;sudo&lt;/code&gt;?! Argh, so annoying isn't it?&lt;/p&gt;

&lt;p&gt;Today, you're gonna learn how to solve this easily, and once for all.&lt;/p&gt;

&lt;p&gt;It's very simple, all you need to do is to add this line to your &lt;code&gt;.vimrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cnoremap w!! execute 'silent! write !sudo tee % &amp;gt;/dev/null' &amp;lt;bar&amp;gt; edit!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you get to the same situation again and you can't save your file, simply run &lt;code&gt;:w!!&lt;/code&gt; command, and your changes will be saved!&lt;/p&gt;

&lt;p&gt;Now, how this works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cnoremap&lt;/code&gt; - tells Vim that the following mappings that is used when in command-line mode&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;w!!&lt;/code&gt; - the mapping (shortcut) itself.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;execute '&amp;lt;command&amp;gt;'&lt;/code&gt; - executes a command between the quotes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;silent!&lt;/code&gt; - run it silently&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;write !sudo tee % &amp;gt;/dev/null&lt;/code&gt; - the magic trick which would take another post to explain&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;bar&amp;gt; edit!&lt;/code&gt; - this calls the edit command to reload the buffer and then avoid messages such as "the buffer has changed".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hint: you need to type &lt;code&gt;:w!!&lt;/code&gt; fast, so it expands into the full command. Typing these four strings slowly and hitting &lt;code&gt;Enter&lt;/code&gt; won't work.&lt;/p&gt;




&lt;p&gt;I share tips like this at &lt;a href="https://masteringvim.com"&gt;Mastering Vim Quickly Newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I also wrote a book &lt;a href="http://jovicailic.org/mastering-vim-quickly/"&gt;Mastering Vim Quickly: From WTF to OMG in no time&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also get the best Vim tips by following &lt;a href="https://twitter.com/MasteringVim"&gt;@MasteringVim&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vim</category>
      <category>sudo</category>
    </item>
    <item>
      <title>A Simple (Yet Powerful) Tip on Git and Vim</title>
      <dc:creator>jovica</dc:creator>
      <pubDate>Tue, 31 Jul 2018 20:35:57 +0000</pubDate>
      <link>https://forem.com/jovica/a-simple-yet-powerful-tip-on-git-and-vim-pam</link>
      <guid>https://forem.com/jovica/a-simple-yet-powerful-tip-on-git-and-vim-pam</guid>
      <description>&lt;p&gt;Here's a tip you might find useful if you work with &lt;code&gt;git&lt;/code&gt; and you use Vim. I recently came up with it.&lt;/p&gt;

&lt;p&gt;For example, I want to see a file in different branch while staying in my current branch(so without changing the branch). I could use command like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git show branch_name:/path/to/file.pl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if I want to see this file in Vim? It's easy, I could run a command like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git show branch_name:/path/to/file.pl | vim -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this way there's no syntax highlight, so we can fix this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git show branch_name:/path/to/file.pl | vim - -c 'set syntax=perl'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's nice :) now my Perl file is properly highlighted.&lt;/p&gt;

&lt;p&gt;Okay, but what if I work with Perl, and Python, and Bash, etc. It's not very handy to type all of this every time.&lt;/p&gt;

&lt;p&gt;Here's one solution - you could create aliases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alias vim.py="vim - -c 'set syntax=python'"
alias vim.pl="vim - -c 'set syntax=perl'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So next time you need to see a Python file in different branch than your current one, you could run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git show branch_name:/path/to/file.py | vim.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you find this useful.&lt;/p&gt;




&lt;p&gt;I share tips like this in Mastering Vim Quickly newsletter at &lt;a href="http://masteringvim.com"&gt;http://masteringvim.com&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>git</category>
    </item>
  </channel>
</rss>
