Saturday, November 7, 2020

Let's talk about Vi/Vim - the text editor

                    ဒီ article မှာတော့ text editor တစ်ခု ဖြစ်တဲ့ Vim အကြောင်းလေး ပြောချင်ပါတယ်။ Text editor ဆိုတာက Windows မှာဆိုရင် notepad တို့ နောက် visual code editor တို့ နောက် sublime text တို့ စတာတွေနဲ့ အတူတူပဲဖြစ်ပါတယ်။ အပေါ်မှာပြောခဲ့တဲ့ editor တွေကျတော့ GUI interface တွေပါဝင်တယ် နောက် တစ်ခြား intergartionလုပ်မယ့် plug-in features တွေလဲ စုံစိနေအောင်ပါဝင်တယ်။ 

                   Vim ကိုတော့အသုံးပြုမယ်ဆိုရင် သူ့မှာ GUI မရှိဘူး terminal ပေါ်ကနေပဲ အသုံးပြုရမယ် keyboard ကို အဓိကထားပြီး အသုံးပြုတဲ့ text editor ဖြစ်ပါတယ်။ Vim ကို Linux/Unix/MacOs တွေမှာ text editor တစ်ခုအနေနဲ့ အသုံးပြုကြတယ်။ Networking ပိုင်းမှာဆိုလဲ Firewalls တွေမှာ cli mode နဲ့ ပြင်ဆင်ရမယ့် အပိုင်းတွေဆိုလဲ ဒီလိုမျိုး  text editor တွေကို အသုံးပြုရပါတယ်။ 

                    ဘယ်လိုမျိုး အဓိက သုံးကျသလဲဆိိုတာ ဥပမာ အားဖြင့် configurations files တွေ script file တွေ နောက် program source code file တွေ ဘာပဲဖြစ်ဖြစ် file တွေကို command line ကနေ ဖွင့်ပြီး edit လုပ်မယ် or script file အသစ် ရေးမယ် စတာတွေလုပ်ချင်တယ်ဆိုရင် Vim ကို အသုံးပြုလို့ရပါတယ်။ Vim သည် "Vi Improved" version ဖြစ်ပြီး originally from "Vi" text editor ကနေ ဆင်းသက်လာတာဖြစ်ပါတယ်။ Vim လိုပဲ တစ်ခြား text editor တွေ ဖြစ်တဲ့ "nano", "emacs" စတဲ့ text editor တွေကိုလဲ အသုံးပြုနိုင်တယ်။

                  Vim က feature-rich ဖြစ်တဲ့ အတွက် ရှိသမျှ usage အကုန်ပြောနေရရင် ပြီးမှာမဟုတ်တော့တဲ့အတွက် ကျွန်တော်လဲ အကုန်မသိတဲ့အတွက် :D   ဒီ article မှာတော့  အသုံးဝင်မယ်ထင်တဲ့ usage လေးတွေကို အဓိကထားပြောသွားမှာဖြစ်ပါတယ်။

Let's get started!!!


Modes in Vim

         အခု အသုံးပြုမယ့်  Vim မှာ basic modes ၃ မျိုးရှိပါတယ်။

1. Command Mode 

2. Write/Insert Mode

3. Visual Mode


1. How to open a file with Vim?

        Vim နဲ့ file တစ်ခုကို ဘယ်လိုဖွင့်မလဲ... လွယ်ပါတယ် $vim <file-name> ဆိုပြီး ဖွင့်ရမှာပါ။ ဖွင့်ပြီးပြီးချင်း ရောက်ရှိနေတဲ့ mode ကို command mode လို့သတ်မှတ်ပါတယ်။ ပြောချင်တာက edit လုပ်ဖို့ ready မဖြစ်သေးဘူး။



2. How to start edit a file with Vim?

                Vim နဲ့ file ကို ဖွင့်ပြီးသွားပြီဆိုရင် စပြီး edit လုပ်ကြမယ်။ ဒီနေရာမှာ command mode ကနေ insert mode ကို စပြောင်းဖို့အတွက် press "i" (i=insert) ဆိုပြီး အသုံးပြုရမှာဖြစ်ပါတယ်။ "Insert" mode ဖြစ်သွားပြီးဆိုရင်တော့ files ကို edit စလုပ်နိုင်ပြီဖြစ်ပါတယ်။ "i" ကိုအသုံးပြုလိုက်တဲ့အခါမှာ လက်ရှိ cursor ရှိနေတဲ့ line ကစပြီး edit လုပ်နိုင်မှာဖြစ်ပါတယ်။ 



                   ဒါမှမဟုတ် press "a" (append) ဆိုပြီး နှိပ်လိုက်မယ်ဆိုရင် လဲ insert mode ကို ရောက်ရှိမှာပဲဖြစ်ပြီး "a" ကတော့ လက်ရှိ cursor ဖြစ်နေတဲ့ စာလုံးရဲ့ နောက်ကနေစပြီး edit လုပ်နိုင်မှာဖြစ်ပါတယ်။ 



                   နောက်ဆုံး တစ်ခုကတော့ press "o" ဖြစ်ပြီး သူကကျတော့ လက်ရှိ cursor ဖြစ်နေတဲ့ line အောက်မှာ line အသစ်ခု ဖွဲ့ပြီး edit လုပ်စေမှာဖြစ်ပါတယ်။



3. How to move around within the file in Vim?


            အိုကေ move around ဆိုတာက  ကျွန်တော်တို့ Vim နဲ့ file ကို ဖွင့်ဖွင့်ချင်း "cursor" ကို command mode ကနေ lines တွေ တစ်လိုင်းပြီး တစ်လိုင်း အပေါ်တက်အောက်ဆင်း ဘယ်ညာသွားတာတွေ လုပ်တာလဲဖြစ်နိုင်တယ်။ ဒါမဟုတ် command mode ကနေ insert mode ကို ပြောင်းပြီး အပေါ်အောက်ဘယ်ညာတွေသွားတာလဲဖြစ်နိုင်တယ်။ 
            Command mode ကနေပဲ cursor move around လုပ်ချင်တယ်ဆိုရင် ကျွန်တော်တို့ keyboard မှာပါတဲ့ up/down and left/right arrow ကို အသုံးပြုနိုင်သလို..... 

            Press "j"  =  Move down

            Press "k" =  Move up

            Press "l"  =  Move right

            Press "h" =. Move left

စတဲ့ Vim က default သတ်မှတ်ထားတဲ့ Key တွေကိုလဲ အသုံးပြုနိုင်ပါတယ်။

           Insert mode ကနေ  move around လုပ်ချင်ရင်တော့ up/down, left/right arrow key ကိုပဲအသုံးပြရမှာဖြစ်ပါတယ်။

4. How to exit from a file (without-saving) with Vim?

                အိုကေ ဒါဆို insert mode ထဲဝင်ပြီးလို့ edit တွေဘာတွေ လုပ်ပြီးပြီ save လုပ်ပြီး file ထဲကနေ ထွက်မယ်ဆိုရင်... 

            1. Press Esc Key (To change back to command mode)

            2. Press : <= colon

            3. Type "wq!" ( write and quit, ! means force)

            4. Press "Enter"



                Edit လုပ်နေရင်းနဲ့ file ထဲကနေ ပြန်ထွက်မယ် save တော့ မလုပ်ချင်ဘူးဆိုရင်......
    

            1. Press Esc Key (To change back to command mode)

            2. Press : <= colon

            3. Type "q!" ( quit, ! means force)

            4. Press "Enter"


                Edit လုပ်နေတာတွေကိုပဲ save လုပ်မယ် file ထဲကနေ မထွက်သေးဘူးဆိုရင်.....

           1. Press Esc Key (To change back to command mode)

            2. Press : <= colon

            3. Type "w" ( w=write)

            4. Press "Enter"


5. How to {copy and paste} / {delete} lines in Vim?

             File ထဲမှာ ရှိတဲ့ lines တွေကို ဘယ်လို copy and past ဘယ်လို လုပ်မလဲ, line တွေကို delete ဘယ်လိုလုပ်မလဲဆိုတော့ insert mode ထဲရောက်နေရင် အရင်ဆုံး Press "ESC" key ဖြင့် command mode ထဲကို ဝင်ရမှာဖြစ်ပါတယ်။ ပြီးရင်တော့.......

            1. Press Esc Key (To change back to command mode if it's in insert mode)

            2. Move the cursor to the line that you wanted to copy

            3. Press "yy" (yy=yanked) to copy

            4. Move the cursor to select the location you want to paste

            5. Press "p" to paste and it will paste what you copied below the cursor you selected



        Copy and Paste ပြီးတော့ lines တွေကို ဘယ်လို delete လုပ်ကြမလဲ.... 

            1. Press "Esc" key ( To change back to command mode if it's in insert mode)

            2. Move the cursor around to the line you want to delete

            3. Press "dd" ( dd=delete ) to delete the entire line

အောက်ပုံမှာဆိုရင် အပေါ်က copy/paste လုပ်ခဲ့တဲ့ hello, .... line ကို ဖျက်လိုက်တာဖြစ်ပါတယ်။



6. How to copy numbers of lines and paste (or) delete numbers of lines at once in Vim?

                အပေါ်မှာ လုပ်ပြခဲ့တဲ့ copy/paste and delete lines တွေက line တစ်လိုင်းချင်းစီကိုပဲ copy/paste, delete လုပ်သွားတာဖြစ်ပါတယ်။ Line 1 လိုင်းထက်ပိုပြီး တစ်ခါထဲ​ copy ကူးချင်တယ် ဥပမာ cursor ရောက်နေတဲ့ line ကနေ စပြီး အောက် ၅ လိုင်းထိ copy ချင်ရင်........

                1. Press Esc Key (To change back to command mode if it's in insert mode)

                2. Move the cursor to the line that you wanted to copy

                3. Press "5yy" means it will copy 5 entire lines start from where cursor is selected

                4. Move the cursor to select the location you want to paste

                5. Press "p" to paste and it will paste what you copied (5xlines) below the cursor you selected




           Multiple lines တွေကို delete လုပ်မယ်ဆိုရင်တော့......

                     1. Press "Esc" key ( To change back to command mode if it's in insert mode)

                    2. Move the cursor around to the line where you want to delete

                    3. Press "5dd" ( dd=delete ) which means it will delete 5 entire lines started from where your cursor is selected.


7. How to Undo and Redo in Vim?

                ဒီတစ်ခုကတော့  ကျွန်တော်တို့ files edit လုပ်တာ မှားသွားလို့ အရင် original ကို ပြန်ခေါ်ချင်ရင် ဒါမှမဟုတ် Redo ဖြင့် edit လုပ်ခဲ့တဲ့ဟာကို ပြန်ခေါ်သုံးချင်တဲ့အခါ.........

                  1. Edit something in Insert Mode and delete for testing purpose

                  1. Press "Esc" key ( To change back to command mode if it's in insert mode)

                  3. Press "u" for Undo [and] Press "Ctrl+R" for Redo


8. How to search "keyword" inside a file with Vim?

                ဒီပုံစံကတော့ ခနခနသုံးလေ့ရှိတယ်။ ဥပမာ configuration file ထဲကို ဝင်ပြင်ရမယ်ပေါ့ file က တော်တော် ကြီးတယ် lines တွေလဲ over 100 lines လောက် ဖြစ်နေတဲ့အခါမှာ ကိုယ်က ကိုယ် ပြင်ချင်တဲ့ "keyword" ကို သိနေတယ် ဒါပေမယ့် exactly in which location ဆိုတာကို မခန့်မှန်းတက်ဘူးဆိုရင် သုံးပါတယ်....

             1. Press "Esc" key ( To change back to command mode if it's in insert mode)

             2. Press "/" and type the keyword you wanted to search

             3. Press "Enter" and if there are same multiple keywords, just keep on pressing "n" for next keyword



9. How to comment and uncomment multiple lines in Vim?

            Configuration file တွေမှာဆိုရင် line ရဲ့ ရှေ့မှာ # (comment) လေးခံလိုက်ရင် ဒီ line က description အနေနဲ့ သုံးတာပဲဖြစ်ဖြစ်, ဒီ line ကို config ထဲမှာ အသုံးမပြုချင်လို့ exclude လုပ်ချင်ရင်ပဲဖြစ်ဖြစ် # (comment) ခံပြီး သုံးရပါတယ်။ပုံမှန် တစ်လိုင်းချင်စီ # (comment) လုပ်သွားမယ်ဆိုရင်တော့ insert mode ဖြင့် ဝင်ပြီး # (comment) လုပ်သွားရုံပဲဖြစ်ပါတယ်။ Multiple lines တွေကို တစ်ချိန်ထဲမှာ # (comment) ပေးချင်ရင်တော့.............

                 1. Press "Esc" key ( To change back to command mode if it's in insert mode)

                 2. Press "Ctrl+v" ( To change to Visual block )

                 3. Use "arrow key" to select the multiple lines you want to comment

                 4. Press "Shift+I" ( To enable insert mode while in visual mode )

                 5. Press "#" comment ( Put # in front of the lines )

                 6. Press "Esc" and wait a while, it will automatically put # (comment) to the lines up to where you have selected.





        Uncomment to multiple lines လုပ်ချင်ရင်တော့..............

                    1. Press "Ctrl+v" to change to visual mode

                    2. Use "arrow" key to select the "commented" lines you want to remove

                    3. After selected, Press "x" and it will remove "#" (comment) from all of the lines you have selected and chosen to uncomment at once.      

                    အိုကေ ဒီလောက်ဆိုရင် Vim ကို အခြေခံ လောက် ဖြစ်တဲ့  ဘယ်လိုဝင်မလဲ, edit, copy and paste, delete, undo and redo and search ဘယ်လိုလုပ်မလဲဆိုတာ သိသွားလောက်ပြီထင်ပါတယ်။ ကျန်တဲ့ တစ်ချို့ မှတ်ထားသင့်တဲ့ usage တွေကိုတော့ screenshot နဲ့ ပြဖို့လဲ သိပ် effect မဖြစ်တာကြောင့် ဒီအတိုင်းပဲ Usage = Command ပုံစံနဲ့ပဲ ဖော်ပြ လိုက်ပါတယ်။

Below usages are from "Command mode" not in "Insert mode"

1. Press "gg" = File ရဲ့ first page ကို သွားချင်ရင်သုံးတယ်

2. Press "Shift+G" = File ရဲ့ last page ကို ရောက်ချင်ရင်သုံးတယ်

3. Press "Shift+$" = Line တစ်လိုင်းရဲ့ နောက်ဆုံး ကို သွားတယ်

4. Press "0" =  Zero ကို နှိပ်လိုက်ရင် Line ရဲ့ နောက်ဆုံးကနေ ရှေ့ဆုံးကို ပြန်ရောက်သွားတယ်

5. Press "x" = x ကို ကျတော့ words တွေကို ဖျက်ချင်တဲ့အခါသုံးတယ် insert mode ထဲမှာဆိုရင်တော့ backspace နဲ့လဲ ဖျက်နိုင်တယ်။ comand mode က နေဖျက်မယ်ဆိုရင်တော့ ဖျက်ချင်တဲ့ keyword ကို cursor နဲ့ select လုပ်ပြီး Press "x" နှိပ် နှိပ်သွားရင် တစ်လုံးချင်းစီ ဖျက်သွားမှာဖြစ်ပါတယ်။

6. Press "w" = move the cursor to the next word

7. Press "b" = move the cursor to the previous word


Vim အကြောင်းလေးကို ဒီလောက်နဲ့ပဲ ရပ်ပါတယ်။ နောက်ထပ် အသုံးပြုနည်း တွေလဲ အများကြီးကျန်ပါသေးတယ် ထပ်လေ့လာချင်ရင်တော့ google မှာ vim cheat sheet ပဲဖြစ်ဖြစ် search ကြည့်ကြည့်ပါ။


That's it 😊


Pls Like and Subscribe Our Root Of Info FB Page and Youtube Channel


https://www.facebook.com/rootofinfo


https://www.youtube.com/channel/UCkOi7WxhUBKONv3uD0CvuWw?view_as=subscriber


Thank you!!!


Share:

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.