ضروریات Vim در لینوکس – انجمن DEV

Summarize this content to 400 words in Persian Lang
فهرست مطالب
پس چرا این مقاله؟
سوال خوب! من استفاده کرده ام Vim در macOS برای مدت بسیار طولانی، اماprimarily برای نوشتن Git پیام ها را متعهد کنید Most از من file editing داردtaken place داخل Visual Studio Code.
Then من started working در Linux، but در یک virtual environment(VirtualBox) installed در Windows 11. این memory و disk space منallocated به آن بود much smaller از what من دارم available در macOS،و Visual Studio Code است known بخاطر حریص بودن حافظه من نصبش کردملینوکس مینت، اما سرعت آن را به شدت کاهش داد OS و حتی crashed از بهtime به time. That است when من decided به use را Vim ویرایشگر متنinstead.
By default، Vim انجام داد not آن را قطع کن for what من wanted به accomplish، soمن انجام دادم some research به learn more در مورد آن، how من می توانستم configure آن را بهhelp من accomplish آنچه من wanted به accomplish، و better حرکت کنیدVim در general.
Well، من found out که من can اساسا use آن را like هر دیگری editor،از جمله Visual Studio Code. آن را was الف matter از mastering آن را Butوجود دارد another aspect به this.
در Linux Mint داخل VirtualBox، برای من، پیش فرض است Terminal بر روی استsmall side. من laptop screen است not را largest و Linux Mintپنجره در VirtualBox است not را largest یا این key input با استفاده ازlaptop keypad است not بیشترین fantastic experience در Windows. بنابراین whatآیا من do در مورد that?
It بود serendipitous. در تحقیقاتم، من accidentally آمد across یک جدیدterminal program تماس گرفت Warp. Currently، آن works در Linux وmacOS یونیکس operating systems. Warp برای Windows است also بیرون آمدنsoon، اما دارد not بوده است released هنوز
Basically، Warp یک است modern، مبتنی بر زنگ terminal با AI ساخته شده در بنابراینyou و your تیم می تواند build عالی software سریعتر
All را Terminal اسکرین شات و گیف I use اینجا بودند taken در Warp.
من آن را دوست دارم Despite داشتن AI ساخته شده، وجود دارد still یک است significantیادگیری curve. این کار را انجام می دهد detect و بنابراین tools ما use در ماnative OS، مانند shell. استفاده می کنم ZSH در macOS، بنابراین Warp استفاده می کند ZSH. درLinux، استفاده می کنم Bash، بنابراین Warp استفاده می کند Bash وجود دارد. من هستم never رفتن back بهiTerm2 در macOS یا Linux Mintپیش فرض است Terminal برنامه Warp همهway!
Vim مخفف چیست؟
Vim مخفف Vi iMproved. Vi است frequently الف symbolic link به Vim،را enhanced version از Vi. If شما دارید vim نصب شده و access بهرا vim فرمان، تو also دارند access به vi فرمان When مننصب شده است Vim در Linux Mint، من gained access به both را vim و viفرمان
پیکربندی Vim
به make من life بسیار easier چه زمانی working با Vim، من configured آن راusing یک فایل I created (که بود not included توسط default) تماس گرفت.vimrc داخل من home directory (/home/maria در Linux و/Users/mariacam در macOS). من added را following به .vimrc فایل:
# Disable compatibility with vim which can cause unexpected issues.
set nocompatible
set autoindent
# Set tab width to 4 columns.
set tabstop=4
# Use space characters instead of tabs.
set expandtab
# Set shift width to 4 spaces.
set shiftwidth=4
# Add numbers to each line on the left-hand side.
set number
# Highlight cursor line underneath the cursor horizontally.
set cursorline
# Highlight cursor line underneath the cursor vertically.
set cursorcolumn
# Do not save backup files.
set nobackup
# While searching though a file incrementally highlight matching characters as you type.
set incsearch
# Do not wrap lines. Allow long lines to extend as far as the line goes.
set nowrap
# Ignore capital letters during search.
set ignorecase
# Override the ignorecase option if searching for capital letters.
# This will allow you to search specifically for capital letters.
set smartcase
# Show partial command you type in the last line of the screen.
set showcmd
# Show the mode you are on the last line.
set showmode
# Show matching words during a search.
set showmatch
# Use highlighting when doing a search.
set hlsearch
# Set the commands to save in history default number is 20.
set history=1000
# Enable auto completion menu after pressing TAB.
set wildmenu
# Make wildmenu behave like similar to Bash completion.
set wildmode=list:longest
# Wildmenu will ignore files with these extensions.
set wildignore=*.docx,*.jpg,*.png,*.gif,*.pdf,*.pyc,*.exe,*.flv,*.img,*.xlsx
# Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype on
# Enable plugins and load plugin for the detected file type.
filetype plugin on
# Load an indent file for the detected file type.
filetype indent on
# Turn syntax highlighting on.
syntax on
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
این immediately ساخته شده است navigating فایل ها در Normal mode وcreating content در Insert mode خیلی easier!
If من open من .vimrc فایل using را vim .vimrc فرمان در macOSiTerm2، اکنون looks like را following:
اگر من open من .vimrc فایل با استفاده از vim .vimrc فرمان در macOS پیچ و تابTerminal، اکنون looks like را following:
Highlighting colors و الف few دیگر details می differ slightly چه زمانیبا استفاده از Warp ترمینال. However، به عنوان shown above، اینها configurations هنوزwork well با Warp و do not باعث هر گونه شود conflicts از آنجایی که Warp کار می کندبا Vim.
دستور vim
این first command به learn در رابطه با Vim است vim فرمان In orderبه open الف file در Vim، من run را following در Terminal:
# this command opens up a file called history.txt in Vim
vim history.txt
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
حالت های Vim
هستند 4 اصلی modes در vim. Normal mode، Insert mode، Visual mode،و Command حالت According به Warp اسناد، نیز وجود داردReplace mode (که در واقع همینطور است part از Normal mode; Replace mode استredundant) Binary mode، و Org mode. Binary و Org mode سقوطunder بیشتر advanced Vim features و اراده not باشد covered اینجابا این حال، information مربوط به both است available درRelated Resources واقع در bottom از این article.
حالت عادی
Normal mode است default mode چه زمانی we وارد کنید Vim. آن است mode کهallows ما به move around الف file. Normal mode می تواند باشد accessed ازother modes توسط pressing را esc کلید
میانبر (کوچک) gg
When من press را g کلید twice، gg، من هستم taken به beginning از یکfile.
میانبر G (بزرگ).
When من press را G کلید (Shift key + G کلید)، من هستم taken به endاز یک file.
میانبر # (تعداد) G
من love این one. # یک است placeholder برای line number که شماwant به go to. بیایید بگوییم من want به go به خط کشیدن 15 از یک file در Vim،من می خواهم go به Normal mode توسط pressing را esc کلید، then تایپ می کرد15G (1 کلید + 5 کلید + Shift key + G key) و من خواهم بود taken بهbeginning از خط 15 از یک file در Vim.
میانبر h (کوچک).
When من press را h کلید، cursor حرکت می کند one character به left.
میانبر j (کوچک).
When من press را j کلید، cursor حرکت می کند one خط down.
میانبر k (کوچک).
When من press را k کلید، cursor حرکت می کند one خط up.
میانبر l (کوچک).
When من press را l کلید، cursor حرکت می کند one شخصیت to راright.
میانبر # (عدد) j
This است similar به میانبر # (تعداد) G.When من press کلید Shift + 6 کلید + j کلید، به عنوان مثال، cursorحرکت می کند 6 خطوط down.
میانبر # (عدد) k
This است similar به میانبر # (تعداد) G.When من press را 8 کلید + (کوچک) k کلید، به عنوان مثال، cursorحرکت می کند 8 خطوط up.
میانبر w (کوچک).
When من press را w کلید، cursor حرکت می کند به beginning ازnext word.
میانبر b (کوچک).
When من press را b کلید، cursor حرکت می کند به beginning ازprevious word.
میانبر (کوچک) e
When من press را e کلید، cursor حرکت می کند به end از یک word.
میانبر W (بزرگ).
When من press را Shift کلید + w کلید، cursor حرکت می کند بهbeginning از next word بعد از یک whitespace. این است basically راsame به عنوان میانبر w (کوچک).
میانبر B (بزرگ).
When من press را Shift کلید + b کلید، cursor حرکت می کند بهbeginning از previous word قبل از الف whitespace. این است basicallyرا same به عنوان میانبر b (کوچک).
میانبر E (بزرگ).
When من press را Shift کلید + e کلید، cursor حرکت می کند به end از یکword قبل از الف whitespace. این است basically را same به عنوانمیانبر (کوچک) e.
میانبر 0 (صفر).
When من press را 0 کلید، cursor حرکت می کند به beginning از یک line.
میانبر ^ (caret).
این ^ (caret) shortcut است same به عنوانمیانبر 0 (صفر). When من press را Shiftکلید + 6 کلید، cursor حرکت می کند به beginning از یک line.
میانبر (علامت دلار) $
When من press را Shift کلید + 4 کلید، cursor حرکت می کند به end از یکخط
میانبر r (کوچک).
When من move را cursor بیش از یک particular character من want بهreplace، من press را r کلید، و then من press را key ازcharacter من want به replace آن را with.
این way این shortcut آثار است when منmove را cursor بیش ازcharacter من want به replace، و سپس press را r کلید، characterمن want به replace است first replaced توسط یک r شخصیت، و then وقتی منpress را character (key) من want به replace آن را with، آن replacesرا r که appeared وقتی من executedمیانبر r (کوچک).
میانبر x (کوچک).
این shortcut است similar بهمیانبر r (کوچک). When من move راcursor بیش از character من want به delete، من پس از آن press را x کلید،و character است deleted. این shortcut نیز هست referred به as را”دکمه حذف”.
این way این shortcut آثار است when من move را cursor بیش ازcharacter من want به delete، و سپس press را x کلید، characterمن wanted به delete است deleted. But اگر من want به bring back راcharacter من دارم deleted، من press (lowercase) u کلید کهexecutes میانبر (کوچک) u.
میانبر X (بزرگ).
این shortcut (معروف به “Backspace button”) حذف می کند characters بهleft از cursor، اما not را character آن است highlighting (if آن رااست highlighting یک). So اگر شما delete یک entire line با استفاده ازمیانبر X (بزرگ)، except برایcharacter را cursor است highlighting، شما می توانید press (lowercase)x کلید، و character خواهد بود removed.
میانبر (کوچک) dw
When من highlight الف word، و سپس press را d به دنبال آن w کلید،را word است deleted. و if من want به bring back که word، من می خواهمpress (lowercase) u کلید به undo که edit. If، however، منwanted به replace را word من deleted، من می خواهم want به useمیانبر (کوچک) cw.
In order به highlight الف complete word، من have وارد شدن Visual mode. بهswitch از Normal Mode به Visual mode، من first را فشار دهید esc کلید وسپس (lowercase) v کلید این esc کلید takes من out از Normal mode،و (lowercase) v کلید takes من به Visual mode. Then، من useیا forward arrow کلید به highlight را word به right ازcursor، مطمئن شوید که end برجسته کردن after را last character ازword، یا left arrow کلید به do را same در opposite direction.
میانبر (کوچک) cw
When من highlight الف word، و سپس press را c کلید followed توسطw کلید، word است deleted، و من پس از آن replace آن را با another one.
In order به highlight الف complete word، من have وارد شدن Visual mode. بهswitch از Normal Mode به Visual mode، اول من press را esc کلید وthen (lowercase) v کلید این esc کلید takes من out ازNormal mode، و v کلید takes من به Visual mode. Then، استفاده می کنمeither را forward arrow کلید به highlight را word به right ازcursor، مطمئن شوید که end برجسته کردن after را last character ازword، یا left arrow کلید به do را same در opposite direction.
میانبر (کوچک).
When من highlight الف character با cursor و سپس press را sکلید، من پس press را key از character من want به replace آن راwith.
میانبر S (بزرگ).
When من highlight را location در یک line جایی که من want به start راedit از من line، and سپس press را Shift کلید + s کلید، آن removesکه content، و then من می توانم type در what من want به replace آن راwith.
میانبر D (بزرگ).
When من place من cursor جایی که من want به start deleting الف line، andسپس press را Shift + d کلید، آن deletes را line از that point بهرا end از line.
میانبر dd (کوچک).
When من select را point از which من want به delete الف line با استفاده ازرا cursor، من پس از آن press را d کلید followed توسط pressing را d کلیدagain، and را line از location از cursor در، است deleted.If من place را cursor در beginning از line، entire lineاست deleted.
میانبر C (بزرگ).
This است similar به میانبر (بزرگ) D،اما when من press را Shift کلید + c کلید، after (part of)line بوده است deleted، من می توانم replace آن را با new content.
میانبر (کوچک) yy
This کار می کند similarly بهمیانبر dd (کوچک)، except کهinstead از line بودن deleted، it است copied. من first قرار دهیدcursor جایی که من want برای شروع copying را line، و then من press را(lowercase) y کلید followed توسط pressing (lowercase) y کلیدagain، and را part از line من selected (یا complete line اگرمن placed را cursor در beginning از line) است copied بهclipboard.
میانبر p (کوچک).
After من دارم copied الف line (یا part از it) as perمیانبر (کوچک) yy، I press را pکلید، و line است pasted زیر line من copied.
میانبر P (بزرگ).
After من دارم copied الف line (یا part از it) as perمیانبر (کوچک) yy، I press را Shiftکلید + p کلید، and را line است pasted above را line من copied.
میانبر (کوچک) u
When من press را u کلید، آن undoes را last change من made. من canهمچنین keep در pressing را u کلید در order به keep در undoing قبلیchanges. Once من هستم happy با current state از من file، من canنوع :w بنابراین that من save این acceptable state. If من want برای خروجVim بعد از saving را acceptable state، من would به سادگی press را escکلید followed توسط : (Shift key +(;) semi-colon کلید)، and منwould باشد taken back به من Terminal سریع
میانبر Control + r
این Control key + r کلید undoes من undo من executed بامیانبر (کوچک) u. و if من want بهrevert به من latest undo، من فقط press (lowercase) u کلیدexecuting میانبر (کوچک) u.
تکرار دستورات
تکرار یک دستور با استفاده از شمارش
Vim پشتیبانی می کند shortcut repetition در Normal mode. Instead از enteringهمان shortcut چندگانه times به repeat را desired action، من می توانمprefix را same shortcut با number از repetitions. به delete 6lines، من می خواهم highlight را beginning از line من wanted به startمن deletion با cursor، و then من می خواهم press را 6 کلیدfollowed توسط d کلید and را d کلید again (6dd). This خواهد شدdelete را line جایی که cursor است located و 5 خطوط after که
به repeat میانبر (کوچک) yy، من می خواهمhighlight را beginning از line من wanted به start من copyingبا cursor، و then من می خواهم press را 6 کلید (again،for example) followed توسط y کلید و y کلید again (6yy).Next، من می خواهم move را cursor زیر current line و سپس move آن را5 times بیشتر، برای یک total از 6 خطوط، stopping در last line منwant به copy ( cursor باید appear به “underline” که line).This خواهد شد copy را line جایی که cursor began و 5 خطوط afterکه Lastly، من می خواهم press (lowercase) p کلید، not حرکت دادنcursor، و this خواهد شد paste را 6 خطوط I copied.
این screenshot زیر shows را first line که من wanted بهstart copying از و where من placed را cursor در beginning ازکه line، و then فشار دادم 6yy (6 کلید + y کلید + y کلید) درbeginning از line جایی که cursor است located:
این screenshot زیر shows را end line از line block من want بهcopy. من moved به این line بعد از من had فشرده شده است 6yy (به عنوان describedبالاتر از first screenshot). من had به take را cursor به lineجایی که من wanted به end را copying process.
Then من pressed را p کلید، و 6 خطوط I copied هستند pasted زیررا lines من copied:
حالت درج
Insert mode اجازه می دهد me به type/insert شخصیت ها just like الف regularمتن editor. من can وارد کنید Insert mode توسط using یک insert shortcut ازNormal mode.
ورود به حالت Insert
There هستند several ways از ورود Insert mode.
میانبر (کوچک) i
When من press را i کلید، من هستم switched به Insert mode. من know من هستمدر Insert mode چون — Insert — ظاهر می شود bottom ازwindow.
میانبر (بزرگ) I
When من press را Shift کلید + i کلید، من switch به Insert mode، andرا cursor حرکت می کند به beginning از line (اگر باشد not در حال حاضرthere).
میانبر (کوچک).
[The (lowercase) a shortcut] behavior است almost را same به عنوان(کوچک) i میانبر. این only difference استthat وقتی من press را a کلید، و زمان — Insert — ظاهر می شودbottom از window، cursor حرکت می کند from را beginning ازline به next character به right.میانبر (بزرگ) A
When من press را Shift کلید + a کلید، من switch به Insert mode،و cursor حرکت می کند to را end از line.
میانبر (کوچک) o
After من press را o کلید، من switch به Insert mode، و cursorحرکت می کند down به second line، creating یک جدید line space. این contentsاز second line منتقل می شود down به third line.
میانبر (بزرگ) O
When من press را Shift + o کلید، من switch به Insert mode، and راfirst line حرکت می کند down به second line، leaving الف line space بر رویخط اول
تغییر از حالت Insert به حالت عادی
In order به switch از Insert mode به Normal mode، من press راesc کلید و if من همیشه هستم unsure آیا من am در Normal mode یا not،من press را esc کلید به make sure.
حالت بصری
I استفاده کنید Visual mode برای انتخاب text، similar به clicking و draggingبا یک mouse. Selecting text اجازه می دهد shortcuts به apply فقط بهselection، مانند copying، deleting، replacingو غیره
وارد شدن به حالت ویژوال
به make الف text selection، first من press (lowercase) v کلید، andاین will همچنین mark را starting انتخاب point. Then من move راcursor به end selection point، و Vim الف را فراهم می کند visual highlightاز text selection.
میانبر (کوچک) v
این shortcut نیز هست known به عنوان Visual mode visual subtype.
When من want وارد شدن Visual mode visual subtype، من first را فشار دهید escکلید به exit هر چه باشد mode من بودم in، و then من press را v کلید، اینswitches من به Visual mode، و will همچنین mark راstart selection point. When من move را cursor در سراسرfirst line، cursor حرکت می کند character توسط character.
Next، من می توانم use را Up arrow یا (lowercase) k کلید بهselect lines رفتن up. As ذکر شده است previously، no موضوع what راdifference در line lengths، lines هستند completely selected. Sameبا when من use را Down arrow کلید یا (lowercase) j کلید بهselect lines رفتن down.
میانبر (بزرگ) V
این shortcut نیز هست known به عنوان Visual mode line-wise visual subtype.آن را always انتخاب می کند full lines.
When من press را Shift کلید + v کلید، من switch به Visual mode، واین makes متن selections خط by خط این است great برای when منwant به select الف block از text در which را lines هستندdifferent lengths. After من select را first line، و سپس move راcursor پایین (یا Up arrow کلید به move را cursor بالا)، خواهد شدselect را whole line نه matter را difference در line length.
میانبر Control + v
این shortcut نیز هست known به عنوان Visual mode block-wise subtype. آن راselects هر rectangular selection.
When فشار می دهم Control کلید + (lowercase) v کلید، من هستم switched بهVisual mode block-wise subtype. So far، دارم not داشتappropriate opportunity به use این subtype. So far، contrary بهpopular opinion، من find آن را somewhat limiting.
همانطور که opposed به Visual mode visual subtype یاVisual mode line-wise visual subtype، shortest line است selected وkept زمانی که هر دو selecting خطوط down یا up. This است زیرا linesو columns هستند being selected در same time، و columns تشکیل شودازlines از equal width.
حالت فرمان
درخواست کمک از Vim
If من need به find بیرون how انجام دادن something در Vim، من می توانم دسترسی داشته باشم Vimکمک توسط running را :help فرمان من اول make sure من am درNormal mode توسط pressing را esc کلید، و then من press را Shiftکلید + (;) semi-colon کلید، و then تایپ میکنم “help”. And را followingظاهر می شود:
Then من press را Return کلید و following ظاهر می شود:
من could همچنین search برای specific help با :help فرمانFor example، می توانستم تایپ کنم :help visual.txt:
When من want به exit را :help یا :help visual.txt فرمان، من firstرا فشار دهید esc کلید followed توسط :q! و then را Return کلید، کهtakes من out از :help visual.txt فرمان However، من هستم still درCommand mode. If من want به quit به طور کامل out از Vim، I به سادگیhit را Return کلید again، and هستم taken back به Terminal سریع
If من want به remain در Vim، اما want به exit خارج از Command mode،instead از pressing را Return کلید الف second time، من press را(lowercase) i کلید and هستم taken به Insert mode. If من want بهswitch به another mode مانند Normal modeمثلا من press راesc کلید This way، من stay در Vim.
سایر دستورات حالت فرمان
Other حالت فرمان commands (some از which دارند already بوده استcovered):
# Quit file
:q
# write changes to file
:w
# Save and quit
:wq
# Save and quit
😡
# Quit forcefully
:q!
# Save and exit forcefully
:wq!
# Go to first line of file
:1
# Set numbers to lines
:set nu
# Remove numbers from lines
:set nanu
# Specified line number, Same as :1 command, just a different number.
:
# The command output will paste in current cursor position.
:r !date
# Executes shell command “ls” of current working directory in Terminal
:!ls
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
There is زیاد more به Command mode، اما this یک است good start!
شما می توانید dig deeper در Vim توسط checking out مختلف Related Resources.
Vimtutor
By default، Vim نصب کنید includes الف tutorial تماس گرفت vimtutor. هستmeant به help ما learn را Vim اصول اولیه hands-on. ما می توانیم start راvimtutor توسط running را following command در Terminal:
vimtutor
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
منابع مرتبط
فهرست مطالب
پس چرا این مقاله؟
سوال خوب! من استفاده کرده ام Vim در macOS برای مدت بسیار طولانی، اماprimarily برای نوشتن Git پیام ها را متعهد کنید Most از من file editing داردtaken place داخل Visual Studio Code.
Then من started working در Linux، but در یک virtual environment
(VirtualBox) installed در Windows 11. این memory و disk space منallocated به آن بود much smaller از what من دارم available در macOS،
و Visual Studio Code است known بخاطر حریص بودن حافظه من نصبش کردم
لینوکس مینت، اما سرعت آن را به شدت کاهش داد OS و حتی crashed از بهtime به time. That است when من decided به use را Vim ویرایشگر متنinstead.
By default، Vim انجام داد not آن را قطع کن for what من wanted به accomplish، so
من انجام دادم some research به learn more در مورد آن، how من می توانستم configure آن را بهhelp من accomplish آنچه من wanted به accomplish، و better حرکت کنیدVim در general.
Well، من found out که من can اساسا use آن را like هر دیگری editor،
از جمله Visual Studio Code. آن را was الف matter از mastering آن را But
وجود دارد another aspect به this.
در Linux Mint داخل VirtualBox، برای من، پیش فرض است Terminal بر روی استsmall side. من laptop screen است not را largest و Linux Mint
پنجره در VirtualBox است not را largest یا این key input با استفاده ازlaptop keypad است not بیشترین fantastic experience در Windows. بنابراین what
آیا من do در مورد that?
It بود serendipitous. در تحقیقاتم، من accidentally آمد across یک جدیدterminal program تماس گرفت Warp. Currently، آن works در Linux وmacOS یونیکس operating systems. Warp برای Windows است also بیرون آمدنsoon، اما دارد not بوده است released هنوز
Basically، Warp یک است modern، مبتنی بر زنگ terminal با AI ساخته شده در بنابراینyou و your تیم می تواند build عالی software سریعتر
All را Terminal اسکرین شات و گیف I use اینجا بودند taken در Warp.
من آن را دوست دارم Despite داشتن AI ساخته شده، وجود دارد still یک است significant
یادگیری curve. این کار را انجام می دهد detect و بنابراین tools ما use در ماnative OS، مانند shell. استفاده می کنم ZSH در macOS، بنابراین Warp استفاده می کند ZSH. درLinux، استفاده می کنم Bash، بنابراین Warp استفاده می کند Bash وجود دارد. من هستم never رفتن back بهiTerm2 در macOS یا Linux Mintپیش فرض است Terminal برنامه Warp همهway!
Vim مخفف چیست؟
Vim مخفف Vi iMproved. Vi است frequently الف symbolic link به Vim،
را enhanced version از Vi. If شما دارید vim نصب شده و access به
را vim فرمان، تو also دارند access به vi فرمان When من
نصب شده است Vim در Linux Mint، من gained access به both را vim و vi
فرمان
پیکربندی Vim
به make من life بسیار easier چه زمانی working با Vim، من configured آن راusing یک فایل I created (که بود not included توسط default) تماس گرفت.vimrc داخل من home directory (/home/maria در Linux و/Users/mariacam در macOS). من added را following به .vimrc فایل:
# Disable compatibility with vim which can cause unexpected issues.
set nocompatible
set autoindent
# Set tab width to 4 columns.
set tabstop=4
# Use space characters instead of tabs.
set expandtab
# Set shift width to 4 spaces.
set shiftwidth=4
# Add numbers to each line on the left-hand side.
set number
# Highlight cursor line underneath the cursor horizontally.
set cursorline
# Highlight cursor line underneath the cursor vertically.
set cursorcolumn
# Do not save backup files.
set nobackup
# While searching though a file incrementally highlight matching characters as you type.
set incsearch
# Do not wrap lines. Allow long lines to extend as far as the line goes.
set nowrap
# Ignore capital letters during search.
set ignorecase
# Override the ignorecase option if searching for capital letters.
# This will allow you to search specifically for capital letters.
set smartcase
# Show partial command you type in the last line of the screen.
set showcmd
# Show the mode you are on the last line.
set showmode
# Show matching words during a search.
set showmatch
# Use highlighting when doing a search.
set hlsearch
# Set the commands to save in history default number is 20.
set history=1000
# Enable auto completion menu after pressing TAB.
set wildmenu
# Make wildmenu behave like similar to Bash completion.
set wildmode=list:longest
# Wildmenu will ignore files with these extensions.
set wildignore=*.docx,*.jpg,*.png,*.gif,*.pdf,*.pyc,*.exe,*.flv,*.img,*.xlsx
# Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype on
# Enable plugins and load plugin for the detected file type.
filetype plugin on
# Load an indent file for the detected file type.
filetype indent on
# Turn syntax highlighting on.
syntax on
این immediately ساخته شده است navigating فایل ها در Normal mode وcreating content در Insert mode خیلی easier!
If من open من .vimrc فایل using را vim .vimrc فرمان در macOS
iTerm2، اکنون looks like را following:
اگر من open من .vimrc فایل با استفاده از vim .vimrc فرمان در macOS پیچ و تابTerminal، اکنون looks like را following:

Highlighting colors و الف few دیگر details می differ slightly چه زمانی
با استفاده از Warp ترمینال. However، به عنوان shown above، اینها configurations هنوزwork well با Warp و do not باعث هر گونه شود conflicts از آنجایی که Warp کار می کند
با Vim.
دستور vim
این first command به learn در رابطه با Vim است vim فرمان In order
به open الف file در Vim، من run را following در Terminal:
# this command opens up a file called history.txt in Vim
vim history.txt
حالت های Vim
هستند 4 اصلی modes در vim. Normal mode، Insert mode، Visual mode،
و Command حالت According به Warp اسناد، نیز وجود داردReplace mode (که در واقع همینطور است part از Normal mode; Replace mode استredundant) Binary mode، و Org mode. Binary و Org mode سقوطunder بیشتر advanced Vim features و اراده not باشد covered اینجا
با این حال، information مربوط به both است available درRelated Resources واقع در bottom از این article.
حالت عادی
Normal mode است default mode چه زمانی we وارد کنید Vim. آن است mode کهallows ما به move around الف file. Normal mode می تواند باشد accessed ازother modes توسط pressing را esc کلید
میانبر (کوچک) gg
When من press را g کلید twice، gg، من هستم taken به beginning از یکfile.
میانبر G (بزرگ).
When من press را G کلید (Shift key + G کلید)، من هستم taken به end
از یک file.
میانبر # (تعداد) G
من love این one. # یک است placeholder برای line number که شماwant به go to. بیایید بگوییم من want به go به خط کشیدن 15 از یک file در Vim،
من می خواهم go به Normal mode توسط pressing را esc کلید، then تایپ می کرد15G (1 کلید + 5 کلید + Shift key + G key) و من خواهم بود taken بهbeginning از خط 15 از یک file در Vim.
میانبر h (کوچک).
When من press را h کلید، cursor حرکت می کند one character به left.
میانبر j (کوچک).
When من press را j کلید، cursor حرکت می کند one خط down.
میانبر k (کوچک).
When من press را k کلید، cursor حرکت می کند one خط up.
میانبر l (کوچک).
When من press را l کلید، cursor حرکت می کند one شخصیت to راright.
میانبر # (عدد) j
This است similar به میانبر # (تعداد) G.When من press کلید Shift + 6 کلید + j کلید، به عنوان مثال، cursor
حرکت می کند 6 خطوط down.
میانبر # (عدد) k
This است similar به میانبر # (تعداد) G.When من press را 8 کلید + (کوچک) k کلید، به عنوان مثال، cursor
حرکت می کند 8 خطوط up.
میانبر w (کوچک).
When من press را w کلید، cursor حرکت می کند به beginning ازnext word.
میانبر b (کوچک).
When من press را b کلید، cursor حرکت می کند به beginning ازprevious word.
میانبر (کوچک) e
When من press را e کلید، cursor حرکت می کند به end از یک word.
میانبر W (بزرگ).
When من press را Shift کلید + w کلید، cursor حرکت می کند بهbeginning از next word بعد از یک whitespace. این است basically راsame به عنوان میانبر w (کوچک).
میانبر B (بزرگ).
When من press را Shift کلید + b کلید، cursor حرکت می کند بهbeginning از previous word قبل از الف whitespace. این است basically
را same به عنوان میانبر b (کوچک).
میانبر E (بزرگ).
When من press را Shift کلید + e کلید، cursor حرکت می کند به end از یکword قبل از الف whitespace. این است basically را same به عنوان
میانبر (کوچک) e.
میانبر 0 (صفر).
When من press را 0 کلید، cursor حرکت می کند به beginning از یک line.
میانبر ^ (caret).
این ^ (caret) shortcut است same به عنوان
میانبر 0 (صفر). When من press را Shift
کلید + 6 کلید، cursor حرکت می کند به beginning از یک line.
میانبر (علامت دلار) $
When من press را Shift کلید + 4 کلید، cursor حرکت می کند به end از یک
خط
میانبر r (کوچک).
When من move را cursor بیش از یک particular character من want بهreplace، من press را r کلید، و then من press را key ازcharacter من want به replace آن را with.
این way این shortcut آثار است when منmove را cursor بیش ازcharacter من want به replace، و سپس press را r کلید، character
من want به replace است first replaced توسط یک r شخصیت، و then وقتی منpress را character (key) من want به replace آن را with، آن replaces
را r که appeared وقتی من executed
میانبر r (کوچک).
میانبر x (کوچک).
این shortcut است similar به
میانبر r (کوچک). When من move راcursor بیش از character من want به delete، من پس از آن press را x کلید،
و character است deleted. این shortcut نیز هست referred به as را
“دکمه حذف”.
این way این shortcut آثار است when من move را cursor بیش ازcharacter من want به delete، و سپس press را x کلید، character
من wanted به delete است deleted. But اگر من want به bring back راcharacter من دارم deleted، من press (lowercase) u کلید کهexecutes میانبر (کوچک) u.
میانبر X (بزرگ).
این shortcut (معروف به "Backspace button") حذف می کند characters بهleft از cursor، اما not را character آن است highlighting (if آن را
است highlighting یک). So اگر شما delete یک entire line با استفاده از
میانبر X (بزرگ)، except برایcharacter را cursor است highlighting، شما می توانید press (lowercase)x کلید، و character خواهد بود removed.
میانبر (کوچک) dw
When من highlight الف word، و سپس press را d به دنبال آن w کلید،
را word است deleted. و if من want به bring back که word، من می خواهمpress (lowercase) u کلید به undo که edit. If، however، منwanted به replace را word من deleted، من می خواهم want به use
میانبر (کوچک) cw.
In order به highlight الف complete word، من have وارد شدن Visual mode. بهswitch از Normal Mode به Visual mode، من first را فشار دهید esc کلید و
سپس (lowercase) v کلید این esc کلید takes من out از Normal mode،
و (lowercase) v کلید takes من به Visual mode. Then، من use
یا forward arrow کلید به highlight را word به right ازcursor، مطمئن شوید که end برجسته کردن after را last character ازword، یا left arrow کلید به do را same در opposite direction.
میانبر (کوچک) cw
When من highlight الف word، و سپس press را c کلید followed توسطw کلید، word است deleted، و من پس از آن replace آن را با another one.
In order به highlight الف complete word، من have وارد شدن Visual mode. بهswitch از Normal Mode به Visual mode، اول من press را esc کلید وthen (lowercase) v کلید این esc کلید takes من out ازNormal mode، و v کلید takes من به Visual mode. Then، استفاده می کنمeither را forward arrow کلید به highlight را word به right ازcursor، مطمئن شوید که end برجسته کردن after را last character ازword، یا left arrow کلید به do را same در opposite direction.
میانبر (کوچک).
When من highlight الف character با cursor و سپس press را s
کلید، من پس press را key از character من want به replace آن راwith.
میانبر S (بزرگ).
When من highlight را location در یک line جایی که من want به start راedit از من line، and سپس press را Shift کلید + s کلید، آن removes
که content، و then من می توانم type در what من want به replace آن راwith.
میانبر D (بزرگ).
When من place من cursor جایی که من want به start deleting الف line، and
سپس press را Shift + d کلید، آن deletes را line از that point به
را end از line.
میانبر dd (کوچک).
When من select را point از which من want به delete الف line با استفاده از
را cursor، من پس از آن press را d کلید followed توسط pressing را d کلیدagain، and را line از location از cursor در، است deleted.If من place را cursor در beginning از line، entire line
است deleted.
میانبر C (بزرگ).
This است similar به میانبر (بزرگ) D،
اما when من press را Shift کلید + c کلید، after (part of)line بوده است deleted، من می توانم replace آن را با new content.
میانبر (کوچک) yy
This کار می کند similarly به
میانبر dd (کوچک)، except کهinstead از line بودن deleted، it است copied. من first قرار دهیدcursor جایی که من want برای شروع copying را line، و then من press را
(lowercase) y کلید followed توسط pressing (lowercase) y کلیدagain، and را part از line من selected (یا complete line اگر
من placed را cursor در beginning از line) است copied بهclipboard.
میانبر p (کوچک).
After من دارم copied الف line (یا part از it) as per
میانبر (کوچک) yy، I press را p
کلید، و line است pasted زیر line من copied.
میانبر P (بزرگ).
After من دارم copied الف line (یا part از it) as per
میانبر (کوچک) yy، I press را Shift
کلید + p کلید، and را line است pasted above را line من copied.
میانبر (کوچک) u
When من press را u کلید، آن undoes را last change من made. من can
همچنین keep در pressing را u کلید در order به keep در undoing قبلیchanges. Once من هستم happy با current state از من file، من can
نوع :w بنابراین that من save این acceptable state. If من want برای خروجVim بعد از saving را acceptable state، من would به سادگی press را esc
کلید followed توسط : (Shift key +(;) semi-colon کلید)، and منwould باشد taken back به من Terminal سریع
میانبر Control + r
این Control key + r کلید undoes من undo من executed با
میانبر (کوچک) u. و if من want بهrevert به من latest undo، من فقط press (lowercase) u کلیدexecuting میانبر (کوچک) u.
تکرار دستورات
تکرار یک دستور با استفاده از شمارش
Vim پشتیبانی می کند shortcut repetition در Normal mode. Instead از entering
همان shortcut چندگانه times به repeat را desired action، من می توانمprefix را same shortcut با number از repetitions. به delete 6lines، من می خواهم highlight را beginning از line من wanted به start
من deletion با cursor، و then من می خواهم press را 6 کلیدfollowed توسط d کلید and را d کلید again (6dd). This خواهد شدdelete را line جایی که cursor است located و 5 خطوط after که
به repeat میانبر (کوچک) yy، من می خواهمhighlight را beginning از line من wanted به start من copying
با cursor، و then من می خواهم press را 6 کلید (again،for example) followed توسط y کلید و y کلید again (6yy).Next، من می خواهم move را cursor زیر current line و سپس move آن را
5 times بیشتر، برای یک total از 6 خطوط، stopping در last line منwant به copy ( cursor باید appear به "underline" که line).This خواهد شد copy را line جایی که cursor began و 5 خطوط after
که Lastly، من می خواهم press (lowercase) p کلید، not حرکت دادنcursor، و this خواهد شد paste را 6 خطوط I copied.
این screenshot زیر shows را first line که من wanted بهstart copying از و where من placed را cursor در beginning از
که line، و then فشار دادم 6yy (6 کلید + y کلید + y کلید) درbeginning از line جایی که cursor است located:

این screenshot زیر shows را end line از line block من want بهcopy. من moved به این line بعد از من had فشرده شده است 6yy (به عنوان described
بالاتر از first screenshot). من had به take را cursor به line
جایی که من wanted به end را copying process.

Then من pressed را p کلید، و 6 خطوط I copied هستند pasted زیر
را lines من copied:

حالت درج
Insert mode اجازه می دهد me به type/insert شخصیت ها just like الف regular
متن editor. من can وارد کنید Insert mode توسط using یک insert shortcut ازNormal mode.
ورود به حالت Insert
There هستند several ways از ورود Insert mode.
میانبر (کوچک) i
When من press را i کلید، من هستم switched به Insert mode. من know من هستم
در Insert mode چون -- Insert --- ظاهر می شود bottom ازwindow.

میانبر (بزرگ) I
When من press را Shift کلید + i کلید، من switch به Insert mode، and
را cursor حرکت می کند به beginning از line (اگر باشد not در حال حاضرthere).

میانبر (کوچک).
[The (lowercase) a shortcut] behavior است almost را same به عنوان
(کوچک) i میانبر. این only difference استthat وقتی من press را a کلید، و زمان -- Insert --- ظاهر می شودbottom از window، cursor حرکت می کند from را beginning ازline به next character به right.

میانبر (بزرگ) A
When من press را Shift کلید + a کلید، من switch به Insert mode،
و cursor حرکت می کند to را end از line.

میانبر (کوچک) o
After من press را o کلید، من switch به Insert mode، و cursor
حرکت می کند down به second line، creating یک جدید line space. این contents
از second line منتقل می شود down به third line.

میانبر (بزرگ) O
When من press را Shift + o کلید، من switch به Insert mode، and راfirst line حرکت می کند down به second line، leaving الف line space بر روی
خط اول
تغییر از حالت Insert به حالت عادی
In order به switch از Insert mode به Normal mode، من press راesc کلید و if من همیشه هستم unsure آیا من am در Normal mode یا not،
من press را esc کلید به make sure.
حالت بصری
I استفاده کنید Visual mode برای انتخاب text، similar به clicking و dragging
با یک mouse. Selecting text اجازه می دهد shortcuts به apply فقط بهselection، مانند copying، deleting، replacingو غیره
وارد شدن به حالت ویژوال
به make الف text selection، first من press (lowercase) v کلید، and
این will همچنین mark را starting انتخاب point. Then من move راcursor به end selection point، و Vim الف را فراهم می کند visual highlight
از text selection.
میانبر (کوچک) v
این shortcut نیز هست known به عنوان Visual mode visual subtype.
When من want وارد شدن Visual mode visual subtype، من first را فشار دهید esc
کلید به exit هر چه باشد mode من بودم in، و then من press را v کلید، اینswitches من به Visual mode، و will همچنین mark راstart selection point. When من move را cursor در سراسرfirst line، cursor حرکت می کند character توسط character.
Next، من می توانم use را Up arrow یا (lowercase) k کلید بهselect lines رفتن up. As ذکر شده است previously، no موضوع what راdifference در line lengths، lines هستند completely selected. Same
با when من use را Down arrow کلید یا (lowercase) j کلید بهselect lines رفتن down.

میانبر (بزرگ) V
این shortcut نیز هست known به عنوان Visual mode line-wise visual subtype.
آن را always انتخاب می کند full lines.
When من press را Shift کلید + v کلید، من switch به Visual mode، و
این makes متن selections خط by خط این است great برای when منwant به select الف block از text در which را lines هستندdifferent lengths. After من select را first line، و سپس move راcursor پایین (یا Up arrow کلید به move را cursor بالا)، خواهد شدselect را whole line نه matter را difference در line length.

میانبر Control + v
این shortcut نیز هست known به عنوان Visual mode block-wise subtype. آن راselects هر rectangular selection.
When فشار می دهم Control کلید + (lowercase) v کلید، من هستم switched بهVisual mode block-wise subtype. So far، دارم not داشتappropriate opportunity به use این subtype. So far، contrary بهpopular opinion، من find آن را somewhat limiting.
همانطور که opposed به Visual mode visual subtype یاVisual mode line-wise visual subtype، shortest line است selected وkept زمانی که هر دو selecting خطوط down یا up. This است زیرا lines
و columns هستند being selected در same time، و columns تشکیل شود
ازlines از equal width.

حالت فرمان
درخواست کمک از Vim
If من need به find بیرون how انجام دادن something در Vim، من می توانم دسترسی داشته باشم Vim
کمک توسط running را :help فرمان من اول make sure من am درNormal mode توسط pressing را esc کلید، و then من press را Shift
کلید + (;) semi-colon کلید، و then تایپ میکنم "help". And را following
ظاهر می شود:

Then من press را Return کلید و following ظاهر می شود:

من could همچنین search برای specific help با :help فرمانFor example، می توانستم تایپ کنم :help visual.txt:

When من want به exit را :help یا :help visual.txt فرمان، من first
را فشار دهید esc کلید followed توسط :q! و then را Return کلید، کهtakes من out از :help visual.txt فرمان However، من هستم still درCommand mode. If من want به quit به طور کامل out از Vim، I به سادگیhit را Return کلید again، and هستم taken back به Terminal سریع
If من want به remain در Vim، اما want به exit خارج از Command mode،instead از pressing را Return کلید الف second time، من press را
(lowercase) i کلید and هستم taken به Insert mode. If من want بهswitch به another mode مانند Normal modeمثلا من press راesc کلید This way، من stay در Vim.
سایر دستورات حالت فرمان
Other حالت فرمان commands (some از which دارند already بوده استcovered):
# Quit file
:q
# write changes to file
:w
# Save and quit
:wq
# Save and quit
:x
# Quit forcefully
:q!
# Save and exit forcefully
:wq!
# Go to first line of file
:1
# Set numbers to lines
:set nu
# Remove numbers from lines
:set nanu
# Specified line number, Same as :1 command, just a different number.
:
# The command output will paste in current cursor position.
:r !date
# Executes shell command "ls" of current working directory in Terminal
:!ls
There is زیاد more به Command mode، اما this یک است good start!
شما می توانید dig deeper در Vim توسط checking out مختلف Related Resources.
Vimtutor
By default، Vim نصب کنید includes الف tutorial تماس گرفت vimtutor. هستmeant به help ما learn را Vim اصول اولیه hands-on. ما می توانیم start راvimtutor توسط running را following command در Terminal:
vimtutor
منابع مرتبط



