Building vim like clone with C++
If you have not heard of vim that’s too bad should be more involved with it. For the most part its the arch btw version of text editor. I was not born in era of emacs vs vim so don’t know much about it, just have heard or seen ancient relics discussing about that (posted 13 years ago) Feeling was nothing but similar to as that when I wished someone on stackoverflow would have posted the regex pattern which I wanted in PAIE (Pre AI ERA). But Vim is something I have been using for about a year now (insert mode, dd , view , wq , mode , :line_number) that’s sums it up along with some helpful extensions.
So decided to write my own vim (how hard can that be!!!! duh~~~~).
Like any other good program lets discuss how to map it first .
For this first part only one aim.
Modes that the progam will live in (visual, insert and normal).
So without wasting any more time and to waste more time lets dive in.
Get Aman Ahmed’s stories in your inbox
Join Medium for free to get updates from this writer.
Now first we will implement state in which the program will be in. We can use ENUM for that
enum class mode_type {
NORMAL,
INSERT,
VISUAL
}next is the context for the editor based on modes
struct editor_context {
std::vector<std::string> lines;
};
Now abstract base model
class editor_mode {
public:
virtual ~editor_mode() = default;
virtual handle_key(char key, editor_context& ctx) = 0;
virtual mode_type get_type() const = 0;
} ;now post this we will need three child class which will inherit the editor mode
class insert_mode : public editor_mode {
public:
void handle_key(char key, editor_context& ctx) override;
mode_type get_type() const override {
return mode_type::INSERT;
}
};
class visual_mode : public editor_mode {
public:
void handle_key(char key, editor_context& ctx) override;
mode_type get_type() const override {
return mode_type::VIEW;
}
};
class normal_mode : public editor_mode {
public:
void handle_key(char key, editor_context& ctx) override;
mode_type get_type() const override {
return mode_type::NORMAL;
}
};main class
class editor {
public:
editor();
std::unique_ptr<editor_mode> current_mode;
void switch_mode(mode_type new_mode);
void run();
};
Complete Code
#include <iostream>
#include <vector>
#include <memory>
enum class mode_type {
NORMAL,
INSERT,
VISUAL
};
struct editor_context {
std::vector<std::string> lines;
};
class editor_mode {
public:
virtual ~editor_mode() = default;
virtual void handle_key(char key, editor_context& ctx) = 0;
virtual mode_type get_type() const = 0;
};
class insert_mode: public editor_mode {
public:
void handle_key(char key , editor_context& ctx) override;
mode_type get_type() const override {return mode_type::INSERT ;}
};
class normal_mode: public editor_mode{
public:
void handle_key(char key , editor_context& ctx) override;
mode_type get_type() const override { return mode_type::NORMAL;}
};
class visual_mode: public editor_mode{
public:
void handle_key(char key , editor_context& ctx ) override;
mode_type get_type() const override {return mode_type::VISUAL;}
};
class editor {
private:
editor_context context;
std::unique_ptr<editor_mode> current_mode;
public:
editor();
void switch_mode(mode_type new_mode);
void run();
};





