ACING THE CKAD

CKAD Exam Tips: How to Use the Vim Editor

Edidiong Asikpo
Ambassador Labs
Published in
7 min readMar 8, 2022

--

From my recently published “CKAD and CKA Exam Tips from 10 People Who Passed the Exam” article, understanding how to use Vim was a recurring tip from the people featured.

Vim is a free and open-source text editor. But unlike other text editors, Vim can be used directly from a command-line interface even though it also has a graphical interface. It supports hundreds of programming languages and file formats, has a powerful search and replace feature, but most of all, it’s command-centered meaning that with just a few commands, you can perform complex text-related tasks.

The purpose of this article is to help you understand why you should learn how to use Vim before taking the CKAD exam, and how to use it effectively.

Should I understand how to use Vim before taking the CKAD exam?

The short answer is: yes, but I bet you want to understand why the answer is yes. So, in this part of the article, I’ll share 3 scenarios that highlight how an understanding of Vim can increase your chances of passing the CKAD exam.

Scenario 1: Creating a Kubernetes Object during the CKAD exam

There’s a good number of Kubernetes Objects that can be created using imperative commands. However, there are others like the persistentVolume or the persistentVolumeClaim that can only be created declaratively. In situations like this, you’d have to create a YAML configuration file manually and then use either the kubectl create or kubectl apply command to apply these objects into your Kubernetes cluster.

When taking the CKAD exam, you can’t create YAML configuration files using your favorite IDE because there’s no way to link them up to the dev environment used for the exam. So, the only way to create YAML files is directly on the CKAD dev environment. But how can you do that quickly? By using Vim!

When you run this vim command vim [insert name].yaml, it will not only generate an empty YAML file for you, but it will also open up the file on the Vim editor. Now, you can add the commands needed to create the Kubernetes object of choice, save the YAML file, and use the kubectl create command to apply it to your cluster.

Scenario 2: Editing a Kubernetes object during the CKAD exam

You’ll most likely have to work on a task that involves editing an existing Kubernetes object or YAML file. In this case, you’d have to either edit the Kubernetes object using the kubectl edit command or move the content of that Kubernetes object to a new YAML file using the-o yaml > command before editing it.

Regardless of the pattern you use, you’ll have access to the file’s content and will have to edit it to meet the needs of the question asked. This is where Vim comes in handy as you’d have to search for a word quickly, delete and add text, amongst other things.

Scenario 3: Fixing an error in a YAML configuration file during the CKAD exam

When you edit a Kubernetes object or YAML file, there’s a chance that you’ll make a mistake that you’ll only discover after you’ve already saved your changes.

Now, let’s imagine for a second the error message returned by Kubernetes specified that there’s a problem on line 87. Your normal instinct as a developer would be to quickly open up that file, scroll down to number 87, and fix the issue. But it’s not that easy with the IDE on the CKAD dev environment because there’s no visible numbering. So, instead of tapping on the arrow buttons on your keyboard and manually counting in your mind till you reach number 87, you can use this :87 command. This will immediately take your cursor to the line specified (87 in this case), allowing you to identify the problem and fix the issue quickly.

As I mentioned in “CKAD Tips: Kubernetes Object Management Techniques”, speed is vital if you want to pass the CKAD exam, so every second matters. By understanding how to use vim commands, you’ll swiftly complete all the tasks during the exam.

Now that you understand the why, let’s dive into understanding the how!

Vim commands you should know for the CKAD exam

One of Vim’s core beliefs is that most people spend more time editing existing text than writing new text. For example, a software engineer who is often tasked with enhancing and maintaining existing code or someone taking the CKAD exam asked to make certain modifications to an existing Kubernetes object.

There are so many Vim commands out there. To make it easier for you to assimilate and identify these commands, I divided this part of the article into four subsections — basics, navigating, editing, and searching. Enjoy! 💛

Basic Vim commands

These are the most basic commands that you have to know to use Vim. The chances of you using the commands in this section during your CKAD exam are very high!

  • vim [insert file name] — You can use this command to create a new file vim ckad.yaml or open up a YAML file vim pod-definition.yaml.
  • i — Once you open up the vim editor using the command above, you’ll only have access to Vim’s command mode (where you can’t edit or insert things into a file). To switch to Vim’s insert mode (where you can edit or make changes to the file), you must click i on your keyboard.
  • esc — In the insert mode, you can no longer run commands aside from editing. To return to the command mode, You’d have to click on the esc key on your keyboard.
  • hjkl — You can use these keys to move the cursor around to the left, down, up, and right, respectively.
  • :w — After you’ve made changes to the Vim editor, you need to save it, right? That’s what this command is used for.
  • :q — This is used to quit the Vim editor.
  • :wq — This command is used to save a file and quit the Vim editor simultaneously.
  • q! — You can use this to discard unsaved changes.

Cursor navigation Vim commands

Being able to navigate through the Vim editor quickly is super important. The faster you can move to the section of choice, the better. Remember, speed is vital!

  • 0 — This will place your cursor at the beginning of a line
  • $ — This will place your cursor at the end of a line
  • ) — This takes you to the start of the next sentence
  • ( — This takes you to the start of the previous sentence
  • H — This will put your cursor at the top of your screen
  • M — This will put your cursor in the middle of your screen
  • L — This will put your cursor at the bottom of your screen

Vim commands for editing

At the beginning of this article, I shared some scenarios explaining why an understanding of Vim was key to passing the CKAD exam.

One of those scenarios highlighted the need to edit Kubernetes objects or YAML configuration files quickly. This section will show you commands that can come in handy when editing.

  • d — If you want to delete some sections of the YAML file, you can highlight them and then click on the d key to delete all of them simultaneously.
  • dd — This will delete a line of text at the location of your cursor.
  • D — This deletes everything from the location of your cursor to the end of the line
  • d0 — This deletes everything from the location of your cursor to the beginning of the line
  • dgg — This deletes everything from the location of your cursor to the beginning of the file
  • dG — This deletes everything from the location of your cursor right up to the end of the YAML file
  • u — Mistakenly deleted something? This key can be used to undo the last operation you performed.
  • ctrl + r — Thought you added the wrong text, deleted it but then realized you didn’t? You can use this command to redo the last operation you performed.
  • p — This will paste whatever had been copied to the unnamed register

Vim commands for searching

There’ll be situations where you’ll be asked to change something in a file. So, instead of manually scrolling and glancing through each line of the file, you can use these commands to find the text you are looking for.

  • /[keyword] — This can be used to search for text in the YAML file instead of manually scrolling up and down to find it. All you need to do is replace [keywprd] with the word you are trying to find.
  • ?[keyword] — This searches the previous text for your keyword, character string, or phrase.

Conclusion

If you don’t understand how to use Vim, you will end up being stuck on a particular task longer than you should be, and like I pointed out several times in this article — speed is vital!

Vim is available on basically every major platform you can think of. Whether you’re using a Mac, Windows, or some Linux distribution — Vim can work on it. So, go ahead and play around with Vim until you are very comfortable with it, as it will increase your chances of passing the CKAD exam.

Ace the CKAD with me

This article is part of the CKAD Tips weekly series, where I write articles that will enable anyone studying for the CKAD to excel during the examinations. Stay up to date on the latest additions to this series by following us on Medium or Twitter and kickstart your Kubernetes learning on our Kubernetes Developer Learning Center.

The CKAD series, so far:

--

--