Banner of Banner of vim text editor : ftplugin and code snippets

Vim Text Editor: ftplugin and snippets


Category: vim

Date: February 2023
Views: 605


Vim is a highly efficient and versatile text editor that has been around for over two decades. It is widely used by developers, sysadmins, and power users for its unique features and capabilities. Vim offers a rich set of features for editing text, programming, and automation that make it a favorite among users who want to get more done with fewer keystrokes.

History of Vim

Vim was originally created as a clone of the original Vi text editor, which was written in the 1970s. Since its initial release, Vim has undergone many improvements and modifications, making it a highly advanced text editor that is widely used today. Vim was written by Bram Moolenaar in 1991 and is free and open-source software.

Features of Vim

  • Modal Editing: Vim uses a modal editing system, which allows users to switch between different modes, such as normal mode, insert mode, and visual mode. This feature provides users with more control over their text and helps them to perform editing tasks more efficiently.
  • Command Line Interface: Vim offers a powerful command line interface that makes it easy for users to perform complex editing tasks with just a few keystrokes. This interface allows users to automate editing tasks, making it a great tool for repetitive or complex editing tasks.
  • Customizability: Vim is highly customizable, allowing users to tailor the editor to their specific needs. Vim users can create custom macros, key bindings, and plugins that make the editor work exactly the way they want it to.
  • Support for Multiple Languages: Vim supports a wide range of programming languages, making it an ideal choice for developers who need to work with multiple languages in a single text editor.

here are some of my personal configs for vim

1. ftplugins

create a directory named "ftplugin" inside your .vim directory and create the following files in it . ftplugin stands for file type plugin, ie a set of commands that will be available for specific filetypes. vim reads the contents of this directory on startup so no need to config anything else

sh.vim

    
" filename sh.vim
" the following function will run the shell script code in another
" buffer, I use it a lot when making tutorials about shell scripting
" as the next line after endfunction is a keybinding to run the script
" <tab> takes me to and back from the script and its output

" the last line is to <ESC> escape to NORMAL mode, ":0r"
" means read the file given by its fullpath into the first line of the script file
" the file simply contains the shabang that is written on top of bash scripts
" #!/bin/bash

function! RunBash()
    let s:current_file = expand("%")
    enew|silent execute ".!bash " . shellescape(s:current_file, 1)
    setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
endfunction

nnoremap <Leader>c :call RunBash()<CR>
inoremap ;sh <ESC>:0r ~/.vim/mysnippets/bash/shabang<CR>
    

cpp.vim

    
" the same principle
" this one is for cpp files
" the function will compile the code, execute and show the output, or errors


function! RunCPP()
    let s:current_file = expand("%")
    enew|silent execute ".!g++ " . shellescape(s:current_file, 1) . " -o " . shellescape(s:current_file, 1) . ".out && ./" . shellescape(s:current_file, 1) . ".out"
    setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
endfunction


nnoremap <Leader>c :call RunCPP()<CR>

" the following keybindings work in insert mode, for example typing ;io
" will insert the directive #include<iostream> and go to the next line
" the code snippets are the entire main function

inoremap ;io #include<iostream><enter>
inoremap ;st using namespace std;<enter><enter>
inoremap ;li #include<stdlib.h><enter>
inoremap ;mm #include<math.h><enter>
inoremap ;ma <ESC>:0r ~/.vim/mysnippets/cpp/main<CR>
inoremap ;maa <ESC>:0r ~/.vim/mysnippets/cpp/main1<CR>
inoremap ;mz <ESC>:0r ~/.vim/mysnippets/cpp/main1<CR>

    
an example of using c++ snippets and compiling and running c++ code from vim
an example of using c++ snippets and compiling and running c++ code from vim

Conclusion

Vim is a powerful and versatile text editor that is widely used by developers, sysadmins, and power users for its unique features and capabilities. Whether you are a beginner or an experienced user, Vim offers something for everyone. So, if you're looking for a text editor that can help you get more done with fewer keystrokes, give Vim a try!



605 views

Previous Article Next Article

0 Comments, latest

No comments.