Skip to content

mistricky/codesnap.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

image

Neovim release action status release action status Issues License stars made with lua written in rust

CodeSnap.nvim

๐Ÿ“ธ Snapshot plugin with rich features that can make pretty code snapshots for Neovim

๐ŸšฃMigration

If you have installed v0.x before, this chapter will show you what break changes version v1.x introduced.

  • The CodeSnapPreviewOn command is not supported, if you prefer live-preview, you can pin CodeSnap.nvim version to v0.0.11 to continue using this command.
  • The opacity and preview_title config has been removed from v1.0.0
  • The editor_font_family was renamed to code_font_family

v1.x has a different architecture and better performance than v0.x, and v1.x can generate screenshots directly without an open browser. We recommend you upgrade to v1.x for a better experience.

โœจFeatures

  • ๐Ÿคฉ Beautiful code snap template
  • ๐Ÿ˜Ž Custom watermark and window style
  • ๐Ÿ’ป Beautiful Mac-style title bar
  • ๐Ÿค– Generate snapshots using only a single command
  • ๐Ÿž Breadcrumbs for display file path
  • ๐ŸŒŠ More beautiful background theme
  • ๐Ÿ”ข Support for display line number make sharing code snapshot easier

Prerequirements

  • Neovim 0.9.0+

Install

We recommend using Lazy.nvim to install CodeSnap.nvim, but you can still use another plugin manager you prefer.

Lazy.nvim

{ "mistricky/codesnap.nvim", build = "make" },

Packer

use {'mistricky/codesnap.nvim', run = 'make'}

Vim-Plug

Plug 'mistricky/codesnap.nvim', { 'do': 'make' }

It's worth mentioning that the screenshot feature is implemented by a module called generator written in Rust, when make the project, the CodeSnap.nvim will mount a precompiled cross-compile generator shared file into the plugin that depends on what OS you are using.

We precompiled the following targets:

  • x86_64-unknown-linux-gnu
  • x86_64-apple-darwin
  • aarch64-apple-darwin

If your platform is in the above list, you can just run make after the plugin is installed like the above examples do, CodeSnap.nvim will automatically mount the shared file into the plugin. This means you don't need any Rust utils to compile manually from source.

Compile from source

You need to install Rust development environment before compiling from source, you can refer Install Rust for more detail.

Please keep in mind, cross-compile to these platforms only helps a portion of users to have out-of-box experience, if your platform is not in the above targets list, you still need to compile from source using make build_generator, for instance using Lazy:

{ "mistricky/codesnap.nvim", build = "make build_generator" },

We always recommend you to compile CodeSnap.nvim from source instead of using the precompiled shared file, because the correctness and consistency of compiling from source are always higher than cross-compiling.

Compile on ARM

If you try to compile CodeSnap.nvim on ARM architecture, you may need to install additional dependencies to compile it, thanks @matteocavestri mentioned in #53 (comment)

export CC=gcc
sudo dnf install libuv libuv-devel # On RHEL based systems
sudo apt-get install libtool libuv1-dev # On Debian based systems

Keymappings

If you use Lazy.nvim as your package manager, here are some examples show you how to configure keymappings for CodeSnap:

{
  "mistricky/codesnap.nvim",
  build = "make build_generator",
  keys = {
    { "<leader>cc", "<cmd>CodeSnap<cr>", mode = "x", desc = "Save selected code snapshot into clipboard" },
    { "<leader>cs", "<cmd>CodeSnapSave<cr>", mode = "x", desc = "Save selected code snapshot in ~/Pictures" },
  },
  opts = {
    save_path = "~/Pictures",
    has_breadcrumbs = true,
    bg_theme = "bamboo",
  },
}

Usage

CodeSnap.nvim provides the following two ways to take snapshots of currently selected code

Copy into the clipboard

To take a beautiful snapshot use CodeSnap.nvim, you can just use CodeSnap command to generate a snapshot of the current selected code, then the CodeSnap.nvim will write the snapshot into the clipboard, and you can paste it anywhere you want.

Clipboard.mov

Copy into clipboard on Linux Wayland

Copy screenshots directly into the clipboard is cool, however, it doesn't work well on wl-clipboard, because the wl-clipboard can't paste the content which come from exited processes. As Hyprland document say:

When we copy something on Wayland (using wl-clipboard) and close the application we copied from, the copied data disappears from the clipboard and we cannot paste it anymore. So to fix this problem we can use a program called as wl-clip-persist which will preserve the data in the clipboard after the application is closed.

If you using CodeSnap.nvim on wl-clipboard, you can refer wl-clip-persist, it reads all the clipboard data into memory and then overwrites the clipboard with the data from our memory to persist copied data.

Save the snapshot

Of course, you can use CodeSnapSave command to save the snapshot to path where you defined it in config.save_path

require("codesnap").setup({
  -- The save_path must be ends with .png, unless when you specified a directory path,
  -- CodeSnap will append an auto-generated filename to the specified directory path
  -- For example:
  -- save_path = "~/Pictures"
  -- parsed: "~/Pictures/CodeSnap_y-m-d_at_h:m:s.png"
  -- save_path = "~/Pictures/foo.png"
  -- parsed: "~/Pictures/foo.png"
  save_path = ...
})
Save.mov

Specify language extension

In some scenarios, CodeSnap.nvim cannot auto-detect what language syntax should used to highlight code, for example, shell script can have no extension, they specify interpreters using shebang.

CodeSnap.nvim won't read the whole content of the file, thus cannot detect what language syntax is this, in this case, you can specify extension explicitly, for instance:

CodeSnapSave sh
CodeSnap sh

Breadcrumbs

Breadcrumbs are something to display the current snapshot file path, you can open it through config has_breadcrumbs:

require("codesnap").setup({
  -- ...
  has_breadcrumbs = true
})

The breadcrumbs look like: image

Custom path separator

The CodeSnap.nvim uses / as the separator of the file path by default, of course, you can specify any symbol you prefer as the custom separator:

require("codesnap").setup({
  -- ...
  has_breadcrumbs = true
  breadcrumbs_separator = "๐Ÿ‘‰"
})

image

Line number

We also support displaying line number, you can set has_line_number to true to display line number.

require("codesnap").setup({
  // ...
	has_line_number = true,
})

image

Custom background

The CodeSnap.nvim comes with many beautiful backgrounds preset, you can set any background you like by setting bg_theme to its name, just like:

require("codesnap").setup({
  -- The "default" background is one you see at the beginning of the README
  bg_theme = "default"
})
bamboo sea
peach grape
dusk summer

Solid color background

If you prefer solid color background, you can set bg_color to your preferred color. For example:

require("codesnap").setup({
  -- ...
  bg_color = "#535c68"
})

CodeSnap

Watermark

Watermark is something that makes screenshots more personalized, but if you don't like watermark just set it as an empty string to hide it.

require("codesnap").setup({
  -- ...
  watermark = ""
})

Commands

CodeSnap # Take a snapshot of the currently selected code and copy the snapshot into the clipboard

CodeSnapSave # Save the snapshot of the currently selected code and save it on the disk

Lua

local codesnap <const> = require("codesnap")

-- Take a snapshot of the currently selected code and copy the snapshot into the clipboard
codesnap.copy_into_clipboard()

-- Save the snapshot of the currently selected code and save it on the disk
codesnap.save_snapshot()

Configuration

Define your custom config using setup function

require("codesnap").setup({...})

There is a default config:

{
    mac_window_bar = true,
    title = "CodeSnap.nvim",
    code_font_family = "CaskaydiaCove Nerd Font",
    watermark_font_family = "Pacifico",
    watermark = "CodeSnap.nvim",
    bg_theme = "default",
    breadcrumbs_separator = "/",
    has_breadcrumbs = false,
    has_line_number = false
}

Contribution

CodeSnap.nvim is a project that will be maintained for the long term, and we always accepts new contributors, please feel free to submit PR & issues.

The commit message convention of this project is following commitlint-wizardoc.

License

MIT.